Python Programming: A Complete Reference Guide
This reference covers core Python concepts from beginner syntax to intermediate patterns
used in production codebases. Each section is self-contained and designed for fast lookup.
1. Data Types and Variables
Python is dynamically typed. Variables are labels pointing to objects in memory, not
containers.
Core types: int, float, complex, str, bool, list, tuple, dict, set, frozenset, NoneType. Everything
in Python is an object, including functions and classes.
Type coercion is explicit: int('42') works, int('hello') raises ValueError. Use isinstance(x, int) for
type checking, not type(x) == int, because the latter breaks with inheritance.
2. String Operations
String formatting: f-strings (f'Hello {name}') are the modern standard. format() works but is
verbose. % formatting is legacy—avoid in new code.
Useful string methods: strip(), split(), join(), replace(), startswith(), endswith(), upper(), lower(),
title(), zfill(), center(). Strings are immutable; all methods return new objects.
Multiline strings use triple quotes. Raw strings (r'path\to\file') suppress backslash
interpretation. Byte strings (b'data') are for binary data, not text.
3. List Comprehensions and Generators
List comprehension: [x*2 for x in range(10) if x % 2 == 0] — creates a full list in memory. Dict
comprehension: {k: v for k, v in items}. Set comprehension: {x for x in data}.
Generator expression: (x*2 for x in range(10000)) — lazy evaluation, no memory allocation
until iterated. Use when you only need to iterate once or the dataset is large.
yield turns a function into a generator. yield from delegates to a sub-generator. Generators
are the backbone of Python's memory efficiency for large data pipelines.
4. Error Handling
try/except/else/finally: the else block runs only when no exception occurred. finally always
runs. Never use bare except: without specifying an exception type—it catches SystemExit and
KeyboardInterrupt, masking critical signals.
Custom exceptions: inherit from Exception (not BaseException). Add context with raise
CustomError('message') from original_error to preserve the exception chain for debugging.
Context managers (with statement) guarantee cleanup. Implement __enter__ and __exit__ or
use @contextmanager from contextlib for function-based context managers.
5. Functions and Closures
*args collects positional arguments into a tuple. **kwargs collects keyword arguments into a
dict. Keyword-only arguments come after *: def f(a, *, b). Positional-only arguments come
before /: def f(a, b, /, c).
Closures capture variables from the enclosing scope. The cell variable in a loop closure is a
common gotcha: use default argument (lambda i=i: i) to capture the current value, not the loop
variable reference.
Decorators are functions that wrap other functions. [Link] preserves the wrapped
function's metadata. Stacked decorators apply bottom-up.
6. Object-Oriented Programming
__init__ is the initializer, not the constructor (__new__ is). __repr__ should return a string that
could recreate the object. __str__ is for human-readable output. __eq__, __hash__, __lt__
enable comparison and sorting.
@property turns a method into a read-only attribute. @setter adds write access.
@classmethod takes the class as first arg (cls). @staticmethod takes no implicit first
argument.
dataclasses (@dataclass) auto-generate __init__, __repr__, __eq__. Use frozen=True for
immutability. slots=True reduces memory footprint. They are not a replacement for full OOP
but excellent for data containers.
7. Common Standard Library Modules
Module Primary Use
os / pathlib File system operations
sys Interpreter access, argv, exit
json JSON serialization/deserialization
datetime Date and time manipulation
collections deque, Counter, defaultdict, namedtuple
itertools Efficient iteration combinatorics
functools lru_cache, partial, reduce
re Regular expressions
threading / multiprocessing Concurrency and parallelism
logging Production-grade logging
unittest / pytest Testing frameworks
8. Performance Tips
Use local variable references inside tight loops (local lookup is faster than global). Prefer join
over string concatenation in loops. Use slots in classes with many instances. Profile before
optimizing—cProfile and line_profiler are the standard tools.
NumPy vectorization beats Python loops by 10-100x for numerical work. Pandas apply() is
slow for large datasets—prefer vectorized column operations. For I/O-bound workloads,
asyncio outperforms threading with lower overhead.