• Home
  • About
    • About The Coder
    • Language List
  • Code Rules
    • #1: Clarity Trumps Everything
    • #2: Source Code is for Humans
    • #3: Don’t Ignore Warnings
    • #4: Comment As You Go
    • #5: Always Use Parentheses
    • #6: Always Define Literals
    • #7: Never Repeat Yourself
  • CS-101
  • Python-101
  • Simple Tricks
  • Guestbook

The Hard-Core Coder

~ I can't stop writing code!

The Hard-Core Coder

Category Archives: Python

Exploring Python.

Simple Python Tricks #1

18 Saturday Nov 2023

Posted by Wyrd Smythe in Python

≈ 3 Comments

Tags

Python code, Simple Tricks

While I love coding, it turns out I don’t enjoy blogging about it quite as much as I thought I would. But I’m not ready to toss in the towel yet, and to help develop more of a habit of posting here, I thought I’d try posting some really simple things.

These are simple tricks intended to help beginners dig deeper into coding with Python.

Continue reading →

Python Descriptors, part 2

18 Wednesday Oct 2023

Posted by Wyrd Smythe in Python

≈ 2 Comments

Tags

Python 101, Python code, Python descriptors, Python properties

Python has the useful notion of descriptor objects, which give object attributes control over how they are accessed. Descriptors enable calculated-on-the-fly attribute values and can prevent or control modification of data values.

The previous post covered the basics. In this post, I’ll dig deeper into Python descriptors with some more involved examples. This post assumes the basics covered in the previous post.

Continue reading →

Python Descriptors, part 1

17 Tuesday Oct 2023

Posted by Wyrd Smythe in Python

≈ 3 Comments

Tags

Python 101, Python code, Python descriptors, Python properties

Python has the useful notion of descriptor objects as well as the built-in property() function to make using them in the most common cases — read-only and calculated instance attributes — quite easy.

In this post I’ll explore Python descriptors with lots of examples demonstrating how to use them. Descriptors are an important aspect in understanding Python and using it effectively.

Continue reading →

Musical Scale Modes Table

01 Sunday Oct 2023

Posted by Wyrd Smythe in Fun, Python

≈ 2 Comments

Tags

computer code, Python code, software design

Mathematician and educator John Baez has an excellent series of blog posts about music theory. The seventh concerns generating scales by using notes separated by fifths. Shifting the start point generates the seven major scale modes. Shifting the root key generates those seven modes in the twelve keys (a total of 7×12=84 scales).

John asked if any of his readers would be interested in creating that table of all 84 rows. It sounded like — and turned out to be — a fun exercise. This post explores in detail the Python solution I came up with.

Continue reading →

Calculating the Number e (in Python)

10 Sunday Sep 2023

Posted by Wyrd Smythe in Fun, Python

≈ 2 Comments

Tags

computer code, exponential functions, math, Python code

It has been almost a year since my last post here. I haven’t been idle code-wise, but I have been too distracted by Real Life to do any blogging here. My publication rate is way down on my main blog, too.

To get me posting here more, I think I need more of a focus on casually documenting my own little projects rather than Code Wise articles. Those take more work than I seem willing to put in these days. With that in mind, I have some trivial Python fun to share…

Continue reading →

Python String Translate

13 Thursday Oct 2022

Posted by Wyrd Smythe in Python

≈ 3 Comments

Tags

Python 101, Python code, str.translate, translate method

Python str instances have many useful methods. I use strip, split, startswith, and others, quite a lot, for instance. One method I haven’t had reason to use so far is translate. It takes a dictionary argument and uses it to map the existing string to a new string.

It’s flexible and useful, so it’s worth knowing how to use.

Continue reading →

Loving the Lambda

15 Saturday Jan 2022

Posted by Wyrd Smythe in Python

≈ 3 Comments

Tags

computer code, program, Python 101, Python code, Python lambda

One part of Python I especially appreciate is lambda functions. While I’ve never pursued functional programming, I do like many things about it, particularly the notion of functions as native data objects. Programming with functional objects opens new vistas. Most languages handle it one way or another, but languages make it natural.

Python’s lambda is such a facility, and I use it often. This week I finally got around to writing a lambda function I’ve been meaning to for a long time, and it’s my new favorite. I thought I’d share it along with some of my other “one-liners”…

Continue reading →

Calculating Entropy (in Python)

21 Tuesday Dec 2021

Posted by Wyrd Smythe in Python

≈ 13 Comments

Tags

entropy, Python code, Shannon entropy

I’ve been playing with calculating the entropy of a toy system used to illustrate the connection between “disorder” and entropy. (See Entropy 101 and Entropy 102.) I needed to calculate the minimum number of moves required to sort a disordered collection, but that turns out to be an NP problem (no doubt related to Traveling Salesman).

The illustration bridges Boltzmann and Shannon entropy, which got me playing around with the latter, and that led to some practical results. This article describes the code I used mainly as an excuse to try embedding my own colorized source code blocks.

Continue reading →

Building a Turing Machine

10 Friday Dec 2021

Posted by Wyrd Smythe in Fun, Python

≈ 1 Comment

Tags

modeling, Python code, simulation, Turing Machine

I’m not sure how I got to thinking about Turing Machines (TMs). I was going through some files recently and spent some time looking at busy-beaver.c (from 2017 according to the file date). It contains an implementation of a TM. But something else got me speculating about my own implementation; I just don’t recall what.

I decided to actually write it when it occurred to me that I could use a Python generator to implement the Turing Machine tape. Sadly, I didn’t think of it in time for the trilogy I just published about Python generators (part 1, part 2, part 3).

Continue reading →

Python Tokenize

03 Friday Dec 2021

Posted by Wyrd Smythe in Python

≈ 1 Comment

Tags

HTML, Python 101, Python code

Recently I thought to myself, “Hey self… Although I’ve never looked into them, I know Python has tools for parsing Python code. I wonder if they might make generating syntax-highlighted HTML pages of Python scripts pretty easy?”

I found the help page for the tokenize module, and the introduction says it’s “useful for implementing ‘pretty-printers,’ including colorizers for on-screen displays.” That sounds like a strong yes.

Continue reading →

← Older posts
Newer posts →
Follow The Hard-Core Coder on WordPress.com

The Posts

  • Python Tk Calculator App
  • Tk Windows in Python
  • Dual Numbers in Python
  • Python Decorators, more
  • Python Decorators, redux
  • Byte Multiplication Trick
  • Parsing Multipart Form Data
  • Simple Python Tricks #18
  • Simple Python Tricks #17
  • Simple Python Tricks #16

The Topics

  • Blog (4)
  • CS101 (37)
  • Fun (36)
  • Interesting (5)
  • Opinion (12)
  • Python (59)
  • Stories (15)

The Month

December 2025
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
293031  
« Nov    

The Past

Posts

RSS Feed

Comments

RSS Feed

The Main Blog

Visit Logos con carne

Lots of wyrds... with meat!

The Author

Wyrd Smythe

Wyrd Smythe

The canonical fool on the hill watching the sunset and the rotation of the planet and thinking what he imagines are large thoughts.

View Full Profile →

Create a website or blog at WordPress.com

  • Subscribe Subscribed
    • The Hard-Core Coder
    • Already have a WordPress.com account? Log in now.
    • The Hard-Core Coder
    • Subscribe Subscribed
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar