Skip to main content
Play · Sandbox

Python Katas

A kata is a small problem you solve more than once. The first attempt teaches you the answer; the third teaches you the language. Everything below runs in a browser or in a single file on your own machine - no project to set up before you start.

What you practise here

  • Comprehensions, generators and lazy pipelines
  • The standard library: itertools, collections, dataclasses
  • Writing the test before the implementation
  • Reading a traceback without panicking

Notebooks & hosted labs

Environments that run in a browser tab or in one command on your own machine. Nothing here needs a licence.

  • JupyterLab running entirely in a browser tab. Nothing to install, nothing to sign up for - the fastest way from curiosity to a running cell.

    Project Jupyter
  • Hosted notebooks that save to Google Drive and share like a document. The free tier includes a GPU runtime, which matters once katas turn into experiments.

    Google Research
  • Paste the URL of any public Git repository and Binder builds it into a live notebook server. How to try someone else's code without cloning it.

    Project Jupyter
  • The full text of the book as Jupyter notebooks. Every figure is code you can edit, which makes it a sandbox as much as a reference.

    Jake VanderPlas

Starter code samples

Short enough to read in one go and to paste into a cell. Change them until they break — that is the exercise.

A generator costs the same whatever you put in it

python

The first kata most people meet. Both expressions describe a million squares; only one of them holds a million squares.

import sys

squares_list = [n * n for n in range(1_000_000)]
squares_gen = (n * n for n in range(1_000_000))

sys.getsizeof(squares_list)   # ~8 MB  - every square exists at once
sys.getsizeof(squares_gen)    # ~200 B - each square exists for one loop

# The kata: keep the pipeline lazy end to end, then measure again.
total = sum(n * n for n in range(1_000_000) if n % 3 == 0)

A kata harness you can reuse all evening

python

Write the cases first and the implementation has somewhere to aim. Run it with pytest -q and the failures read as a to-do list.

import pytest

from katas.rle import encode


@pytest.mark.parametrize(
    "raw, expected",
    [
        ("", ""),
        ("a", "a1"),
        ("aab", "a2b1"),
        ("aaabbbcccd", "a3b3c3d1"),
    ],
)
def test_encode_collapses_runs(raw, expected):
    assert encode(raw) == expected

The standard library has already solved it

python

Half of Python katas are a collections or itertools call in disguise. Learning which one is the actual exercise.

from collections import Counter
from itertools import groupby

words = "the quick brown fox jumps over the lazy brown dog".split()

Counter(words).most_common(2)
# [('the', 2), ('brown', 2)]

# groupby only groups runs, which is why sorting first is not optional.
[(k, len(list(g))) for k, g in groupby(sorted(words, key=len), key=len)]
# [(3, 5), (4, 1), (5, 3)]

Free practice grounds

Places to keep going once the samples run. Every one is usable without paying; where an account is needed we have said so.

Reference & documentation

What to read when the sandbox stops answering the question.

Keep going with the community

The links above belong to the wider web. These ones belong to us — written, reviewed and taught by members.

Other sandboxes