Skip to main content

Menu

LEVEL 0
0/5 XP
HomeAboutTopicsPricingMy VaultStatsPractice TestsCertifications

Categories

πŸŽ“ Certifications
πŸ€– Artificial Intelligence
☁️ Cloud and Infrastructure
πŸ’Ύ Data and Databases
πŸ’Ό Professional Skills
🎯 Programming and Development
πŸ”’ Security and Networking
πŸ“š Specialized Topics
CheatGrid
HomeAboutTopicsPricingMy VaultStatsPractice TestsCertifications
LVLEVEL 0
0/5 XP
GitHub
Β© 2026 CheatGridβ„’. All rights reserved.
Privacy PolicyTerms of UseAboutContact

Python Cheat Sheet

Python Cheat Sheet

Tables
Back to Programming Languages
Updated 2026-05-28
Next Topic: Python Libraries Cheat Sheet
🎯Take a practice test on this topic7 practice tests Β· 235 questionsβ†’

Python is a high-level, interpreted programming language created by Guido van Rossum in 1991, emphasizing code readability through significant whitespace and a philosophy of "there should be one obvious way to do it." It's widely used across web development, data science, automation, machine learning, and systems programming, powered by a vast ecosystem of packages and an active global community. Python 3.14 (released October 2025) introduced deferred annotation evaluation, t-strings, and officially supported free-threading β€” marking one of the most significant runtime shifts in Python's history. The key to mastering Python isn't just knowing syntax β€” it's understanding when to use list comprehensions over loops, how duck typing works, and why context managers matter for resource safety.

Quick Index362Β entriesΒ Β·Β 26Β tables
Mind Map

26 tables, 362 concepts. Select a concept node to jump to its table row.

Preparing mind map...

Table 1: Data Types

Python's built-in types are the foundation every program builds on. Knowing which are mutable, which are hashable, and how each behaves in memory keeps you out of the most common Python traps β€” like assuming bool is not an int, or that two distinct lists with equal contents are the same object.

TypeExampleDescription
int
x = 42
Arbitrary-precision integers β€” no overflow limit unlike most languages.
float
y = 3.14
β€’ Double-precision floating-point (64-bit IEEE 754)
β€’ beware rounding errors with exact comparisons.
str
s = "hello"
β€’ Immutable sequence of Unicode characters
β€’ supports f-strings, slicing, and a rich method surface.
bool
flag = True
β€’ Subclass of int β€” True is 1 and False is 0
β€’ this surprises most newcomers
list
arr = [1, 2, 3]
Mutable ordered sequence β€” the most versatile and commonly used collection type.
dict
d = {"key": "val"}
β€’ Mutable mapping of unique keys to values
β€’ insertion-ordered since Python 3.7.
tuple
t = (1, 2, 3)
β€’ Immutable ordered sequence
β€’ hashable when elements are hashable β€” usable as dict keys.
set
s = {1, 2, 3}
β€’ Mutable unordered collection of unique hashable elements
β€’ O(1) membership test.
frozenset
fs = frozenset([1, 2])
β€’ Immutable version of set
β€’ hashable β€” can be a dict key or set element.
None
x = None
β€’ Singleton representing absence of value
β€’ distinct from empty collections or zero
bytes
b = b"data"
β€’ Immutable sequence of byte values (0–255)
β€’ for binary data and network protocols
bytearray
ba = bytearray(b"x")
Mutable version of bytes β€” for in-place modification of binary data.
range
r = range(10)
Immutable sequence representing an arithmetic progression β€” memory-efficient for iteration.
complex
z = 1 + 2j
β€’ Numbers with real and imaginary parts
β€’ j suffix denotes the imaginary unit

Table 2: Operators

Every Python operator looks familiar from other languages, but most carry one or two Python-specific subtleties β€” true vs floor division, modulo whose sign follows the divisor, right-associative **, short-circuit and/or that return operands instead of booleans. The newer additions (:= walrus, | for dicts) reflect Python's continuing language design.

