This python cheat sheet follows the data structures chapter of the official Python tutorial and adds the control flow you need to put them to use. [1] It targets Python 3. Search for a method name such as append or items, or pick a topic to see one group at a time.
How to use the python cheat sheet
- Type in the search box to filter by any word in a name, a command or its note.
- Choose a topic to see one group instead of the whole list.
- Read the command in the right-hand column and its meaning on the left.
- Press Print for a clean copy of whatever is shown.
What is in each topic
The python cheat sheet is split into 5 topics. The table shows how many entries each topic holds and which ones they are, so you can jump straight to the right group with the topic box.
| Topic | Entries | What it covers |
|---|---|---|
| Lists | 7 | add to the end; add many; insert; remove by value; remove by index; sort in place; slice |
| Dictionaries | 6 | create; read a value; safe read; loop over pairs; list the keys; sorted keys |
| Sets and tuples | 6 | make a set; empty set; union; intersection; difference; unpack a tuple |
| Comprehensions | 4 | list comprehension; with a filter; dict comprehension; set comprehension |
| Control flow and functions | 6 | conditional; for loop; while loop; define a function; return a value; import |
Lists
append(x) adds an item to the end, extend(iterable) adds every item from an iterable, and insert(i, x) inserts before index i. remove(x) removes the first item equal to x and raises ValueError if there is none, while pop(i) removes and returns the item at i, the last one by default. [1] sort(key=None, reverse=False) sorts in place and returns None, so write a.sort(), not a = a.sort(). [1]
Slicing, such as a[1:3], returns items from index 1 up to but not including 3. Negative indexes count from the end. Because lists change in place, copy one with a[:] if you need the original afterwards.
Dictionaries, sets and tuples
Reading d[key] raises KeyError if the key is missing, so d.get(key, default) is safer: it returns the default, or None, instead. [1] Loop over pairs with for k, v in d.items(), and list(d) returns the keys in insertion order while sorted(d) returns them sorted. [1]
set(iterable) removes duplicates, and set() makes an empty set because {} makes an empty dict. The operators | & - and ^ give the union, intersection, difference and symmetric difference. [1] A tuple is an immutable sequence you can unpack with x, y, z = t. [1]
Comprehensions, flow and functions
A list comprehension builds a list from an expression and a for clause with an optional if, for example [x**2 for x in range(5)] gives [0, 1, 4, 9, 16]. [1] Set and dict comprehensions use curly brackets in the same way, such as {x: x**2 for x in (2, 4, 6)}. [1]
Python marks blocks by indentation. Write if, elif and else for choices, for i in range(5) to repeat a fixed number of times and while to repeat until a condition fails. Functions use def name(a, b=1), where b has a default, and return ends the function with a value. The control flow lines are listed from our own working.
Who this page helps
Developers use a cheat sheet to look up a command they have not typed for a while, to learn the names of the options and to compare similar commands. Beginners can read down a topic to see what is possible, and experienced users can search for the one flag they forgot.
Tips for getting the most from the python cheat sheet
- Use get on dictionaries when a key may be missing, and a comprehension when you build a list from another.
- Remember that sort changes a list in place and returns None, while sorted returns a new list.
- Test small snippets in the interactive prompt before you paste them into a script.
Common mistakes
Most problems come from running a command in the wrong place or with the wrong target. Read a command before you press Enter, replace every placeholder with your own value, and try destructive commands on a test copy first. When a command behaves differently from the note, check the version you have installed and the official documentation.
Where the entries come from
Each command was checked against the documentation listed below this article. Where a command is standard but is not shown on the page we read, the article says it is our own working. Options and defaults change between releases, so run the help command for the version you have installed before you rely on a flag.
What this page does not do
It lists the commands most people need and does not try to cover every option. For the complete syntax, error messages and advanced features, open the official documentation linked in the sources, and keep it beside the cheat sheet while you learn.
Printing and checking
Filter by topic before you print if you only need one section, and the printed page will be shorter. The notes are short on purpose, so keep your textbook or the official documentation open for the full detail and for anything that changes between versions.
What next
Use this often? Add Python Cheat to your Home screen.
Frequently asked questions
What does this python cheat sheet include?
List, dictionary and set methods, tuples, comprehensions, conditionals, loops, functions and imports.
What is the difference between append and extend?
append adds one item to the end of a list. extend adds every item from an iterable, so extending with a list of three adds three items.
Why does sort return None?
list.sort sorts in place and returns None to show that it changed the list. Use sorted(a) to get a new sorted list.
How do I make an empty set?
Use set(). Curly brackets with nothing inside make an empty dictionary.
Was this tool helpful?
What went wrong?
We only receive the reason you pick, never what you typed into the tool.
Thanks for your feedback.