1---2name: python-cookbook3description: Python Cookbook4---56# Stas' Python Cookbook78> Distilled from **Stas' Python Cookbook** open book by Stas Bekman - source: https://github.com/stas00/python-cookbook (CC BY-SA 4.0). This skill is a condensed index; each section links back to the full chapter for depth, runnable snippets, and gotchas.910A practical, standard-library-first reference for the Python idioms that come up again and again in real work. It leads with the *why*, shows the *how* with copy-paste snippets, and calls out the gotchas that bite in practice. Targets **Python 3.8+**; version-specific features are flagged inline. For deep single-process/tool debugging (gdb, strace, py-spy, core files, CUDA) pair this with [The Art of Debugging](https://github.com/stas00/the-art-of-debugging/blob/master/SKILL.md).1112## Core principles1314- **Reach for the standard library first.** A third-party package is only suggested when the stdlib genuinely falls short - and it's flagged with `pip install`.15- **Prefer the modern idiom.** f-strings over `%`/`.format()`, `pathlib` over `os.path`, `subprocess.run` over `os.system`, dataclasses over ad-hoc tuples, `logging` over `print` in real programs.16- **Know the gotcha before it bites.** Mutable default arguments, shallow vs deep copy, pass-by-object-reference, naive vs aware datetimes, the GIL - each chapter flags the trap.17- **Read the linked section before applying a recipe** - each has worked examples, caveats, and edge cases the one-liner here omits.1819## Part I - Language core2021Full chapters: [Strings](https://github.com/stas00/python-cookbook/blob/master/content/README.md#strings-and-text) · [Formatting](https://github.com/stas00/python-cookbook/blob/master/content/README.md#string-formatting) · [Numbers](https://github.com/stas00/python-cookbook/blob/master/content/README.md#numbers-and-math) · [Regex](https://github.com/stas00/python-cookbook/blob/master/content/README.md#regular-expressions).2223- **Text wrangling:** case/containment/split/join/strip/replace, encoding-decoding, and a [worked text-normalization example](https://github.com/stas00/python-cookbook/blob/master/content/README.md#regular-expressions). Compare strings with a [readable diff](https://github.com/stas00/python-cookbook/blob/master/content/README.md#strings-and-text). See [Strings and Text](https://github.com/stas00/python-cookbook/blob/master/content/README.md#strings-and-text).24- **Formatting:** the [format spec mini-language](https://github.com/stas00/python-cookbook/blob/master/content/README.md#string-formatting) (alignment, padding, precision, `,`/`_` grouping), [f-string tricks](https://github.com/stas00/python-cookbook/blob/master/content/README.md#string-formatting) (`=` debug, nested specs), and number/byte humanization. See [String Formatting](https://github.com/stas00/python-cookbook/blob/master/content/README.md#string-formatting).25- **Numbers/math:** rounding traps, `decimal`/`fractions`, [statistics without NumPy](https://github.com/stas00/python-cookbook/blob/master/content/README.md#numbers-and-math), and special values (`nan`/`inf`). See [Numbers and Math](https://github.com/stas00/python-cookbook/blob/master/content/README.md#numbers-and-math).26- **Regex:** the [core functions](https://github.com/stas00/python-cookbook/blob/master/content/README.md#regular-expressions), flags, groups/assertions, substitution with backreferences/callables, splitting, and escaping literals. See [Regular Expressions](https://github.com/stas00/python-cookbook/blob/master/content/README.md#regular-expressions).2728## Part I - Data structures2930Full chapters: [Lists](https://github.com/stas00/python-cookbook/blob/master/content/README.md#lists) · [Tuples](https://github.com/stas00/python-cookbook/blob/master/content/README.md#tuples) · [Sets](https://github.com/stas00/python-cookbook/blob/master/content/README.md#sets) · [Dictionaries](https://github.com/stas00/python-cookbook/blob/master/content/README.md#dictionaries) · [Comprehensions/itertools](https://github.com/stas00/python-cookbook/blob/master/content/README.md#comprehensions-iterators-and-itertools).3132- **Lists:** add/remove, [shallow vs deep copy](https://github.com/stas00/python-cookbook/blob/master/content/README.md#lists) (a classic bug source), slicing/splicing, searching/aggregating, chunking, and ranges. See [Lists](https://github.com/stas00/python-cookbook/blob/master/content/README.md#lists).33- **Tuples & sets:** immutability and namedtuples ([Tuples](https://github.com/stas00/python-cookbook/blob/master/content/README.md#tuples)); set algebra and mutation ([Sets](https://github.com/stas00/python-cookbook/blob/master/content/README.md#sets)).34- **Dictionaries:** construction/merging (`|`), [access with defaults](https://github.com/stas00/python-cookbook/blob/master/content/README.md#dictionaries), [`defaultdict`](https://github.com/stas00/python-cookbook/blob/master/content/README.md#dictionaries), [`Counter`](https://github.com/stas00/python-cookbook/blob/master/content/README.md#dictionaries), transformations, and [dataclasses for structured records](https://github.com/stas00/python-cookbook/blob/master/content/README.md#dictionaries). See [Dictionaries](https://github.com/stas00/python-cookbook/blob/master/content/README.md#dictionaries).35- **Comprehensions & iterators:** list/dict/set/generator forms, [the essential itertools](https://github.com/stas00/python-cookbook/blob/master/content/README.md#comprehensions-iterators-and-itertools), flatten/zip/unzip, and [generators with `yield`](https://github.com/stas00/python-cookbook/blob/master/content/README.md#comprehensions-iterators-and-itertools) for streaming. See [Comprehensions, Iterators and itertools](https://github.com/stas00/python-cookbook/blob/master/content/README.md#comprehensions-iterators-and-itertools).3637## Part I - Functions, classes, time3839Full chapters: [Functions](https://github.com/stas00/python-cookbook/blob/master/content/README.md#functions) · [Classes](https://github.com/stas00/python-cookbook/blob/master/content/README.md#classes-and-objects) · [Dates](https://github.com/stas00/python-cookbook/blob/master/content/README.md#dates-and-times).4041- **Functions:** `*args`/`**kwargs`, the [mutable-default-argument trap](https://github.com/stas00/python-cookbook/blob/master/content/README.md#functions), [pass-by-object-reference](https://github.com/stas00/python-cookbook/blob/master/content/README.md#functions), closures, `functools` (partial/`lru_cache`/reduce), and [decorators](https://github.com/stas00/python-cookbook/blob/master/content/README.md#functions). See [Functions](https://github.com/stas00/python-cookbook/blob/master/content/README.md#functions).42- **Classes:** dunder methods, inheritance, dynamic attributes/delegation, [importing a class from a string](https://github.com/stas00/python-cookbook/blob/master/content/README.md#classes-and-objects), and [context managers](https://github.com/stas00/python-cookbook/blob/master/content/README.md#classes-and-objects). See [Classes and Objects](https://github.com/stas00/python-cookbook/blob/master/content/README.md#classes-and-objects).43- **Dates/times:** parsing/formatting, [always use timezone-aware datetimes](https://github.com/stas00/python-cookbook/blob/master/content/README.md#dates-and-times), durations, and measuring elapsed time. See [Dates and Times](https://github.com/stas00/python-cookbook/blob/master/content/README.md#dates-and-times).4445## Part II - Runtime & environment4647Full chapters: [Modules](https://github.com/stas00/python-cookbook/blob/master/content/README.md#modules-and-imports) · [Files/IO](https://github.com/stas00/python-cookbook/blob/master/content/README.md#files-paths-and-io) · [Env & args](https://github.com/stas00/python-cookbook/blob/master/content/README.md#environment-variables-and-program-arguments) · [Subprocess](https://github.com/stas00/python-cookbook/blob/master/content/README.md#subprocess-and-shell-integration).4849- **Modules/imports:** how [`sys.path`](https://github.com/stas00/python-cookbook/blob/master/content/README.md#modules-and-imports) resolves imports, inspecting/reloading modules, and dynamic import. See [Modules and Imports](https://github.com/stas00/python-cookbook/blob/master/content/README.md#modules-and-imports).50- **Files/paths/IO:** [`pathlib` basics](https://github.com/stas00/python-cookbook/blob/master/content/README.md#files-paths-and-io), metadata, create/move/delete, globbing, [reading/writing](https://github.com/stas00/python-cookbook/blob/master/content/README.md#files-paths-and-io), and [temp files/dirs](https://github.com/stas00/python-cookbook/blob/master/content/README.md#files-paths-and-io). See [Files, Paths and I/O](https://github.com/stas00/python-cookbook/blob/master/content/README.md#files-paths-and-io).51- **Env vars & CLI:** reading [environment variables](https://github.com/stas00/python-cookbook/blob/master/content/README.md#environment-variables-and-program-arguments) safely, [`argparse`](https://github.com/stas00/python-cookbook/blob/master/content/README.md#environment-variables-and-program-arguments), and replaying the exact command line. See [Environment Variables and Program Arguments](https://github.com/stas00/python-cookbook/blob/master/content/README.md#environment-variables-and-program-arguments).52- **Subprocess:** run external commands with `subprocess.run` (capture output, check errors, avoid `shell=True` pitfalls) instead of `os.system`. See [Subprocess and Shell Integration](https://github.com/stas00/python-cookbook/blob/master/content/README.md#subprocess-and-shell-integration).5354## Part II - Data, concurrency, network5556Full chapters: [Serialization](https://github.com/stas00/python-cookbook/blob/master/content/README.md#serialization-json-csv-pickle-xml-gzip) · [Concurrency](https://github.com/stas00/python-cookbook/blob/master/content/README.md#concurrency-processes-threads-and-the-gil) · [Networking](https://github.com/stas00/python-cookbook/blob/master/content/README.md#networking).5758- **Serialization:** [JSON](https://github.com/stas00/python-cookbook/blob/master/content/README.md#serialization-json-csv-pickle-xml-gzip) (custom encoders, streaming), CSV, pickle (and its security caveat), gzip, [memory-safe streaming XML](https://github.com/stas00/python-cookbook/blob/master/content/README.md#serialization-json-csv-pickle-xml-gzip), and archives. See [Serialization](https://github.com/stas00/python-cookbook/blob/master/content/README.md#serialization-json-csv-pickle-xml-gzip).59- **Concurrency:** the [GIL](https://github.com/stas00/python-cookbook/blob/master/content/README.md#concurrency-processes-threads-and-the-gil) and when to use processes vs threads, process pools, the unified [`concurrent.futures`](https://github.com/stas00/python-cookbook/blob/master/content/README.md#concurrency-processes-threads-and-the-gil) API, and background monitoring threads. See [Concurrency](https://github.com/stas00/python-cookbook/blob/master/content/README.md#concurrency-processes-threads-and-the-gil).60- **Networking:** sockets/ports and [HTTP with `requests`](https://github.com/stas00/python-cookbook/blob/master/content/README.md#networking). See [Networking](https://github.com/stas00/python-cookbook/blob/master/content/README.md#networking).6162## Part III - Debugging, profiling & testing6364Full chapters: [Logging](https://github.com/stas00/python-cookbook/blob/master/content/README.md#printing-logging-and-output-control) · [Debugging](https://github.com/stas00/python-cookbook/blob/master/content/README.md#debugging) · [Introspection](https://github.com/stas00/python-cookbook/blob/master/content/README.md#introspection-and-object-inspection) · [Profiling](https://github.com/stas00/python-cookbook/blob/master/content/README.md#profiling-cpu-and-memory) · [Exceptions](https://github.com/stas00/python-cookbook/blob/master/content/README.md#exceptions-and-error-handling) · [Testing](https://github.com/stas00/python-cookbook/blob/master/content/README.md#testing-with-pytest-and-unittest).6566- **Output control:** print to stderr, [unbuffered output](https://github.com/stas00/python-cookbook/blob/master/content/README.md#printing-logging-and-output-control), tee to console+file, [`logging` over `print`](https://github.com/stas00/python-cookbook/blob/master/content/README.md#printing-logging-and-output-control) for real programs, and `warnings`. See [Printing, Logging and Output Control](https://github.com/stas00/python-cookbook/blob/master/content/README.md#printing-logging-and-output-control).67- **Debugging:** the [interactive debugger](https://github.com/stas00/python-cookbook/blob/master/content/README.md#debugging) (`breakpoint()`/pdb), debugging forked/multiprocess code, [getting a traceback out of a stuck or crashed process](https://github.com/stas00/python-cookbook/blob/master/content/README.md#debugging), programmatic stack traces, and [tracing execution](https://github.com/stas00/python-cookbook/blob/master/content/README.md#debugging). See [Debugging](https://github.com/stas00/python-cookbook/blob/master/content/README.md#debugging).68- **Introspection:** [what is this thing?](https://github.com/stas00/python-cookbook/blob/master/content/README.md#introspection-and-object-inspection) (`type`/`dir`/`inspect`), dumping an object's attributes, and prettier dumps. See [Introspection and Object Inspection](https://github.com/stas00/python-cookbook/blob/master/content/README.md#introspection-and-object-inspection).69- **Profiling:** [`timeit`](https://github.com/stas00/python-cookbook/blob/master/content/README.md#profiling-cpu-and-memory) for micro-benchmarks, [`cProfile`](https://github.com/stas00/python-cookbook/blob/master/content/README.md#profiling-cpu-and-memory) for function-level CPU, `line_profiler` for per-line, [`tracemalloc`](https://github.com/stas00/python-cookbook/blob/master/content/README.md#profiling-cpu-and-memory) for memory, `psutil` for process RSS, and leak-finding. See [Profiling CPU and Memory](https://github.com/stas00/python-cookbook/blob/master/content/README.md#profiling-cpu-and-memory).70- **Exceptions:** raising/catching precisely, [re-raising and chaining (`from`)](https://github.com/stas00/python-cookbook/blob/master/content/README.md#exceptions-and-error-handling), custom exceptions, and good habits (never bare-`except`). See [Exceptions and Error Handling](https://github.com/stas00/python-cookbook/blob/master/content/README.md#exceptions-and-error-handling).71- **Testing:** [pytest basics](https://github.com/stas00/python-cookbook/blob/master/content/README.md#testing-with-pytest-and-unittest), fixtures, capturing output, [parallel/slow-test control](https://github.com/stas00/python-cookbook/blob/master/content/README.md#testing-with-pytest-and-unittest), and `unittest`. See [Testing with pytest and unittest](https://github.com/stas00/python-cookbook/blob/master/content/README.md#testing-with-pytest-and-unittest).7273## Part IV - Packaging & tooling7475Full chapters: [Versions/Deps](https://github.com/stas00/python-cookbook/blob/master/content/README.md#versions-and-dependencies) · [Packaging](https://github.com/stas00/python-cookbook/blob/master/content/README.md#packaging-and-requirements) · [Code quality](https://github.com/stas00/python-cookbook/blob/master/content/README.md#code-quality-and-formatting) · [Big data](https://github.com/stas00/python-cookbook/blob/master/content/README.md#big-data-and-scaling-pointers) · [Appendix](https://github.com/stas00/python-cookbook/blob/master/content/README.md#appendix-one-liners-and-resources).7677- **Versions/deps:** check the running Python version, query installed package versions, [compare versions correctly](https://github.com/stas00/python-cookbook/blob/master/content/README.md#versions-and-dependencies) (not string compare), and [virtual environments](https://github.com/stas00/python-cookbook/blob/master/content/README.md#versions-and-dependencies). See [Versions and Dependencies](https://github.com/stas00/python-cookbook/blob/master/content/README.md#versions-and-dependencies).78- **Packaging:** generate a requirements file from imports and [modern packaging with `pyproject.toml`](https://github.com/stas00/python-cookbook/blob/master/content/README.md#packaging-and-requirements). See [Packaging and Requirements](https://github.com/stas00/python-cookbook/blob/master/content/README.md#packaging-and-requirements).79- **Code quality:** formatters/linters (black/ruff) and type checking. See [Code Quality and Formatting](https://github.com/stas00/python-cookbook/blob/master/content/README.md#code-quality-and-formatting).80- **Scaling pointers & one-liners:** when to reach past the stdlib ([Big Data](https://github.com/stas00/python-cookbook/blob/master/content/README.md#big-data-and-scaling-pointers)) plus handy shell one-liners, recursion limits, and human-readable tables ([Appendix](https://github.com/stas00/python-cookbook/blob/master/content/README.md#appendix-one-liners-and-resources)).8182## Pick the recipe by need8384| Need | Reach for |85|---|---|86| Format a number/string cleanly | [format spec mini-language](https://github.com/stas00/python-cookbook/blob/master/content/README.md#string-formatting), [f-string tricks](https://github.com/stas00/python-cookbook/blob/master/content/README.md#string-formatting) |87| Match/extract/replace text | [Regular Expressions](https://github.com/stas00/python-cookbook/blob/master/content/README.md#regular-expressions) |88| Count / group / default values | [`Counter`](https://github.com/stas00/python-cookbook/blob/master/content/README.md#dictionaries), [`defaultdict`](https://github.com/stas00/python-cookbook/blob/master/content/README.md#dictionaries) |89| Structured record type | [dataclasses](https://github.com/stas00/python-cookbook/blob/master/content/README.md#dictionaries) |90| Stream/lazily process data | [generators & itertools](https://github.com/stas00/python-cookbook/blob/master/content/README.md#comprehensions-iterators-and-itertools) |91| Work with files/paths | [`pathlib`](https://github.com/stas00/python-cookbook/blob/master/content/README.md#files-paths-and-io) |92| Parse CLI arguments | [`argparse`](https://github.com/stas00/python-cookbook/blob/master/content/README.md#environment-variables-and-program-arguments) |93| Run an external command | [`subprocess.run`](https://github.com/stas00/python-cookbook/blob/master/content/README.md#subprocess-and-shell-integration) |94| Read/write JSON/CSV/gzip | [Serialization](https://github.com/stas00/python-cookbook/blob/master/content/README.md#serialization-json-csv-pickle-xml-gzip) |95| Parallelize work | [`concurrent.futures`](https://github.com/stas00/python-cookbook/blob/master/content/README.md#concurrency-processes-threads-and-the-gil) (mind the [GIL](https://github.com/stas00/python-cookbook/blob/master/content/README.md#concurrency-processes-threads-and-the-gil)) |96| Real logging (not print) | [`logging`](https://github.com/stas00/python-cookbook/blob/master/content/README.md#printing-logging-and-output-control) |97| Code is too slow | [`cProfile`/`timeit`/`line_profiler`](https://github.com/stas00/python-cookbook/blob/master/content/README.md#profiling-cpu-and-memory) |98| Memory keeps growing | [`tracemalloc`/`psutil`](https://github.com/stas00/python-cookbook/blob/master/content/README.md#profiling-cpu-and-memory) |99| A stuck/crashed process | [get a traceback out of it](https://github.com/stas00/python-cookbook/blob/master/content/README.md#debugging) |100| Write/run tests | [pytest & unittest](https://github.com/stas00/python-cookbook/blob/master/content/README.md#testing-with-pytest-and-unittest) |101| Compare version strings | [compare versions correctly](https://github.com/stas00/python-cookbook/blob/master/content/README.md#versions-and-dependencies) |102| Package/pin a project | [`pyproject.toml`](https://github.com/stas00/python-cookbook/blob/master/content/README.md#packaging-and-requirements), [requirements](https://github.com/stas00/python-cookbook/blob/master/content/README.md#packaging-and-requirements) |103104## Notes for AI agents105106- **Prefer the stdlib and the modern idiom** (f-strings, `pathlib`, `subprocess.run`, dataclasses, `logging`) unless the user's environment dictates otherwise; only add a dependency when the stdlib truly can't do it.107- **Watch the flagged gotchas** - mutable default args, shallow vs deep copy, pass-by-object-reference, naive datetimes, `shell=True`, bare `except`, string version comparison - before shipping a snippet.108- **Measure before optimizing:** profile with `cProfile`/`timeit`/`tracemalloc` rather than guessing which line is slow or leaky.109- **Read the linked chapter section** before applying an unfamiliar recipe - each has worked examples, caveats, and copy-paste code the index line omits.110- **Note the target version** (3.8+ baseline); guard newer-only features (`|` dict merge, `str.removeprefix`, `zoneinfo`, structural pattern matching) when portability matters.111- For deep runtime debugging of a crash/hang/segfault/OOM (gdb, strace, py-spy, core files, CUDA), use the companion skill: [The Art of Debugging](https://github.com/stas00/the-art-of-debugging/blob/master/SKILL.md).