koans.app

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.

Start the path

Awakening

How a koan checks your answers, and what Python counts as true.

  1. Asserts
  2. Truth
  3. None and identity
  4. Code that raises
  5. Types
  6. Names

Numbers

Integers, floats, the operators between them, and exact alternatives.

  1. Arithmetic
  2. Division
  3. Big and small
  4. Conversion
  5. Comparison
  6. The math module
  7. Fractions and decimals

Strings

Quoting, f-strings and format specs, slicing, methods, escapes, and bytes.

  1. Quoting and f-strings
  2. Format specs
  3. Indexing and slicing
  4. String methods
  5. Split and join
  6. Comparing strings
  7. Escapes and raw strings
  8. Bytes and encodings

Lists

Python's workhorse sequence: changing it, sorting it, copying it, and building it.

  1. Lists
  2. Slicing and changing
  3. List methods
  4. Sorting
  5. Comprehensions
  6. Copies
  7. Unpacking

Tuples, dicts and sets

Python's other built-in collections, and the collections module.

  1. Tuples
  2. Dictionaries
  3. Dict methods
  4. Dict comprehensions
  5. Sets
  6. The collections module

Control flow

Choosing and repeating: if, for, while, match, and the walrus, :=.

  1. if
  2. for
  3. while and loop else
  4. match
  5. The walrus

Functions

Parameters, defaults, closures, scope, decorators and recursion.

  1. Functions
  2. Default arguments
  3. *args and **kwargs
  4. Keyword-only and positional-only
  5. Lambdas and higher-order functions
  6. Closures and nonlocal
  7. Scope
  8. Decorators
  9. Recursion

Iterators and generators

How for loops really work, and lazy sequences.

  1. Iteration
  2. Generators
  3. Generator expressions
  4. itertools
  5. Built-ins for iterables

Classes

Defining your own types: attributes, properties, special methods, inheritance and dataclasses.

  1. Classes
  2. Class and instance attributes
  3. Properties
  4. Special methods
  5. Inheritance
  6. Class and static methods
  7. Dataclasses
  8. Abstract classes
  9. Slots
  10. Descriptors
  11. Class creation hooks

Exceptions

Catching, raising, chaining, and cleaning up with context managers.

  1. try and except
  2. Raising
  3. Exception chaining
  4. Context managers
  5. Exception groups

The standard library

A few of the modules that come with Python.

  1. functools
  2. operator
  3. datetime
  4. json
  5. Regular expressions
  6. enum
  7. heapq and bisect
  8. statistics and random

Type hints

Annotations, unions, protocols, typed dicts and generics: what they say, and what Python does with them when it runs.

  1. Annotations
  2. Unions
  3. Protocols and TypedDict
  4. Generics

Puzzles

Small programs that put the path together: read them closely and predict what they do.

  1. FizzBuzz
  2. Word frequencies
  3. Anagrams
  4. Run-length encoding
  5. The sieve of Eratosthenes