OperatorExampleDescription
Arithmetic
x + y, x - y, x * y
Addition, subtraction, multiplication β€” work on numbers and some sequences (like str, list).
Division
x / y
Always returns float even for integer operands β€” true division since Python 3.
Floor division
x // y
β€’ Rounds down to nearest integer
β€’ result type follows operands (int//int β†’ int).
Modulo
x % y
Remainder β€” sign matches divisor, unlike C/Java where it matches the dividend.
Exponentiation
x ** y
β€’ Raises x to power y
β€’ right-associative and supports fractional/negative exponents
Comparison
x == y, x < y, x >= y
Equality and ordering β€” chainable like 1 < x < 10 (evaluates each pair once).
Identity
x is y
Tests if objects are the same instance in memory β€” not value equality.
Membership
x in y
β€’ Checks if x exists in container y
β€’ O(1) for sets and dicts
Logical AND
x and y
Returns first falsy value or last value if all truthy β€” short-circuits.
Logical OR
x or y
Returns first truthy value or last value if all falsy β€” short-circuits.
Logical NOT
not x
Boolean negation β€” returns True if x is falsy.
Bitwise AND
x & y
β€’ Bitwise AND of integers
β€’ also set intersection for sets
Bitwise OR
x | y
β€’ Bitwise OR of integers
β€’ also set union for sets
Bitwise XOR
x ^ y
β€’ Bitwise exclusive OR
β€’ also symmetric difference for sets
Bitwise NOT
~x
β€’ One's complement β€” inverts all bits
β€’ result is -(x+1).
Left shift
x << n
β€’ Shifts bits left by n
β€’ equivalent to multiplying by 2**n.
Right shift
x >> n
β€’ Shifts bits right by n
β€’ equivalent to floor-dividing by 2**n.
Walrus operator
(x := 5)
Assigns and returns value in a single expression β€” useful in while and comprehensions (3.8+).
Dict merge operator
d1 | d2
β€’ Merges two dicts into a new dict
β€’ &#124β€’ = updates in-place (Python 3.9+).

Table 3: String Operations

Strings are immutable Unicode sequences with a rich method surface for searching, splitting, joining, and formatting. Modern code reaches for f-strings for interpolation, str.join for performant concatenation, and the removeprefix/removesuffix pair for safe edge trimming β€” and always passes encoding="utf-8" to open() to avoid locale-dependent surprises.

MethodExampleDescription
f-strings
f"{x} + {y} = {x+y}"
β€’ Fastest and most readable formatting
β€’ supports expressions, format specs, and nested quotes since 3.12.
format()
"{} {}".format(a, b)
String formatting with positional/keyword arguments β€” more verbose than f-strings.
split()
s.split(",")
β€’ Splits into list by delimiter
β€’ default splits on any whitespace and removes empty strings
join()
",".join(items)
Concatenates an iterable into a single string β€” preferred over += in loops for performance.
strip()
s.strip()
β€’ Removes leading and trailing whitespace
β€’ lstrip()/rstrip() for one side only
removeprefix() / removesuffix()
s.removeprefix("pre")
β€’ Removes exact prefix or suffix if present
β€’ returns unchanged string otherwise (Python 3.9+).
replace()
s.replace("old", "new")
β€’ Returns copy with all occurrences replaced
β€’ optional count parameter to limit replacements
upper() / lower()
s.upper()
β€’ Case conversion
β€’ casefold() for aggressive case-insensitive comparisons across locales
startswith() / endswith()
s.startswith("pre")
Checks prefix/suffix β€” accepts a tuple of multiple options.
find() / index()
s.find("sub")
β€’ Finds substring position
β€’ find() returns -1 if not found, index() raises ValueError.
count()
s.count("x")
Returns the number of non-overlapping occurrences of a substring.
partition()
s.partition(":")
β€’ Splits into 3-tuple (before, sep, after)
β€’ always returns 3 parts even if separator not found
isdigit() / isalpha()
s.isdigit()
β€’ Character-type checks
β€’ many variants: isalnum(), isspace(), islower(), isupper().
encode() / decode()
s.encode("utf-8")
β€’ Converts str β†’ bytes via encoding
β€’ bytes.decode("utf-8") reverses it
center() / ljust() / rjust()
s.center(20, "*")
β€’ Pads string to a given width with fill character
β€’ useful for formatting output
zfill()
"42".zfill(5)
Pads with leading zeros to specified width β€” "00042".
title() / capitalize()
s.title()
β€’ title() capitalizes each word
β€’ capitalize() only the first character of the whole string
translate()
s.translate(str.maketrans("abc","ABC"))
Replaces characters per a mapping table β€” efficient for multi-character substitution.
t-strings
t"Hello {name}"
Template strings producing a Template object for custom processing and safe interpolation (Python 3.14+).
%-formatting (legacy)
"%s %d" % (s, n)
C-style formatting β€” replaced by f-strings but still seen in legacy code.

Table 4: Slicing and Indexing

Slice syntax [start:stop:step] is the unified way to index Python sequences β€” zero-based, half-open, and quietly forgiving of out-of-range indices. The same three components handle reversal ([::-1]), shallow copies ([:]), every-other-element selection ([::2]), and reverse walks, making slicing one of the highest-leverage pieces of syntax to master.

PatternExampleDescription
Positive indexing
s[0], s[2]
Zero-based indexing from start β€” first element is [0].
Negative indexing
s[-1], s[-2]
Indexes from end β€” -1 is last element, -2 is second-to-last.
Basic slice
s[1:4]
Elements from index 1 up to (but not including) 4 β€” half-open interval.
Omit start
s[:3]
From beginning to index 3 β€” equivalent to s[0:3].
Omit end
s[2:]
From index 2 to end β€” includes the last element.
Full copy
s[:]
Creates a shallow copy of the entire sequence.
Step slicing
s[::2]
Every 2nd element β€” third parameter is step size.
Reverse
s[::-1]
Reverses sequence β€” negative step iterates backward.
Negative step slice
s[4:1:-1]
From index 4 down to (but not including) 1.

Table 5: Control Flow

Python's control-flow keywords stay deliberately small: if/elif/else driven by truthiness, two loop forms with the unusual else clause, and (since 3.10) structural match with rich pattern types. The match statement goes far beyond a switch β€” it destructures sequences, mappings, class instances, and supports OR patterns, guards, and wildcard captures in one readable block.

StatementExampleDescription
if
if x > 0:
print("pos")
Conditional execution β€” tests truthiness of expression.
elif
elif x == 0:
print("zero")
Chain multiple conditions β€” only the first true branch executes.
else
else:
print("neg")
Executes when all preceding conditions are false.
Ternary expression
x if cond else y
Inline conditional β€” returns x if cond is true, else y.
for loop
for i in range(5):
print(i)
Iterates over any iterable object β€” most common loop type.
while loop
while x > 0:
x -= 1
β€’ Repeats while condition is truthy
β€’ check happens before each iteration
break
break
Exits innermost enclosing loop immediately.
continue
continue
Skips to the next iteration of the innermost loop.
else on loops
for i in items:
pass
else:
print("done")
Executes if loop completes without break β€” useful for search patterns.
pass
pass
Null operation β€” syntactic placeholder where a statement is required.
match-case (literal)
match x:
case 1:
print("one")
Structural pattern matching β€” compares against literal values (Python 3.10+).
match OR pattern
case 1 | 2 | 3:
β€’ Matches multiple values in one case arm using &#124β€’ (Python 3.10+).
match capture pattern
case Point(x=x, y=y):
Destructures class instances by attribute names into local variables (Python 3.10+).
match sequence pattern
case [first, *rest]:
Matches and unpacks sequences β€” *rest captures remaining items (Python 3.10+).
match mapping pattern
case {"action": a}:
Matches dicts by key presence β€” **rest captures remaining keys (Python 3.10+).
match guard
case x if x > 0:
Adds a condition to any pattern β€” branch only executes if guard is true (Python 3.10+).
match wildcard
case _:
Catch-all pattern that matches anything β€” like default in other languages (Python 3.10+).

Table 6: Functions

Functions are first-class values in Python β€” they can be passed, returned, default-parameterized, decorated, and unpacked from iterables. Two non-obvious facts dominate the rough edges: default arguments are evaluated once at definition time (the source of the mutable-default trap), and the / and * separators in signatures enforce positional-only and keyword-only calling conventions explicitly.

TechniqueExampleDescription
Function definition
def func(a, b):
return a + b
β€’ Creates a reusable code block
β€’ return without value returns None.
Default arguments
def f(x, y=10):
β€’ Parameters with default values β€” must come after required params
β€’ evaluated once at definition.
Keyword arguments
f(x=1, y=2)
Named parameters β€” allows any order at call site and improves call-site clarity.
Positional-only params
def f(a, b, /, c):
Parameters before / cannot be passed by keyword β€” enforces positional calling (Python 3.8+).
Keyword-only params
def f(a, *, b):
Parameters after bare * must be passed by keyword β€” prevents accidental positional misuse.
*args
def f(*args):
Variable positional arguments β€” collected as a tuple.
**kwargs
def f(**kwargs):
Variable keyword arguments β€” collected as a dict.
Lambda
lambda x: x * 2
Anonymous function β€” single expression only, no statements.
Docstring
"""Description."""
First string in function body β€” accessible via __doc__ and used by help().
Type hints
def f(x: int) -> int:
Optional annotations β€” not enforced at runtime but used by type checkers.
Unpacking in calls
f(*args, **kwargs)
Expands sequences and mappings into function arguments at call time.

Table 7: Comprehensions and Generators

List, dict, and set comprehensions build a collection from an iterable in one expression β€” typically faster than the equivalent for/append loop and easier to read. Generator expressions and yield-based functions use almost the same syntax but produce lazy iterators, which is what lets Python stream gigabytes without loading them into memory.

TechniqueExampleDescription
List comprehension
[x**2 for x in range(10)]
Creates a list from an iterable β€” faster and cleaner than for-loop append.
With condition
[x for x in nums if x > 0]
Filters elements β€” condition acts as an inline filter on the iterable.
Nested comprehension
[x+y for x in a for y in b]
Multiple for clauses β€” left-to-right nesting order (outer loop first).
Dict comprehension
{k: v**2 for k, v in d.items()}
Creates a dict from an iterable β€” key-value pairs with unique keys (later values win).
Set comprehension
{x**2 for x in nums}
Creates a set β€” automatically deduplicates results.
Generator expression
(x**2 for x in range(10))
β€’ Lazy evaluation β€” memory-efficient for large sequences
β€’ not stored in memory
Generator function
def gen():
yield x
Function with yield β€” pauses and resumes execution on each next() call.
yield from
yield from iterable
Delegates to a subiterator β€” simplifies nested generators (Python 3.3+).

Table 8: Classes and OOP

Python's class model favors flexibility over enforcement β€” no private attributes, no compile-time type checks, no formal interfaces. What you do get is a predictable MRO via C3 linearization, descriptor-driven property access, real multiple inheritance, and abc.ABC for defining enforced interfaces without metaclass boilerplate.

ConceptExampleDescription
Class definition
class Dog:
pass
Creates a new type β€” naming convention is PascalCase.
init
def __init__(self, name):
Constructor β€” initializes instance when object is created.
Instance attributes
self.name = name
Per-object data β€” defined in __init__ or other methods via self.
Class attributes
class Dog:
species = "Canis"
Shared across all instances β€” defined at class level.
Methods
def bark(self):
Functions bound to instance β€” first param is self by convention.
Inheritance
class Puppy(Dog):
Derives from parent class β€” inherits attributes and methods.
super()
super().__init__(name)
Calls parent class method β€” handles multiple inheritance via MRO correctly.
@property
@property
def age(self):
Getter method β€” accessed like an attribute but computed dynamically.
@property.setter
@age.setter
def age(self, val):
Setter method β€” allows validation and side effects on attribute assignment.
@classmethod
@classmethod
def factory(cls):
Method receives class, not instance β€” for alternative constructors.
@staticmethod
@staticmethod
def util():
Regular function in class namespace β€” no implicit first parameter.
Multiple inheritance
class C(A, B):
Inherits from multiple classes β€” uses C3 MRO to resolve method order.
ABC / @abstractmethod
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self): ...
Enforces interface β€” subclasses cannot be instantiated until all abstract methods are implemented.
str / repr
def __repr__(self):
String representations β€” __repr__ for debugging, __str__ for users.
slots
__slots__ = ('x', 'y')
Declares fixed attributes β€” reduces memory and blocks __dict__ creation.
new
def __new__(cls):
β€’ Static method that creates the instance before __init__
β€’ needed for immutable subclasses
init_subclass
def __init_subclass__(cls):
Hook called when a class is subclassed β€” lightweight alternative to metaclasses (Python 3.6+).

