Python Koans
Python reads almost like plain English, which makes it a favorite first language, and it’s just as much at home in professional work: web backends, automation, science, data analysis and machine learning. Its large standard library means many tasks need nothing extra installed. These koans cover numbers and strings, lists, dictionaries and sets, functions, generators, classes and exceptions, and a tour of the standard library.
86 koans in 13 groups, tested with pytest. Each is a small program whose tests fail: replace every ___ until they pass.
Awakening
How a koan checks your answers, and what Python counts as true.
Numbers
Integers, floats, the operators between them, and exact alternatives.
Strings
Quoting, f-strings and format specs, slicing, methods, escapes, and bytes.
- Quoting and f-strings
- Format specs
- Indexing and slicing
- String methods
- Split and join
- Comparing strings
- Escapes and raw strings
- Bytes and encodings
Lists
Python's workhorse sequence: changing it, sorting it, copying it, and building it.
Tuples, dicts and sets
Python's other built-in collections, and the collections module.
Control flow
Choosing and repeating: if, for, while, match, and the walrus, :=.
Functions
Parameters, defaults, closures, scope, decorators and recursion.
- Functions
- Default arguments
- *args and **kwargs
- Keyword-only and positional-only
- Lambdas and higher-order functions
- Closures and nonlocal
- Scope
- Decorators
- Recursion
Iterators and generators
How for loops really work, and lazy sequences.
Classes
Defining your own types: attributes, properties, special methods, inheritance and dataclasses.
- Classes
- Class and instance attributes
- Properties
- Special methods
- Inheritance
- Class and static methods
- Dataclasses
- Abstract classes
- Slots
- Descriptors
- Class creation hooks
Exceptions
Catching, raising, chaining, and cleaning up with context managers.
The standard library
A few of the modules that come with Python.
Type hints
Annotations, unions, protocols, typed dicts and generics: what they say, and what Python does with them when it runs.
Puzzles
Small programs that put the path together: read them closely and predict what they do.