Skip to content

Python quick reference

Idioms and standard-library tricks worth remembering.

Comprehensions

squares = [x**2 for x in range(10) if x % 2 == 0]
lookup = {k: v for k, v in pairs}

Useful stdlib

from collections import defaultdict, Counter
from functools import lru_cache, partial
from itertools import groupby, chain

@lru_cache(maxsize=None)
def fib(n): ...

Context managers

from contextlib import contextmanager

@contextmanager
def timer():
    start = time.perf_counter()
    yield
    print(time.perf_counter() - start)

Type hints that pull their weight

from typing import TypedDict

class Config(TypedDict):
    host: str
    port: int