Table 9: Magic Methods (Dunder Methods)

Dunder methods are how custom classes plug into Python's syntax β€” len(), obj[key], for x in obj, ==, +, with, even calling an instance like a function. Implementing the right small set turns a class from "useful" to "feels built-in," and the data model documents every hook the language consults.

MethodExampleDescription
init
def __init__(self):
Instance initialization β€” called when object is created.
str
def __str__(self):
Informal string representation β€” used by str() and print().
repr
def __repr__(self):
β€’ Official string representation β€” should be valid Python when possible
β€’ fallback for __str__.
len
def __len__(self):
β€’ Returns length β€” called by len()
β€’ must return non-negative int.
bool
def __bool__(self):
β€’ Truthiness check β€” called by bool() and in boolean contexts
β€’ falls back to __len__.
getitem
def __getitem__(self, key):
Indexing operator [] β€” enables subscript access.
setitem
def __setitem__(self, k, v):
Item assignment [k] = v β€” makes object a mutable container.
delitem
def __delitem__(self, key):
Deletion via del obj[key] β€” required for full mutable container protocol.
iter
def __iter__(self):
Returns an iterator β€” enables for-loop support and unpacking.
next
def __next__(self):
Gets next value β€” raises StopIteration when exhausted.
contains
def __contains__(self, item):
Membership test in β€” should return boolean.
enter / exit
def __enter__(self):
Context manager protocol β€” enables with statement.
add
def __add__(self, other):
β€’ Addition operator + β€” return new object
β€’ don't modify self.
eq
def __eq__(self, other):
β€’ Equality operator == β€” default compares identity
β€’ defining it disables __hash__ by default
hash
def __hash__(self):
β€’ Makes instance hashable β€” required to use as dict key or set element
β€’ must be consistent with __eq__.
lt / gt
def __lt__(self, other):
Comparison operators < and > β€” enables sorting.
call
def __call__(self):
Makes instance callable like a function β€” enables obj() syntax.
format
def __format__(self, spec):
Custom format spec support β€” called by format() and f-string {obj:spec} syntax.
class_getitem
def __class_getitem__(cls, item):
Enables generic alias syntax MyClass[T] β€” supports runtime type parameterization.

Table 10: Exception Handling

Exceptions are Python's primary error-signalling mechanism, with a four-clause structure (try/except/else/finally) and clean chaining via raise ... from .... Python 3.11's ExceptionGroup and except* give the language a way to express multiple concurrent failures β€” essential for asyncio.TaskGroup and structured-concurrency designs.

TechniqueExampleDescription
try-except
try:
risky()
except ValueError:
Catches exceptions β€” specific types should precede general ones.
Multiple exceptions
except (ValueError, KeyError):
Catches multiple types in one clause β€” tuple of exception classes.
except with variable
except ValueError as e:
Captures the exception object β€” access message and attributes via e.
else clause
else:
print("success")
Runs if no exception raised in the try block β€” before finally.
finally
finally:
cleanup()
β€’ Always executes β€” even if exception raised or return encountered
β€’ for resource cleanup
raise
raise ValueError("msg")
Throws an exception β€” bare raise re-raises the current active exception.
Custom exceptions
class MyError(Exception):
Define new exception types β€” inherit from Exception or a more specific subclass.
Exception chaining
raise NewError() from e
Links exceptions β€” preserves original cause for debugging tracebacks.
ExceptionGroup
raise ExceptionGroup("msg",
[ValueError(), KeyError()])
Groups multiple exceptions together β€” handled with except* (Python 3.11+).
except*
except* ValueError as eg:
Catches matching exceptions from an ExceptionGroup β€” multiple except* clauses can match (Python 3.11+).
Bare except (avoid)
except:
β€’ Catches all exceptions including KeyboardInterrupt β€” dangerous, hides bugs
β€’ avoid

Table 11: File Operations

File I/O uses a unified open() API parameterized by mode (r/w/a/b/+) and is best paired with with to guarantee cleanup on every exit path. Modern code increasingly leans on pathlib.Path for path manipulation and one-call read_text/write_text shortcuts β€” and remembers to pass encoding="utf-8" to dodge locale-dependent surprises across operating systems.

TechniqueExampleDescription
with statement
with open("f.txt") as f:
Context manager β€” auto-closes file even if an exception occurs.
Iterate file
for line in f:
Memory-efficient line iteration β€” preferred over readlines() for large files.
read()
content = f.read()
Reads entire file as string β€” memory-intensive for large files.
readline()
line = f.readline()
Reads a single line β€” includes newline character.
readlines()
lines = f.readlines()
Returns list of all lines β€” newlines preserved in each element.
write()
f.write("text")
Writes string to file β€” doesn't add newline automatically.
writelines()
f.writelines(lines)
Writes a sequence of strings β€” no newlines added automatically.
Mode "r" (default)
open("f.txt", "r")
Read mode β€” raises FileNotFoundError if file does not exist.
Mode "w"
open("f.txt", "w")
Write mode β€” truncates existing file or creates new.
Mode "a"
open("f.txt", "a")
β€’ Append mode β€” adds to end of existing file
β€’ creates if absent
Mode "b"
open("f.bin", "rb")
β€’ Binary mode β€” for non-text files
β€’ returns bytes objects
Mode "x"
open("f.txt", "x")
Exclusive creation β€” raises FileExistsError if file already exists.
pathlib.Path
Path("f.txt").read_text(encoding="utf-8")
β€’ Object-oriented path API β€” cleaner than os.path
β€’ copy()/move() added in 3.14.
open() for reading
f = open("f.txt", encoding="utf-8")
Always specify encoding="utf-8" to avoid locale-dependent surprises on Windows.

Table 12: Modules and Imports

Python's import system stitches code into namespaces using sys.path (search list), sys.modules (cache), and a finder/loader stack you can extend. Idiomatic code prefers explicit import over from X import *, uses relative imports inside packages, and guards script-only code behind if __name__ == "__main__": so the same file works as both a library and a script.

TechniqueExampleDescription
import module
import math
Imports entire module β€” access via math.func().
from import
from math import sqrt
Imports specific names β€” directly accessible without module prefix.
import as
import numpy as np
Aliases module to a shorter name for convenience.
if name == "main"
if __name__ == "__main__":
Checks if script is run directly vs imported β€” guards executable code.
Relative imports
from . import sibling
Within packages β€” dot notation for current/parent package.
init.py
pkg/__init__.py
Makes directory a package β€” can initialize the package and control public API.
sys.path
sys.path.append(dir)
Module search paths β€” modify to add custom locations.
importlib
importlib.import_module(name)
Dynamic imports β€” import by string name at runtime.
from import * (avoid)
from math import *
β€’ Imports all public names β€” discouraged
β€’ pollutes namespace and hides origins

