Quick Reference · general-purpose programming language

python cheat sheet

Everything in Python is a name bound to an object. Learn how names are looked up, how objects differ in mutability, and how functions/classes package behavior — and the syntax stops being 300 things to memorize.

core syntax & types data structures control & functions oop & advanced gotcha most common

Distilled & cross-checked across: docs.python.org/3 · realpython.com · PEP 572 / 634 · w3schools.com/python · geeksforgeeks.org/python · python.org/downloads

Name resolution — where Python looks up a name (LEGB)
Local inside the current function params, loop vars, x = 1 assignment ⇒ local by default Enclosing outer function's scope closures capture this Global top level of the module import, module constants Built-in the __builtins__ module len, range, print, str… not found → not found → not found → nonlocal x global x (forces assignment into module scope) YOUR FUNCTION MODULE & PYTHON ITSELF
01Run & Environmentstart here
02Variables & Assignmentnames → objects
03Numbers & Operatorsarithmetic & logic
04Strings & f-stringsimmutable text
05Listsordered, mutable
06Tuples & Setsimmutable / unique
07Dictionarieskey → value
08Control Flowbranching
09Loopsiteration
10Functionsreusable behavior
11Comprehensions & Generatorsbuild collections concisely
12OOP Basicsclasses & objects
13Dataclassesless boilerplate
14Decorators & Context Managerswrap behavior
15Exception Handlingfail gracefully
16File I/O & Pathlibread/write
17Modules, Packages & Envsorganize & isolate
18Standard Library Highlightsbatteries included
19Type Hintsfor tooling, not runtime
20Common Pitfallshandle with care
Slice Notationseq[start:stop:step]

Objects, references & evaluation

Same-looking code, four different runtime behaviors — where most Python bugs actually come from.

aliasing a mutable object

b = a doesn't copy the list — both names point at the same object, so mutating through either one is visible from both.

a b [1, 2, 3, 4] one object · two names · b.append(4) changes both

shallow vs. deep copy

copy() duplicates the outer container only; nested lists stay shared. deepcopy() recursively duplicates everything.

original shallow copy shared inner [ ] deepcopy's own [ ]

eager list vs. lazy generator

A list comprehension computes every value immediately. A generator expression computes one value at a time, only when asked.

[x*x for x …] (x*x for x …) all in memory one at a time

decorator call flow

@decorator replaces the function with a wrapper. The wrapper decides if/when the original actually runs.

caller wrapper() func() ★ return

Worth memorizing

is ≠ ==is checks identity; == checks value equality
mutable defaultsevaluated once at def time — use None as a sentinel
copy ≠ deepcopycopy() is one level deep; nested mutables still shared
list * n[[0]*3]*3 repeats one inner list, not three
global vs nonlocalglobal targets module scope; nonlocal targets the enclosing function
param orderpositional-only → normal → *args → keyword-only → **kwargs
f-stringsevaluated at runtime — fastest & most readable interpolation
__new__ vs __init____new__ creates the instance; __init__ only initializes it