PythonMastery
Cheat sheets

Python cheat sheets: free one-page PDFs

15 topics, each on a single A4 page you can print, pin above your desk or keep open next to your editor. 904 examples in all, and every one is run against Python 3.13 before a sheet is published, so the result printed next to it is the result you'll get. Each sheet ends with the mistakes people actually make.

Preview of the Python strings cheat sheet

Python strings

Strings never change in place: every method returns a new string, so keep the result (s = s.strip()).

One A4 page · 79 examples · PDF, 220 KB
Preview of the Python lists cheat sheet

Python lists

Lists change in place: append(), sort() and friends edit the list and return None, so don't assign their result.

One A4 page · 67 examples · PDF, 185 KB
Preview of the Python tuples cheat sheet

Python tuples

A tuple is a list that can't change, which is what lets it be a dict key.

One A4 page · 64 examples · PDF, 202 KB
Preview of the Python dictionaries cheat sheet

Python dictionaries

A dict maps unique, hashable keys to values: lookup by key is fast, and keys stay in the order you added them.

One A4 page · 56 examples · PDF, 206 KB
Preview of the Python sets cheat sheet

Python sets

A set holds each value once, in no particular order, and answers x in s instantly.

One A4 page · 62 examples · PDF, 205 KB
Preview of the Python slicing cheat sheet

Python slicing

seq[start:stop:step] makes a new sequence from a run of items: start is included, stop is excluded, and any of the three can be left out.

One A4 page · 49 examples · PDF, 219 KB
Preview of the Python comprehensions cheat sheet

Python comprehensions

A comprehension is a for loop that builds a new collection: read [n * 2 for n in nums if n > 0] as "for each n in nums, if n > 0, keep n * 2".

One A4 page · 49 examples · PDF, 208 KB
Preview of the Python f-strings cheat sheet

Python f-strings

Anything in {} is run as Python and turned into text; after a colon comes the format spec, {value:fill align sign width , .precision type}.

One A4 page · 70 examples · PDF, 238 KB
Preview of the Python files and paths cheat sheet

Python files and paths

A Path is only a name until you read or write it, and text files should always be opened with encoding="utf-8".

One A4 page · 63 examples · PDF, 234 KB
Preview of the Python exceptions cheat sheet

Python exceptions

An error travels up through the calls until an except that matches its class catches it, so catch the narrowest class you can actually handle.

One A4 page · 54 examples · PDF, 231 KB
Preview of the Python dates and times cheat sheet

Python dates and times

A date is a day, a datetime is a day plus a clock time, and a timedelta is a length of time: subtract two datetimes and you get one.

One A4 page · 67 examples · PDF, 209 KB
Preview of the Python regular expressions cheat sheet

Python regular expressions

A pattern describes text; re finds it, splits on it or replaces it.

One A4 page · 58 examples · PDF, 225 KB
Preview of the Python itertools cheat sheet

Python itertools

Every tool here is lazy: it hands out one item at a time and is used up after one pass, so the examples wrap it in list() to show what it yields.

One A4 page · 54 examples · PDF, 199 KB
Preview of the Python type hints cheat sheet

Python type hints

Hints are labels for readers and tools: Python stores them but never checks them, a checker such as mypy or pyright does.

One A4 page · 53 examples · PDF, 206 KB
Preview of the Python argparse cheat sheet

Python argparse

argparse turns the words typed after your script's name into an object of typed values, and writes the help and error messages for you.

One A4 page · 59 examples · PDF, 212 KB

Free to share, not to sell. Print them for yourself, your class or your team, and share the files as they are. Each sheet links to its runnable version on this site, where you can try every example in the browser.