Table 13: Common Built-in Functions

The built-ins show up in nearly every Python program β€” len, range, enumerate, zip, map, filter, sum, sorted, min/max, all/any, isinstance. Mastering their key= arguments, lazy-iterator returns, and edge cases (vacuous all([]), zip truncation, input always returning str) keeps day-to-day code short and correct.

FunctionExampleDescription
len()
len([1, 2, 3])
Returns length of sequence or collection β€” calls __len__.
range()
range(start, stop, step)
Generates arithmetic sequence β€” stop is exclusive.
enumerate()
enumerate(items, start=0)
Adds counter to iterable β€” returns (index, value) pairs.
zip()
zip(list1, list2)
β€’ Pairs elements from iterables β€” stops at shortest input
β€’ strict=True raises if lengths differ (3.10+).
map()
map(func, iterable)
Applies function to each element β€” returns lazy iterator.
filter()
filter(predicate, items)
Filters elements where predicate is truthy β€” lazy iterator.
sorted()
sorted(items, key=func)
β€’ Returns new sorted list β€” doesn't modify original
β€’ reverse=True for descending
sum()
sum(numbers, start=0)
Sums a numeric iterable β€” optional starting value.
min() / max()
min(items, key=func)
Returns smallest/largest element β€” optional key function.
all()
all([True, True, False])
Returns True if all elements truthy or iterable is empty (vacuously true).
any()
any([False, False, True])
Returns True if any element truthy.
reversed()
reversed(seq)
Returns a reverse iterator β€” doesn't modify original.
isinstance()
isinstance(obj, type)
β€’ Checks object type β€” accepts tuple of types
β€’ works with inheritance
type()
type(obj)
Returns object's exact type β€” for checking, prefer isinstance().
getattr() / setattr() / hasattr()
getattr(obj, "name", default)
Dynamic attribute access β€” getattr supports a default, avoids AttributeError.
vars() / dir()
vars(obj)
β€’ vars() returns __dict__
β€’ dir() lists all attributes and methods of an object
abs() / divmod()
abs(-5), divmod(17, 5)
β€’ abs() returns magnitude
β€’ divmod(a, b) returns (quotient, remainder) as a tuple
hex() / oct() / bin()
hex(255) β†’ "0xff"
Converts integers to hexadecimal/octal/binary string representations.
callable()
callable(func)
Returns True if object appears callable (has __call__).
hash()
hash("hello")
β€’ Returns integer hash of a hashable object
β€’ consistent within a process
id()
id(obj)
Returns object's memory address β€” guaranteed unique for its lifetime.
input()
input("Enter: ")
Reads a line from stdin β€” always returns string.
print()
print(*values, sep=" ", end="\n")
Writes to stdout β€” customizable separator and line ending.

Table 14: Itertools Module

itertools is Python's grab-bag of iterator-combining building blocks β€” composable, lazy, and implemented in C for speed. The module covers infinite generators (count, cycle), combinatorics (combinations, permutations, product), grouping by adjacent runs (groupby), windowing (pairwise, batched), and reduction (accumulate).

FunctionExampleDescription
chain()
chain(it1, it2)
Concatenates iterables β€” flattens multiple iterables into single iterator.
chain.from_iterable()
chain.from_iterable(nested)
Flattens one level of nesting lazily β€” like chain(*nested) without unpacking.
islice()
islice(it, start, stop)
Slices an iterator β€” doesn't support negative indices.
compress()
compress('ABCDEF', [1,0,1,0,1,1])
Filters data by a boolean selector iterable β€” yields A, C, E, F.
filterfalse()
filterfalse(lambda x: x<5, [1,4,6,3,8])
Yields elements where predicate is false β€” opposite of filter().
starmap()
starmap(pow, [(2,5), (3,2)])
Applies function with arguments unpacked from each tuple in iterable.
pairwise()
pairwise([1, 2, 3])
Returns overlapping pairs of consecutive elements β€” (1,2), (2,3) (Python 3.10+).
batched()
batched(range(10), 3)
Splits iterable into fixed-size tuples β€” last batch may be shorter (Python 3.12+).
groupby()
groupby(items, key=func)
Groups consecutive elements by key β€” input must be sorted by same key first.
accumulate()
accumulate(nums, func)
β€’ Running totals or reductions β€” default is addition
β€’ accepts any binary function
takewhile()
takewhile(pred, it)
Yields elements while predicate is true β€” stops at first false.
dropwhile()
dropwhile(pred, it)
Skips elements while predicate is true β€” then yields all remaining.
tee()
tee(iterable, n=2)
Splits one iterator into n independent iterators β€” use before an iterator is consumed.
zip_longest()
zip_longest(a, b, fillvalue=None)
Like zip() but continues to the longest input, filling shorter ones with fillvalue.
combinations()
combinations(items, r)
All r-length combinations β€” order doesn't matter, no repeated elements.
combinations_with_replacement()
combinations_with_replacement('AB', 2)
All r-length combinations allowing repeated elements β€” yields AA, AB, BB.
permutations()
permutations(items, r)
All r-length permutations β€” order matters, no repeated elements.
product()
product(a, b)
Cartesian product β€” equivalent to nested for-loops.
count()
count(start=0, step=1)
Infinite counter β€” useful with zip() or takewhile().
cycle()
cycle([1, 2, 3])
Repeats iterable infinitely β€” saves copy of all elements in memory.
repeat()
repeat(10, times=3)
Repeats object n times β€” infinite if times omitted.

Table 15: Decorators

Decorators wrap a function or class to add behavior without changing the callsite. The syntax @decorator is sugar for func = decorator(func), extending naturally to parameterized decorators (three nesting levels) and to stacking multiple decorators (applied bottom-up). functools.wraps is mandatory in every wrapper to preserve the original's metadata.

TechniqueExampleDescription
Function decorator
@decorator
def func():
Wraps function to modify behavior β€” sugar for func = decorator(func).
Simple decorator
def deco(f):
def wrap():
return f()
return wrap
Returns a wrapper function that replaces the original.
functools.wraps
@wraps(f)
def wrapper():
Preserves function metadata (__name__, __doc__, __wrapped__) β€” use inside every wrapper.
Decorator with args
@deco(arg)
def func():
Returns a decorator β€” three-level nesting: deco(arg)(func).
Stacking decorators
@deco1
@deco2
def f():
Multiple decorators applied bottom-up: deco1(deco2(f)).
Class decorator
@deco
class MyClass:
Modifies or replaces a class β€” callable receives and returns the class.
lru_cache
@lru_cache(maxsize=128)
Memoizes function results with LRU eviction β€” cache_info() reports hits and misses.
functools.cache
@cache
Unbounded memoization β€” faster than lru_cache(None) as no eviction overhead (Python 3.9+).
functools.cached_property
@cached_property
def value(self):
Computes property once per instance and caches as a normal attribute β€” delete attribute to reset (Python 3.8+).
functools.total_ordering
@total_ordering
class C:
Generates all comparison methods from just __eq__ and one ordering method (__lt__, __le__, __gt__, or __ge__).
Method decorator
@staticmethod
def method():
Built-in decorators: @classmethod, @staticmethod, @property.

Table 16: Context Managers

Context managers β€” driven by __enter__/__exit__ (or __aenter__/__aexit__ for async) β€” guarantee deterministic resource cleanup even if the block raises. The contextlib module makes new managers trivial via the @contextmanager generator decorator and packages handy ones like suppress, closing, and the dynamic-composition ExitStack.

