Python interviews probe mutability (why sort vs sorted), names vs objects, the GIL (CPU-bound vs I/O), decorators, and generators. Be ready to explain is vs ==, default-argument traps, and what a list comprehension does. Web roles add WSGI/ASGI and SQL — still start from the language.
This Python cheat sheet is written for people searching for a fast, accurate reference: students, junior developers, and teams shipping production software. Pin it, copy the snippets, and come back when syntax slips.
Quick reference
- Mutable: lists, dicts, sets. Immutable: str, tuple, frozenset, numbers.
- is vs ==:
isidentity.==equality. Interned small ints are a trap in demos. - LEGB: Local, Enclosing, Global, Built-in.
global/nonlocalto assign outward. - Decorator: A callable that wraps a function.
@wrapsto keep__name__. - Generator:
yieldproduces lazy iterators.yield fromdelegates. - GIL: One bytecode thread at a time in CPython. I/O releases it; CPU-bound needs multiprocessing or native extensions.
- comprehension: Builds a collection. A generator expression
(x for x in xs)is lazy. - with: Context managers (
open, locks).__enter__/__exit__or@contextmanager.
Copy-paste examples
Decorator and a generator
Be ready to write both by hand. Mention functools.wraps unprompted if you can.
from functools import wraps
def once(fn):
seen = {'done': False, 'value': None}
@wraps(fn)
def wrapper(*args, **kwargs):
if not seen['done']:
seen['value'] = fn(*args, **kwargs)
seen['done'] = True
return seen['value']
return wrapper
def chunks(xs, n):
buf = []
for x in xs:
buf.append(x)
if len(buf) == n:
yield buf
buf = []
if buf:
yield bufCommon mistakes
- Saying the GIL means Python cannot do concurrency — threads still help I/O.
- Confusing shallow copy with deepcopy in an interview whiteboard.
- Writing
except:(bare) as the error-handling answer. - Cannot explain why
def f(x=[])is a bug.
FAQ
- list.sort vs sorted?
sortmutates and returns None.sortedreturns a new list. Interviewers watch whether you use the return value.
- What is a generator used for? Streaming large data without loading it all. Pipelines of iterators.
rangein Python 3 is this idea.
- How do you speed up Python? Profile first. Then algorithms, PyPy/C extensions, numpy, or move hot loops off the GIL. Not “rewrite in Java” as the first answer.
Related Python cheat sheets
Build with this stack
When a cheat sheet is not enough — you need a production app, a student FYP, or a custom dashboard — ArpaNeuro builds custom web development and also sells ready-made source code. Request a quote and tell us the stack.
Browse software development services or the source code marketplace if you want a working codebase instead of starting from a blank file.