Python Cheat Sheet — 1-page PDF
Core syntax, data structures, control flow, functions, OOP, exceptions, I/O, built-ins. The whole course on one dense, printable page for your midterm or final, with every formula typeset and worked examples where they help.
The first part is free below. See pricing →
For a specific exam: Intro to Programming (Python) Final exam study sheet.
- 1 pageUS Letter
- 10 sections10 topics
- Free previewfull sheet with Unlimited
Cheat sheet locked
On the sheet
What’s on it
The topics a typical Python Programming exam tests, all on the one page.
- Data types and operators
- Strings and f-strings
- Lists, tuples, dicts, sets
- Comprehensions
- Control flow
- Functions, args, kwargs
- Classes and dunder methods
- Exceptions
- File I/O
- Common built-ins and standard library
Free preview
The free part, as text
The free part of the sheet, section by section. Unlock the full sheet to read the rest, edit any block, and print it or save it as a PDF.
01Types & Ops
- Dynamic typing
- Names bind to objects; a variable’s type = object type, not name type.
- Core types:
int,float,bool,str,NoneType,list,tuple,dict,set - Truthiness:
False,None,0,0.0,'',[],(),{},set()are falsy ==tests value;istests identity (same object)
Arithmetic
/ true div; // floor div; % remainder; ** exponent.
Comparisons
Chained: a < b < c ≡ a < b and b < c.
Logic
Short-circuit eval; returns last evaluated operand, not always bool.
Membership
Tests containment in strings, sequences, sets, dict keys.
Bitwise
&, |, ^, ~, <<, >>
On integers; often distinct from logical ops.
Watch out:== None / != None → prefer is None / is not None. and/or precedence lower than comparisons.
02Collections
- List ops:
append,extend,insert,pop,remove,sort,reverse,copy - Tuple ops: packing/unpacking; single-item tuple needs trailing comma:
(1,) - Dict ops:
d[k],get,keys,values,items,update,pop,popitem - Set ops:
add,update,remove,discard,union,intersection,difference
Unpacking
Right side iterable length must match targets unless using starred target a, *mid, b.
Hashability
| Can be key/member? | Examples |
|---|---|
| Yes | int, str, tuple of hashables |
| No | list, dict, set |
Watch out:d[k] raises KeyError if absent; d.get(k, default) is safe. dict preserves insertion order; set does not guarantee order.
03Comprehensions
- Comprehension
- Compact syntax to build a collection from an iterable.
List comp
[expr for x in iterable if cond]
if filters items; multiple for clauses nest left-to-right.
04Functions
Parameters
positional-only, positional-or-keywordargs, keyword-onlykwargs
Defaults may be given for positional-or-keyword or keyword-only params; keyword-only come after *args or a bare *.
Return
Multiple values returned as a tuple.
*args- Extra positional args packed into a tuple.
**kwargs- Extra keyword args packed into a dict.
Call syntax
*seq unpacks sequence; **dict unpacks mapping.
- Scope: local inside function; global module scope
- Use
global xto rebind module variable;nonlocal xfor enclosing function variable - Default args evaluated once at definition time
Watch out:Mutable defaults trap: def f(x, lst=[]): ... reuses same list. Use None sentinel, then create inside.
05Classes
- Class
- Blueprint for objects; attributes hold state; methods operate on instances.
06Exceptions
Custom error
class MyError(Exception):
passWatch out:Catch specific exceptions first. Avoid bare except: unless you truly need to catch everything (incl. KeyboardInterrupt).
07File I/O
- Context manager
with open(path, mode, encoding='utf-8') as f:closes file automatically.
Open modes
r read; w overwrite; a append; x create; b binary; + update.
- Text I/O:
read(),readline(),readlines() - Write:
write(s),writelines(seq) - Iterate line-by-line:
for line in f: - Use
encoding='utf-8'unless specified otherwise
Path
from pathlib import Path
Use Path('dir') / 'file.txt' for path joining.
Ex.Write list lines to file
with open('out.txt','w',encoding='utf-8') as f:\n f.writelines(lines)
Watch out:read() loads whole file into memory; for large files prefer iteration or chunked reads.
Cheat sheet locked
Also on the full Python Programming sheet
8 more sections, with every definition, formula and worked example. Unlimited unlocks every full sheet in the library to edit, print or save as a PDF, and makes unlimited sheets from your own notes.
Strings
- String
- Common methods
- f-string
- Formatting mini-table
Collections
- List
- Tuple
- Dict
- Set
Comprehensions
- Set comp
- Dict comp
- Generator expr
Control Flow
- If
- For
- While
- Loop control
- Range
Functions
- Function
Classes
- Instance method
- Constructor
- Common dunders
Exceptions
- Exception
- Raise
- Try/except
Built-ins
- Common built-ins
- Stdlib picks
- Useful stdlib
FAQ
Questions
About the Python Programming sheet and making your own.
- What does this Python Programming cheat sheet cover?
- It has 10 sections: Types & Ops, Strings, Collections, Comprehensions, Control Flow, Functions, Classes, Exceptions, File I/O and Built-ins. Together they cover the topics a typical Python Programming exam tests, including data types and operators, strings and f-strings, lists, tuples, dicts, sets and comprehensions.
- Is the python cheat sheet free?
- The first part is free to read on this page, with no account needed. The full sheet opens in the app with Unlimited ($4.99 a month or $19 for a semester), where you can edit it, print it or save it as a PDF. It is one US Letter page and prints cleanly in black and white or colour.
- Can I bring this Python Programming cheat sheet to my exam?
- Only if your course allows a note sheet, so check the syllabus. This one is a single US Letter page. If your exam allows a different size, such as a 3x5 or 4x6 index card or one page front and back, Cheat Sheet AI makes one at exactly that size from your own lecture notes.
- How accurate is it?
- The sheet was written by our engine from a standard course outline, then a second model fact-checked every formula and statement against it; it corrected 2 statements before publishing. Notation and emphasis vary between courses, so check it against your own lecture notes.
- Can I make a Python Programming sheet from my own notes?
- Yes. Upload your lecture slides, notes or study guide and Cheat Sheet AI writes a sheet in your course's notation, sized to exactly the space your exam allows. Your first sheets are free.
Keep going
More sheets
All 40 sheets are in the sheet library.
Your course, your notes, one page.
Upload your own Python Programming slides or notes and get a study sheet in your course’s notation. Your first sheet needs no account, and a free account adds one more. See pricing