TechniqueExampleDescription
with statement
with open("f") as f:
Ensures resource cleanup β€” calls __enter__ and __exit__.
Multiple contexts
with open("a") as f1, open("b") as f2:
Manages multiple resources β€” left-to-right acquisition, right-to-left release.
contextlib.contextmanager
@contextmanager
def ctx():
yield
Creates a context manager from a generator β€” yield separates setup from teardown.
Custom enter / exit
def __enter__(self):
return self
Full protocol implementation β€” __exit__ receives (exc_type, exc_val, exc_tb).
contextlib.suppress
with suppress(FileNotFoundError):
Ignores specified exceptions β€” cleaner than try: ... except: pass.
contextlib.closing
with closing(obj):
Calls close() on exit β€” for objects without a native context manager.
ExitStack
with ExitStack() as stack:
Manages a variable number of context managers dynamically.

Table 17: Async/Await (Asyncio)

asyncio delivers cooperative single-threaded concurrency: many waiting coroutines, one running at a time, no blocking. await is the only yield point β€” every awaited call must be non-blocking β€” and Python 3.11+ adds structured concurrency via TaskGroup plus deadline-based timeout(). The synchronization primitives (Lock, Event, Semaphore, Queue) mirror the threading module but are awaitable, not blocking.

TechniqueExampleDescription
async def
async def fetch():
Defines a coroutine function β€” must be awaited or scheduled as a task.
await
result = await coro()
Suspends coroutine β€” yields control to event loop until result is ready.
asyncio.run()
asyncio.run(main())
β€’ Entry point β€” creates event loop and runs coroutine
β€’ closes loop on completion
asyncio.create_task()
task = create_task(coro())
Schedules coroutine to run concurrently with other tasks.
asyncio.TaskGroup
async with TaskGroup() as tg:
tg.create_task(coro())
Structured concurrency β€” auto-cancels remaining tasks on failure (Python 3.11+).
asyncio.gather()
await gather(coro1(), coro2())
Runs multiple coroutines concurrently β€” waits for all to complete.
asyncio.timeout()
async with asyncio.timeout(5):
Context manager with deadline β€” raises TimeoutError if exceeded (Python 3.11+).
asyncio.wait_for()
await wait_for(coro(), timeout=5)
Adds timeout to a coroutine β€” raises TimeoutError if exceeded.
asyncio.Lock
async with asyncio.Lock():
Mutex lock for async tasks β€” prevents concurrent access to shared state.
asyncio.Event
event = asyncio.Event()
await event.wait()
Notifies multiple tasks that something happened β€” set(), clear(), wait().
asyncio.Semaphore
async with asyncio.Semaphore(10):
Limits number of concurrent coroutines accessing a resource.
asyncio.Queue
q = asyncio.Queue()
await q.put(x)
x = await q.get()
FIFO queue for producer-consumer patterns β€” task_done() + join() for completion tracking.
asyncio.sleep()
await asyncio.sleep(1)
Non-blocking sleep β€” yields control to other coroutines.
async for
async for item in async_iter:
Iterates over an async iterable β€” calls __aiter__ and __anext__.
async with
async with resource:
Async context manager β€” awaits __aenter__ and __aexit__.

Table 18: Type Hints

Type hints are runtime metadata that static checkers (mypy, pyright, pyre) consume to find bugs before they ship. PEP 585 (Python 3.9) made built-in generics work directly (list[int]), PEP 604 (3.10) introduced int | None unions, PEP 695 (3.12) added inline generic-parameter syntax, and PEP 649/749 (3.14) introduced deferred evaluation β€” annotations are no longer evaluated at import time, dramatically improving startup performance.

TechniqueExampleDescription
Basic types
x: int = 5
Annotates variable type β€” not enforced at runtime.
Function annotations
def f(x: int) -> str:
Parameter and return types β€” used by type checkers like mypy and pyright.
Built-in generics
items: list[int]
d: dict[str, int]
Use built-in types directly as generics β€” no List/Dict imports needed (Python 3.9+).
Union (pipe syntax)
x: int | str
β€’ Multiple allowed types β€” modern &#124β€’ syntax replaces Union[int, str] (Python 3.10+).
Optional[T]
x: Optional[int] = None
β€’ Shorthand for Union[T, None] β€” prefer T &#124β€’ None in new code
TypedDict
class Config(TypedDict):
host: str
port: int
Typed dict with specific key/value types β€” enables type checking on dict literals.
Callable
f: Callable[[int], str]
Function type β€” list of param types then return type.
Protocol
class Sized(Protocol):
def __len__(self): ...
Structural subtyping β€” duck typing with static type checking.
Literal
x: Literal["a", "b"]
Restricts to specific values β€” useful for discriminated unions.
TypeVar
T = TypeVar('T')
Generic type variable β€” for parametric polymorphism in functions and classes.
Type parameter syntax
def f[T](x: T) -> T:
Inline generic type variables on functions and classes β€” replaces TypeVar (Python 3.12+).
type statement
type Vector = list[float]
Declares a type alias with lazy evaluation β€” replaces TypeAlias annotation (Python 3.12+).
ParamSpec
P = ParamSpec('P')
Captures the parameter types of a callable β€” essential for typing decorators that preserve signatures.
Unpack
def f(**kwargs: Unpack[Config]):
Types **kwargs using a TypedDict β€” enables per-key type checking (Python 3.12+).
Any
x: Any
β€’ Disables type checking β€” avoid when possible
β€’ escape hatch
TypeIs
def is_str(x) -> TypeIs[str]:
Narrows type in both if/else branches β€” safer than TypeGuard (Python 3.13+).
@override
@override
def method(self):
Marks method as overriding parent β€” type checkers flag mismatches (Python 3.12+).
Deferred annotations
def f(x: MyClass) -> None:
In Python 3.14+ annotations are not evaluated at import time β€” forward references work natively without quotes.

Table 19: Regular Expressions

The re module exposes a single regex engine through both functional (re.search, re.findall, re.sub) and compiled (re.compile(...)) APIs. The biggest pitfalls are re.match vs re.search (anchored vs unanchored) and forgetting r"..." raw strings β€” without them, Python's own backslash processing eats half your pattern before the engine sees it.

FunctionExampleDescription
re.search()
re.search(r"\d+", s)
Searches anywhere in string β€” returns first match object or None.
re.match()
re.match(r"\d+", s)
Matches at start of string only β€” returns match object or None.
re.fullmatch()
re.fullmatch(r"\d+", s)
Matches entire string β€” both ^ and $ are implicit.
re.findall()
re.findall(r"\d+", s)
Returns list of all matches β€” non-overlapping strings.
re.finditer()
re.finditer(r"\d+", s)
Returns iterator of match objects β€” memory-efficient for large inputs.
re.sub()
re.sub(r"\d+", "X", s)
Replaces matches β€” accepts function as replacement for dynamic substitutions.
re.split()
re.split(r"\s+", s)
Splits by pattern β€” preserves groups in result if pattern has groups.
re.compile()
p = re.compile(r"\d+")
Precompiles pattern β€” faster for reuse across many strings.
Groups
m.group(1)
Captures subpatterns β€” group(0) is entire match, group(1) first capture.
Named groups
(?P<name>\d+)
Named capture β€” access via m.group('name') or m.groupdict().
Non-greedy
r"\d+?"
? after quantifier β€” matches as few characters as possible.
Lookahead / lookbehind
r"\d+(?= px)"
Zero-width assertions β€” (?=...) lookahead, (?<=...) lookbehind β€” don't consume chars.
re.IGNORECASE
re.search(r"a", s, re.I)
β€’ Case-insensitive matching β€” combine flags with re.I &#124β€’ re.M.
re.DOTALL
re.search(r".+", s, re.S)
Makes . match newlines too β€” useful for multi-line text blocks.
re.MULTILINE
re.search(r"^word", s, re.M)
^ and $ match start/end of each line, not just the string.
Raw strings
r"\d+"
Prefix with r β€” avoids double-escaping backslashes in patterns.

Table 20: Dataclasses and Modern Class Features

@dataclass (Python 3.7+) generates __init__, __repr__, and __eq__ from type-annotated attributes, with knobs for frozen, slots, and kw_only to fit specific use cases. For runtime validation and coercion at system boundaries, the ecosystem leans on pydantic; for richer feature sets without the validation cost, on attrs.

