# Stas' Python Cookbook
An open book of practical, example-driven recipes for the Python standard library and the idioms that come up again and again in real work — text wrangling, data structures, files and processes, debugging, profiling, testing, and packaging.
This book grew out of a personal cheat sheet. Every section leads with the *why*, then shows the *how* with runnable snippets, and calls out the gotchas that bite people in practice. It targets **Python 3.8+**, and notes version specifics where they matter.
This is based on the python cheat sheet that I have been honing since many years and still refer to it daily. I then used Claude to convert it to more readable prose, that is hopefully more enjoyable to read. If you prefer the raw/dense cheat sheet you can find it [here](python.txt).
## Table of Contents
The full cookbook is in **[content/](./content/)**, organized into four parts:
1. **[Part I: Language Core](./content/README.md#part-i-language-core)** - strings and text, formatting, numbers, regex, lists, tuples, sets, dictionaries, comprehensions/itertools, functions, classes, dates and times.
2. **[Part II: Runtime and Environment](./content/README.md#part-ii-runtime-and-environment)** - modules and imports, files/paths/IO, environment and arguments, subprocess, serialization, concurrency, networking.
3. **[Part III: Debugging, Profiling and Testing](./content/README.md#part-iii-debugging-profiling-and-testing)** - output and logging, debugging, introspection, profiling, exceptions, testing.
4. **[Part IV: Packaging and Tooling](./content/README.md#part-iv-packaging-and-tooling)** - versions and dependencies, packaging, code quality, scaling pointers, and an appendix of one-liners.
## Updates
I announce any significant updates on my twitter channel [https://twitter.com/StasBekman](https://twitter.com/StasBekman).
## Ebook versions of the book
You can download various ebook formats of this book:
* [PDF](https://huggingface.co/stas/python-cookbook/resolve/main/Stas%20Bekman%20-%20Stas%27%20Python%20Cookbook.pdf?download=true)
* [EPUB](https://huggingface.co/stas/python-cookbook/resolve/main/Stas%20Bekman%20-%20Stas%27%20Python%20Cookbook.epub?download=true)
I will try to rebuild these once in a few weeks or so, but if you want the latest ebook versions, the instructions for building are [here](build).
## SKILL.md for AI agents
I maintain a [SKILL.md](./SKILL.md) file that you can use to teach your AI agent everyday Python and standard-library idioms better.
See also the companion skills: [Machine Learning Engineering](https://github.com/stas00/ml-engineering/blob/master/SKILL.md) and [The Art of Debugging](https://github.com/stas00/the-art-of-debugging/blob/master/SKILL.md).
## Related Open Books
- [Machine Learning Engineering Open Book](https://github.com/stas00/ml-engineering) — training and operating large-scale ML models.
- [The Art of Debugging Open Book](https://github.com/stas00/the-art-of-debugging) — methodologies and recipes for debugging Unix, Python and PyTorch programs.
## Contributing
If you found a bug, typo or would like to propose an improvement please don't hesitate to open an [Issue](https://github.com/stas00/python-cookbook/issues) or contribute a PR.
## License
The content of this site is distributed under [Attribution-ShareAlike 4.0 International](./LICENSE-CC-BY-SA).
## My repositories map
✔ **Books:** [Machine Learning Engineering](https://github.com/stas00/ml-engineering) | [The Art of Debugging](https://github.com/stas00/the-art-of-debugging) | [Stas' Python Cookbook](https://github.com/stas00/python-cookbook)
✔ **Applications:** [ipyexperiments](https://github.com/stas00/ipyexperiments)
✔ **Tools and Cheatsheets:** [bash](https://github.com/stas00/bash-tools) | [conda](https://github.com/stas00/conda-tools) | [git](https://github.com/stas00/git-tools) | [jupyter-notebook](https://github.com/stas00/jupyter-notebook-tools) | [make](https://github.com/stas00/make-tools) | [python](https://github.com/stas00/python-tools) | [tensorboard](https://github.com/stas00/tensorboard-tools) | [unix](https://github.com/stas00/unix-tools)
✔ **Other Machine Learning:** [ML ways](https://github.com/stas00/ml-ways) | [Porting](https://github.com/stas00/porting)