Python by topic
101 posts about Python, organized by topic rather than by date. Each topic links to its full archive, with the most recent posts listed to get you started.
Functions & parameters
Functions package reusable code, and parameters control what goes into them; these posts cover parameter types, keyword arguments, print's lesser-known options, and how functions work internally.
- Python parameters primer
- Write better Python functions!
- Become more fluent with Python functions in just 15 weeks
- Function Dissection Lab: Learn how Python functions work by examining their innards
- Improve your Python: WPE A2 (“Functions for beginners”) starts next week!
- Beyond the “hello, world” of Python’s “print” function
- foo(y=y), and similar code that confuses Python newbies
- Five Python function parameters you should know and use
- Python function brain transplants
Objects & classes
Classes and objects organize data and behavior in Python, and these posts cover attribute lookup with ICPO, identity versus equality, __init__, and why object-oriented code is worth writing.
- Python is vs ==: Object identity and value equality
- The “why” of object-oriented programming (in Python)
- Let’s de-confuse Python objects!
- LEGB? Meet ICPO, Python’s search strategy for attributes
- Master Python objects (and understand how they work)
- A short Python class puzzle
- Python’s objects and classes — a visual guide
- In Python, it’s all about the attributes
- Teaching and acting (or, why I don’t plan to sell recorded classes in the near future)
- Benchmarking old-style and new-style Python classes
- Making Python’s __init__ method magical
Strings & sequences
Strings, lists, and tuples share one sequence API in Python, and these posts cover slicing and reversing, str.join, raw strings on Windows, isdigit versus isnumeric, and the speed of concatenation and formatting.
- Python str.join(): Why it’s a string method, not a list method
- Reverse a string or list in Python with the [::-1] slice
- Python sequences: The shared API of str, list, and tuple
- Quick Python tip: “int” strips strings of whitespace
- Playing with Python strings, lists, and variable names — or, a complex answer to a simple question
- Understanding Python slices
- Python’s str.isdigit vs. str.isnumeric
- Avoiding Windows backslash problems with Python’s raw strings
- Raw strings to the rescue!
- Speedy string concatenation in Python
- The relative speeds of str.format and %
- Teaching an old dog new tricks — or, how I learned to love Python’s str.format, and gave up on %
Lists & tuples
Lists and tuples are Python's core ordered collections, one mutable and one not, and these posts cover slicing, joining, sorting, list comprehensions, the shared sequence API, and quirks like += with a tuple.
- Python str.join(): Why it’s a string method, not a list method
- Reverse a string or list in Python with the [::-1] slice
- Python sequences: The shared API of str, list, and tuple
- Playing with Python strings, lists, and variable names — or, a complex answer to a simple question
- Why do Python lists let you += a tuple, when you can’t + a tuple?
- Python parentheses primer
- Implementing “zip” with list comprehensions
- Understanding nested list comprehensions in Python
- Sorting lists of dicts — an exercise from “Practice Makes Python”
- Turning a PostgreSQL array to rows
- Turning PostgreSQL rows into arrays
- PostgreSQL array indexes and length
- Good intentions, unexpected results: Mailing lists and DMARC
Dictionaries & sets
Dictionaries map keys to values and sets store unique unordered items, both relying on hashing; these posts cover subset checks, dict.fromkeys pitfalls, memory usage, hashability, sorting lists of dicts, and building dicts with reduce.
- Python set subset checks with <=: Spelling Bee word validation
- dict.fromkeys: Why a mutable default value gets shared
- Today’s lesson in “Python for non-programmers”: Dictionaries
- Python dicts and memory usage
- Is it hashable? Fun and games with hashing in Python
- Sorting lists of dicts — an exercise from “Practice Makes Python”
- Creating Python dictionaries with “reduce”
Comprehensions
Comprehensions build lists, dicts, and sets from an iterable in a single expression; these posts explain nested comprehensions, rebuilding zip with them, and analogies to Excel and SQL thinking.
- You can, but should you? Combining some of Python’s more esoteric features
- Implementing “zip” with list comprehensions
- Understanding nested list comprehensions in Python
- Want to understand Python’s comprehensions? Think in Excel or SQL.
Iterators & generators
Objects that produce values one at a time, on demand, rather than all at once; these posts show how to implement the iterator protocol yourself and how generators, coroutines, and "yield from" fit together.
- Making sense of generators, coroutines, and “yield from” in Python
- A quick introduction to implementing Python iterators
Decorators
A decorator wraps a function to add behavior without changing its code; these posts demystify how they work, show why functools.wraps matters, and share slides and code from a conference talk.
- Stop fearing Python decorators
- Making your Python decorators even better, with functool.wraps
- Get code + slides from my “Practical Decorators” talk from Euro Python / PyCon 2019
Map, filter & reduce
Python's functional trio transforms, selects, and combines elements of a sequence; these posts work through map's mindset and a long series on reduce, using it to build dictionaries, min and max, filter, and Scrabble scores.
- Thinking with “map”
- Summary of my “reduce” series
- Implementing “filter” with “reduce”, in Ruby and Python
- Implementing “map” with “reduce”, in Ruby and Python
- Creating Ruby hashes with “reduce”
- Creating Python dictionaries with “reduce”
- Creating collections with “reduce”
- Implementing “min” and “max” with “reduce”
- Calculating Scrabble scores with “reduce”
- Understanding “reduce” (first in a series)
Scoping & namespaces
Python decides which variable or attribute a name refers to using rules like LEGB and ICPO; these posts walk through those lookup paths, keyword-argument confusion, and odd cases like True+True.
- LEGB? Meet ICPO, Python’s search strategy for attributes
- foo(y=y), and similar code that confuses Python newbies
- 2+2 might be 4, but what is True+True?
- Fun with Python scoping rules
Modules & imports
Python code is organized into modules that you load with import; these posts trace how a module name is resolved to a file on sys.path, how to reload a module while developing, and how the subprocess module handles shell globbing.
- importlib.reload: Reimporting a Python module during development
- Python import: How a module name becomes a file on sys.path
- Globbing and Python’s “subprocess” module
Exceptions & error handling
Python signals problems by raising exceptions, and these posts cover raising and catching them, working with warnings, and reading a traceback.
- Working with warnings in Python (Or: When is an exception not an exception?)
- My favorite terrible Python error message
- How not to write an error message
More topics
AI-assisted coding
Using LLMs and agents as part of everyday Python work, with posts on learning to code alongside AI, agentic coding workflows, and how developer skills are shifting.
- New: In-browser Python/Pandas/Git exercises, with AI support
- Is Python becoming Pinyin?
- Take agentic coding to the next level
- Learn to code with AI — not just write prompts
- We’re all VCs now: The skills developers need in the AI era
- My Week with ChatGPT
course-setup
A tool for getting students' Python environments ready before training begins; this post introduces course-setup and explains how instructors can use it in their own classes.
Dates & times
Working with dates and times in Python means using the datetime module's objects and methods, and here you'll see how weekday() identifies Saturdays and Sundays.
Files & I/O
Reading and writing files means dealing with paths, handles, and cleanup, and these posts cover how imports resolve module names to files, deleting files based on other filenames, and when Python actually closes a file without "with".
- Python import: How a module name becomes a file on sys.path
- Using Python to delete files based on other files’ names
- If you don’t use “with”, when does Python close files? The answer is: It depends.
Git
Git is the version-control system most developers use daily, and these posts cover practical shortcuts like jumping back to your last branch, plus courses and in-browser exercises for practicing it.
- New: In-browser Python/Pandas/Git exercises, with AI support
- Save time — and 30% — with my 25 Python, Git, and data science courses!
- In PostgreSQL, as in life, don’t wait too long to commit
- The easiest way to return to the last Git branch
Identity & equality
Python's `is` operator checks whether two names point to the same object, while `==` compares values; these posts explain the difference and why `is` is rarely what you want.
Loops & control flow
Loops and conditionals decide how many times your code runs and in what order; here you'll find a look at Python's lesser-known else clause on for and while loops.
Numbers & precision
Python's numeric types behave in surprising ways, and these posts explain banker's rounding, floating-point quirks, underscores in numeric literals, bitwise operators, and why booleans can be added like integers.
- Python round and banker’s rounding: Why round(2.5) returns 2
- Python numeric literals: Using underscores to group digits
- Understanding bitwise operations in Python
- 2+2 might be 4, but what is True+True?
- Fun with floats
Regular expressions
Regular expressions are compact patterns for matching text, and these posts offer encouragement and practical guidance for learning them, plus how to apply them inside PostgreSQL queries.
Shell & developer tools
The Unix command line is where most Python work happens, and these posts cover two everyday tools: the find utility for locating files, and Control-R for searching your shell history.
Syntax & formatting
Python's punctuation and line structure shape how readable your code is; these posts cover what parentheses do, how to split long lines across multiple lines, and when clever syntax goes too far.
- Python line continuation: How parentheses split long lines
- You can, but should you? Combining some of Python’s more esoteric features
- Python parentheses primer
Teaching Python
Helping others learn Python brings its own challenges, and this post introduces course-setup, a tool for getting students' environments ready before training begins.
uv & packaging
uv is a fast Python package and project manager that replaces pip and virtualenv; these posts show how to start with it and avoid common mistakes in how people use it.
Variables & assignment
Variables in Python are names bound to objects rather than boxes holding values, and these posts cover how assignment works, the several ways to assign names, and why comparison and assignment differ.