TechniqueExampleDescription
@dataclass
@dataclass
class Point:
x: float
y: float
Auto-generates __init__, __repr__, __eq__ β€” less boilerplate (Python 3.7+).
Field with default
x: int = 0
Default values β€” mutable defaults must use field(default_factory=...).
field()
items: list = field(default_factory=list)
Customizes field behavior β€” prevents shared mutable defaults across instances.
frozen=True
@dataclass(frozen=True)
Makes instances immutable β€” enables hashing for use as dict keys.
slots=True
@dataclass(slots=True)
Generates __slots__ for reduced memory β€” prevents dynamic attribute creation (Python 3.10+).
kw_only=True
@dataclass(kw_only=True)
Forces keyword-only arguments in __init__ β€” prevents positional mistakes (Python 3.10+).
post_init
def __post_init__(self):
Runs after __init__ β€” for computed fields or validation.
InitVar
temp: InitVar[int]
Init-only variable β€” not stored as field, available in __post_init__.
asdict() / astuple()
asdict(obj)
Converts dataclass to dict/tuple β€” deep-copies nested dataclasses.
attrs
@define
class C:
Third-party alternative β€” more features than dataclasses (validators, converters, slots by default).
Pydantic
class User(BaseModel):
Data validation library β€” runtime type checking, coercion, and JSON schema generation.

Table 21: Enum Module

The enum module provides a way to define named constant sets with distinct identity, preventing typos and magic-string comparisons. Python 3.11 added StrEnum and IntEnum auto-values that produce the lowercased member name, and Python 3.12 improved the repr of enum members and simplified subclassing.

TypeExampleDescription
Enum
class Color(Enum):
RED = 1
GREEN = 2
Base class for enumerations β€” members have unique identity and are accessed as Color.RED.
auto()
RED = auto()
Automatically assigns values β€” default is sequential integers starting at 1.
IntEnum
class Status(IntEnum):
OK = 200
Enum whose members are also int β€” compatible with integer APIs.
StrEnum
class Direction(StrEnum):
NORTH = auto()
Members are also str β€” auto() generates the lowercased member name (Python 3.11+).
Flag
class Perm(Flag):
READ = auto()
WRITE = auto()
β€’ Members support bitwise operations β€” combine with &#124β€’ , test with in.
IntFlag
class Mode(IntFlag):
R = 4
W = 2
Flag + int compatibility β€” members behave as integers for system calls and bitmasks.
Enum iteration
list(Color)
Enumerates all members in definition order.
Enum lookup
Color(1), Color["RED"]
Access by value (Color(1)) or by name (Color["RED"]).
member.value / .name
Color.RED.value, Color.RED.name
Access the raw value or the member name string.

Table 22: Collections Module

The collections module provides specialized container datatypes that extend Python's built-in dict, list, set, and tuple. deque is the go-to for O(1) queue operations, OrderedDict retains its usefulness for LRU implementations despite regular dicts being ordered since 3.7, and Counter eliminates a common tally-building loop pattern entirely.

TypeExampleDescription
deque
from collections import deque
dq = deque([1,2,3], maxlen=5)
β€’ Double-ended queue β€” O(1) append and pop on both ends
β€’ list.pop(0) is O(n).
deque.appendleft() / popleft()
dq.appendleft(0)
dq.popleft()
Efficient left-side operations β€” core advantage over list for queue usage.
deque with maxlen
deque(maxlen=10)
β€’ Bounded deque β€” oldest items auto-discarded when full
β€’ perfect for sliding windows
defaultdict
defaultdict(list)
Dict that auto-creates missing keys using a factory function.
Counter
Counter("abracadabra")
Dict subclass for counting hashable objects β€” most_common(n) returns top-n items.
Counter arithmetic
c1 + c2, c1 - c2, c1 & c2
β€’ Supports +, -, & (intersection), &#124β€’ (union) β€” results keep only positive counts.
Counter.total()
c.total()
Returns sum of all counts (Python 3.10+).
namedtuple
Point = namedtuple('Point', 'x y')
Tuple with named fields β€” immutable, memory-efficient alternative to a class.
OrderedDict
OrderedDict()
β€’ Dict that remembers insertion order and supports move_to_end()
β€’ still useful for LRU caches
OrderedDict.move_to_end()
od.move_to_end("key")
Moves key to end (or beginning with last=False) β€” key primitive for LRU cache implementation.
ChainMap
ChainMap(local, global_, builtins)
β€’ Groups multiple dicts β€” searches sequentially without copying
β€’ write goes to first map
UserDict / UserList
class MyDict(UserDict):
Subclass-friendly wrappers around dict/list β€” safer to subclass than the built-ins directly.

Table 23: Concurrency

Python offers three concurrency models: threading (shared memory, GIL-limited for CPU work, good for I/O), multiprocessing (separate processes, true parallelism, higher overhead), and asyncio (cooperative single-thread, ultra-low overhead, requires async-aware code). concurrent.futures wraps both threading and multiprocessing behind one uniform executor interface; Python 3.14 adds InterpreterPoolExecutor for sub-interpreter concurrency without the IPC overhead of full processes.

TechniqueExampleDescription
ThreadPoolExecutor
with ThreadPoolExecutor(max_workers=4) as ex:
fut = ex.submit(func, arg)
β€’ Thread pool β€” best for I/O-bound tasks
β€’ GIL limits CPU parallelism
ProcessPoolExecutor
with ProcessPoolExecutor() as ex:
fut = ex.submit(func, arg)
β€’ Process pool β€” true CPU parallelism
β€’ data is pickled between processes
executor.map()
list(ex.map(func, items))
Parallel map β€” returns results in submission order, raises first exception encountered.
Future.result()
fut.result(timeout=5)
Blocks until result available β€” re-raises any exception thrown by the worker.
as_completed()
for f in as_completed(futures):
Yields futures as they complete β€” better throughput than waiting in submission order.
InterpreterPoolExecutor
with InterpreterPoolExecutor() as ex:
Runs tasks in sub-interpreters β€” true parallelism without pickle overhead (Python 3.14+).
threading.Thread
t = Thread(target=f, args=(1,))
t.start(); t.join()
Spawns a native OS thread β€” use daemon=True to die with main thread.
threading.Lock
lock = Lock()
with lock:
Mutex for shared state β€” with lock: is preferred (auto-releases on exceptions).
threading.Event
e = Event()
e.set(); e.wait()
Allows threads to signal each other β€” wait() blocks until set().
threading.Semaphore
sem = Semaphore(3)
Limits concurrent access to n threads β€” acquire() decrements, release() increments.
multiprocessing.Pool
with Pool() as pool:
pool.map(func, items)
Higher-level multiprocessing API β€” automatically manages worker process lifecycle.
multiprocessing.Queue
q = mp.Queue()
q.put(x); q.get()
Process-safe queue β€” IPC via pickle across process boundaries.
GIL and free-threading
python3.14 -X gil=0
Python 3.14 makes free-threading production-ready (PEP 779) β€” true thread parallelism possible.

Table 24: Logging Module

Python's logging is a hierarchical system: every logger is a node in a tree rooted at the root logger, and events propagate upward unless propagate=False. Application code should always call logging.getLogger(__name__), let libraries configure nothing themselves, and leave root-level configuration to the application entry point.

TechniqueExampleDescription
basicConfig()
logging.basicConfig(level=logging.INFO,
format="%(asctime)s %(levelname)s %(message)s")
β€’ Configures root logger β€” one-time call for simple scripts
β€’ must be before first log call
getLogger()
logger = logging.getLogger(__name__)
β€’ Gets or creates a named logger β€” hierarchy via dot notation
β€’ always use __name__.
Log levels
logger.debug("...")
logger.info("...")
logger.warning("...")
logger.error("...")
logger.critical("...")
Five levels: DEBUG < INFO < WARNING < ERROR < CRITICAL β€” only events at or above set level are emitted.
StreamHandler
h = logging.StreamHandler()
logger.addHandler(h)
Outputs to stderr (default) or any stream β€” standard handler for console output.
FileHandler
h = logging.FileHandler("app.log")
Writes to a file β€” use RotatingFileHandler for size-limited rotation.
RotatingFileHandler
RotatingFileHandler("app.log", maxBytes=1e6, backupCount=3)
Rotates log file when it reaches maxBytes β€” keeps backupCount backups.
Formatter
fmt = Formatter("%(asctime)s - %(name)s - %(message)s")
h.setFormatter(fmt)
Controls log record format β€” standard attributes include asctime, name, levelname, message.
logger.exception()
except Exception:
logger.exception("Error")
Logs at ERROR level plus the full stack trace β€” use inside except blocks.
Propagate
logger.propagate = False
Stops log records bubbling up to parent loggers β€” avoids duplicate output.
dictConfig / fileConfig
logging.config.dictConfig(cfg)
Configures logging from a dict or file β€” preferred for production over basicConfig.
LoggerAdapter
adapter = LoggerAdapter(logger, {"req_id": id})
Adds contextual data to every log call without modifying the message format.

Table 25: Virtual Environments and Packaging

Isolation is the first principle: one project, one environment, so dependency versions never interfere. venv is the built-in option (zero install, always available), while uv (Astral) is a fast Rust-based alternative that manages both environments and Python installations with near-instant dependency resolution.

ToolExampleDescription
venv create
python -m venv .venv
Creates a virtual environment β€” installs pip + copies/symlinks interpreter.
venv activate
source .venv/bin/activate
.venv\Scripts\activate
Activates environment β€” modifies PATH so python/pip point to venv.
pip install
pip install requests
pip install -r requirements.txt
Installs packages β€” -r installs from requirements file.
pip freeze
pip freeze > requirements.txt
Snapshots current installed packages with pinned versions.
uv init
uv init example
Creates a new project with pyproject.toml β€” fast Rust-based tool (Astral).
uv run
uv run script.py
Runs script in an isolated, auto-created environment β€” no manual activation needed.
uv add
uv add requests
Adds dependency to pyproject.toml and updates lockfile atomically.
uv sync
uv sync
Installs dependencies from lockfile β€” reproduces exact environment.
uv python install
uv python install 3.12
Downloads and installs a specific Python version β€” managed alongside venv.
pyproject.toml
[project]
name = "myapp"
requires-python = ">=3.11"
β€’ Modern packaging standard β€” replaces setup.py
β€’ supported by pip, uv, poetry, flit
pyenv
pyenv install 3.11.5
pyenv local 3.11.5
Manages multiple Python versions side-by-side β€” sets .python-version file per directory.
poetry
poetry new project
poetry add requests
Package manager with lockfile and publishing support β€” poetry install reproduces env.

Table 26: Advanced Patterns and Best Practices

These patterns represent idiomatic Python thinking: prefer iteration over indexing, exploit descriptors and __slots__ for performance-critical classes, compose with functools.partial (and Python 3.14's Placeholder for gap filling), and exploit generators for lazy pipelines that process data without materializing everything in memory.

TechniqueExampleDescription
Generator pipeline
evens = (x for x in data if x%2==0)
result = sum(evens)
β€’ Chains generators lazily β€” never materializes intermediate lists
β€’ memory-efficient for large datasets
functools.partial
double = partial(mul, 2)
Pre-fills arguments β€” creates specialized callable from a general function.
functools.Placeholder
f = partial(pow, ..., 2)
Marks positional gaps in partial() that callers must fill β€” ... is the Placeholder (Python 3.14+).
functools.reduce
reduce(lambda a,b: a+b, nums)
Folds a sequence left-to-right into a single value.
Descriptor protocol
def __get__(self, obj, type):
Implements per-attribute access β€” foundation for property, classmethod, staticmethod.
slots
__slots__ = ("x", "y")
Replaces per-instance __dict__ with fixed memory layout β€” reduces RAM and speeds attribute access.
Walrus operator
while chunk := f.read(8192):
β€’ Assignment expression β€” assigns and evaluates in one step
β€’ avoids repeated calls
Positional-only params
def f(a, b, /, c):
β€’ / separator β€” a, b can only be passed by position
β€’ prevents API-breaking renames
Keyword-only params
def f(*, a, b):
* separator β€” a, b must be passed by keyword.
Metaclass
class Meta(type):
def __new__(mcs, ...):
Controls class creation β€” used by ORMs, ABCs, and plugin registries.
init_subclass
def __init_subclass__(cls, **kw):
Hook called when a class is subclassed β€” simpler plugin registration than metaclasses.
Singleton pattern
def __new__(cls):
if not cls._instance:
cls._instance = super().__new__(cls)
return cls._instance
Ensures one instance via __new__ β€” thread-safe version requires a lock.
t-strings (PEP 750)
tmpl = t"Hello {name}"
β€’ Like f-strings but deferred β€” returns a Template object instead of a string
β€’ enables safe HTML/SQL generation (Python 3.14+).
deferred annotations (PEP 749)
def f() -> MyClass:
β€’ Annotations are not evaluated at import time in Python 3.14+ β€” forward references require no quotes
β€’ use annotationlib to evaluate explicitly
concurrent.interpreters
import concurrent.interpreters
New in Python 3.14 (PEP 734) β€” runs code in isolated sub-interpreters with true parallelism via InterpreterPoolExecutor.
Back to Programming Languages
Next Topic: Python Libraries Cheat Sheet

More in Programming Languages

  • PHP Programming Language Cheat Sheet
  • Python Libraries Cheat Sheet
  • Arrays & Strings Cheat Sheet
  • Java Cheat Sheet
  • Object-Oriented Programming (OOP) Cheat Sheet
  • TOML Configuration Format Cheat Sheet
View all 31 topics in Programming Languages

References

Official Documentation

  1. Python 3 Official Documentation
  2. Python 3.14 What's New
  3. Python 3.13 What's New
  4. Python 3.12 What's New
  5. Python 3.11 What's New
  6. Python 3.10 What's New
  7. Python Built-in Types
  8. Python Built-in Functions
  9. Python Built-in Exceptions
  10. Python Data Model
  11. Python Language Reference
  12. Python Expressions
  13. Python Compound Statements (with, if, for, while)
  14. Python Simple Statements
  15. collections β€” Container datatypes
  16. itertools β€” Functions for iterating over iterables
  17. functools β€” Higher-order functions
  18. typing β€” Support for type hints
  19. dataclasses β€” Data Classes
  20. enum β€” Support for enumerations
  21. abc β€” Abstract Base Classes
  22. contextlib β€” Utilities for with-statement contexts
  23. asyncio β€” Asynchronous I/O
  24. asyncio Tasks and Coroutines
  25. asyncio Synchronization Primitives
  26. asyncio Queues
  27. threading β€” Thread-based parallelism
  28. multiprocessing β€” Process-based parallelism
  29. concurrent.futures β€” Launching parallel tasks
  30. concurrent.interpreters β€” Multiple interpreters in the same process
  31. logging β€” Logging facility for Python
  32. logging.handlers β€” Logging handlers
  33. logging.config β€” Logging configuration
  34. re β€” Regular expression operations
  35. pathlib β€” Object-oriented filesystem paths
  36. io β€” Core tools for working with streams
  37. os β€” Miscellaneous operating system interfaces
  38. sys β€” System-specific parameters and functions
  39. venv β€” Creation of virtual environments
  40. pip documentation
  41. annotationlib β€” Introspection of annotations
  42. Python Glossary
  43. Python HOWTOs
  44. Functional Programming HOWTO
  45. Descriptor HOWTO Guide
  46. Sorting HOWTO
  47. Logging HOWTO
  48. Logging Cookbook
  49. Regex HOWTO
  50. Python Packaging User Guide
  51. Writing pyproject.toml
  52. Python Type System Reference

PEPs (Python Enhancement Proposals)

  1. PEP 8 β€” Style Guide for Python Code
  2. PEP 20 β€” The Zen of Python
  3. PEP 257 β€” Docstring Conventions
  4. PEP 484 β€” Type Hints
  5. PEP 526 β€” Syntax for Variable Annotations
  6. PEP 544 β€” Protocols: Structural subtyping
  7. PEP 557 β€” Data Classes
  8. PEP 572 β€” Assignment Expressions (Walrus Operator)
  9. PEP 585 β€” Type Hinting Generics in Standard Collections
  10. PEP 604 β€” Allow X | Y syntax for unions
  11. PEP 634 β€” Structural Pattern Matching
  12. PEP 635 β€” Structural Pattern Matching Motivation
  13. PEP 636 β€” Structural Pattern Matching Tutorial
  14. PEP 646 β€” Variadic Generics
  15. PEP 649 β€” Deferred Evaluation of Annotations
  16. PEP 695 β€” Type Parameter Syntax
  17. PEP 698 β€” Override decorator for static typing
  18. PEP 703 β€” Making the Global Interpreter Lock Optional
  19. PEP 719 β€” Python 3.13 Release Schedule
  20. PEP 729 β€” Typing governance process
  21. PEP 734 β€” Multiple Interpreters in the stdlib
  22. PEP 749 β€” Implementing PEP 649 (Deferred Annotations)
  23. PEP 750 β€” Template strings (t-strings)
  24. PEP 779 β€” Criteria for supported status for free-threaded Python
  25. PEP 3107 β€” Function Annotations

Real Python Tutorials

  1. Real Python β€” Python Type Checking Guide
  2. Real Python β€” Python Data Classes
  3. Real Python β€” Primer on Python Decorators
  4. Real Python β€” Python Metaclasses
  5. Real Python β€” Async IO in Python
  6. Real Python β€” Python Concurrency Guide
  7. Real Python β€” Speed Up Python with Concurrency
  8. Real Python β€” Python Virtual Environments Guide
  9. Real Python β€” Regular Expressions in Python
  10. Real Python β€” Python Logging Guide
  11. Real Python β€” Python Generators Guide
  12. Real Python β€” Python Itertools Guide
  13. Real Python β€” Python Enum Guide
  14. Real Python β€” Python collections Module
  15. Real Python β€” Python functools Module
  16. Real Python β€” Python Context Managers
  17. Real Python β€” Python Scope and LEGB Rule
  18. Real Python β€” Python String Formatting Best Practices
  19. Real Python β€” Structural Pattern Matching in Python 3.10
  20. Real Python β€” Python 3.12 f-strings
  21. Real Python β€” Python Walrus Operator
  22. Real Python β€” Python Annotations
  23. Real Python β€” Python 3.14 Preview

Technical Blogs & Tutorials

  1. Astral uv Documentation
  2. uv CLI Reference
  3. attrs Documentation
  4. Pydantic Documentation
  5. mypy Documentation
  6. pyright Documentation
  7. poetry Documentation
  8. pyenv on GitHub
  9. Ben Hoyt β€” Pattern Matching in Python 3.10
  10. Cloudsmith β€” Python 3.14 Changes
  11. OpenSourceForu β€” Python 3.13 and 3.14 Features
  12. Hynek Schlawack β€” Structlog (Python logging)
  13. Python Speed β€” Profiling Python Programs
  14. Python Tips β€” Generators and Itertools
  15. tonybaloney.github.io β€” Python 3.13 free-threading
  16. Itamar Turner-Trauring β€” Getting full value from Python type hints
  17. Brett Cannon β€” How importlib.resources works
  18. Fluent Python 2nd Edition (O'Reilly)
  19. Effective Python 3rd Edition (Addison-Wesley)
  20. Python Tricks: The Book (Dan Bader)
  21. Python 3 Patterns, Recipes and Idioms (Bruce Eckel)
  22. Twilio Blog β€” Python Concurrency Comparison
  23. Cloudflare Blog β€” Python async patterns
  24. Python Engineer β€” Python 3.12 new features
  25. Corey Schafer Python Tutorials
  26. Martin Heinz β€” Python Project Setup in 2024
  27. Arjan Codes β€” Python Design Patterns
  28. Python Discord Community

GitHub Repositories & Code Examples

  1. CPython source code (GitHub)
  2. Python Type Stubs (typeshed)
  3. mypy (GitHub)
  4. pydantic (GitHub)
  5. attrs (GitHub)
  6. astral-sh/uv (GitHub)
  7. astral-sh/ruff (GitHub)
  8. pyenv (GitHub)
  9. poetry (GitHub)
  10. structlog (GitHub)
  11. pendulum (GitHub)
  12. Rich β€” terminal output library (GitHub)
  13. httpx (GitHub)
  14. aiohttp (GitHub)
  15. Python type_extensions (GitHub)
  16. awesome-python (GitHub)
  17. Python patterns (GitHub)
  18. wtfpython β€” Python quirks explained (GitHub)
  19. 30-seconds-of-python (GitHub)

Video Resources

  1. mCoding β€” Python Concurrency (YouTube)
  2. mCoding β€” Python Type Hints (YouTube)
  3. Corey Schafer β€” Python Decorators (YouTube)
  4. Corey Schafer β€” Python OOP Tutorials (YouTube)
  5. Arjan Codes β€” Python Design Patterns (YouTube)
  6. PyCon US 2024 Talks (YouTube)
  7. PyCon US 2025 (YouTube)
  8. Talk Python to Me Podcast
  9. Python Bytes Podcast
  10. Raymond Hettinger β€” Beyond PEP 8 (PyCon 2015)
  11. David Beazley β€” Python Concurrency from the Ground Up (PyCon 2015)
  12. Brett Slatkin β€” Refactoring Python (PyCon 2016)
  13. James Powell β€” So you want to be a Python expert (PyData 2017)
  14. Ned Batchelder β€” Facts and Myths about Python names and values

Industry Best Practice Guides & Books

  1. Google Python Style Guide
  2. Python Best Practices for Code Quality (Wemake)
  3. Black Code Formatter
  4. isort β€” Import sorting
  5. Ruff β€” Fast Python linter
  6. Bandit β€” Security linter for Python
  7. Safety β€” Dependency security scanner
  8. Python Testing with pytest (Brian Okken)
  9. Clean Code in Python (Mariano Anaya)
  10. Architecture Patterns with Python (Harry Percival & Bob Gregory)
  11. High Performance Python 2nd Edition (O'Reilly)
  12. CPython Internals (Anthony Shaw)
  13. Python Cookbook 3rd Edition (O'Reilly)
  14. Python One-Liners (Mayer)
  15. pytest Documentation
  16. Python unittest Documentation
  17. coverage.py Documentation
  18. pre-commit hooks
  19. OWASP Python Security

Other

  1. Python Weekly Newsletter
  2. PyCoder's Weekly
  3. Import Python Newsletter
  4. Awesome Python β€” curated list of frameworks and tools
  5. Python Package Index (PyPI)
  6. CPython issue tracker (GitHub Issues)
  7. Python Discuss (discuss.python.org)
  8. Stack Overflow β€” Python tag
  9. Python subreddit
  10. learnpython subreddit
  11. Python Wiki
  12. Python Speed Center
  13. Python Benchmarks (pyperformance)
  14. pyperf β€” Python benchmarking toolkit
  15. Jupyter Project Documentation
  16. IPython Documentation
  17. Python REPL improvements (ptpython)
  18. Python Cheat Sheet Community (gto76)
  19. Devhints Python Cheat Sheet
  20. Learn X in Y Minutes β€” Python