Skip to content
Unverified — AI-generated content. Help verify this page

Python Cheat Sheet

Quick reference for Python data structures, comprehensions, generators, standard library essentials, type hints, and common patterns.


Data Structures

Built-in Types

TypeLiteralMutableOrderedDuplicates
list[1, 2, 3]YesYesYes
tuple(1, 2, 3)NoYesYes
set{1, 2, 3}YesNoNo
frozensetfrozenset({1, 2})NoNoNo
dict{"a": 1}YesYes (3.7+)Keys: No
str"hello"NoYesYes
bytesb"hello"NoYesYes
bytearraybytearray(b"hi")YesYesYes

List Operations

python
nums = [3, 1, 4, 1, 5]
nums.append(9)              # Add to end
nums.extend([2, 6])         # Add multiple
nums.insert(0, 0)           # Insert at index
nums.pop()                  # Remove & return last
nums.pop(2)                 # Remove & return at index
nums.remove(1)              # Remove first occurrence
nums.sort()                 # Sort in place
nums.sort(reverse=True)     # Sort descending
sorted(nums)                # Return new sorted list
nums.reverse()              # Reverse in place
nums.index(4)               # First index of value
nums.count(1)               # Count occurrences

Dict Operations

python
d = {"a": 1, "b": 2}
d["c"] = 3                  # Set key
d.get("x", 0)               # Get with default
d.setdefault("x", [])       # Get or set default
d.update({"d": 4, "e": 5})  # Merge dict
d | {"f": 6}                # Merge (3.9+, returns new)
d |= {"f": 6}               # Merge in place (3.9+)
d.pop("a")                  # Remove & return value
d.keys()                    # View of keys
d.values()                  # View of values
d.items()                   # View of (key, value) pairs
del d["b"]                  # Delete key

Set Operations

python
a = {1, 2, 3}
b = {3, 4, 5}
a | b                       # Union: {1, 2, 3, 4, 5}
a & b                       # Intersection: {3}
a - b                       # Difference: {1, 2}
a ^ b                       # Symmetric difference: {1, 2, 4, 5}
a.issubset(b)               # Is a subset of b?
a.issuperset(b)             # Is a superset of b?

Comprehensions & Generators

python
# List comprehension
squares = [x**2 for x in range(10)]

# Filtered comprehension
evens = [x for x in range(20) if x % 2 == 0]

# Dict comprehension
word_len = {w: len(w) for w in ["hello", "world"]}

# Set comprehension
unique_lower = {s.lower() for s in ["Hi", "HI", "hi"]}

# Nested comprehension (flatten)
flat = [x for row in matrix for x in row]

# Generator expression (lazy, memory efficient)
total = sum(x**2 for x in range(1_000_000))

# Generator function
def fibonacci():
    a, b = 0, 1
    while True:
        yield a
        a, b = b, a + b

String Operations

python
s = "Hello, World!"
s.upper()                   # "HELLO, WORLD!"
s.lower()                   # "hello, world!"
s.strip()                   # Remove leading/trailing whitespace
s.split(", ")               # ["Hello", "World!"]
", ".join(["a", "b", "c"])  # "a, b, c"
s.replace("World", "Py")   # "Hello, Py!"
s.startswith("Hello")       # True
s.endswith("!")             # True
s.find("World")             # 7 (index or -1)
s.count("l")                # 3
f"val={42:08d}"             # "val=00000042" (f-string format)
f"{3.14159:.2f}"            # "3.14" (2 decimal places)

Common Standard Library

itertools

python
from itertools import (
    chain, islice, groupby, product,
    combinations, permutations, accumulate, count, cycle, repeat
)

chain([1,2], [3,4])              # 1, 2, 3, 4
islice(range(100), 5, 10)        # 5, 6, 7, 8, 9
groupby(sorted(data), key=fn)    # Group consecutive equal elements
product("AB", "12")              # A1, A2, B1, B2
combinations([1,2,3], 2)         # (1,2), (1,3), (2,3)
permutations([1,2,3], 2)         # (1,2), (1,3), (2,1), ...
accumulate([1,2,3,4])            # 1, 3, 6, 10 (running sum)

collections

python
from collections import (
    defaultdict, Counter, deque, namedtuple, OrderedDict
)

# defaultdict - auto-initialize missing keys
dd = defaultdict(list)
dd["key"].append(1)

# Counter - count occurrences
c = Counter("abracadabra")       # Counter({'a': 5, 'b': 2, ...})
c.most_common(2)                 # [('a', 5), ('b', 2)]

# deque - double-ended queue, O(1) append/pop both ends
dq = deque([1, 2, 3], maxlen=5)
dq.appendleft(0)
dq.rotate(1)                    # [3, 0, 1, 2]

# namedtuple - lightweight immutable class
Point = namedtuple("Point", ["x", "y"])
p = Point(3, 4)
print(p.x, p.y)

pathlib

python
from pathlib import Path

p = Path("/usr/local/bin")
p / "python3"                    # /usr/local/bin/python3
p.exists()                       # True/False
p.is_file()                      # True/False
p.is_dir()                       # True/False
p.name                           # "bin"
p.stem                           # filename without suffix
p.suffix                         # file extension
p.parent                         # /usr/local
p.glob("*.py")                   # Generator of matching files
p.rglob("*.py")                  # Recursive glob
Path.cwd()                       # Current working directory
Path.home()                      # Home directory

# Read/write
Path("file.txt").read_text()
Path("file.txt").write_text("content")
Path("dir").mkdir(parents=True, exist_ok=True)

functools

python
from functools import lru_cache, partial, reduce, wraps

# lru_cache - memoize function results
@lru_cache(maxsize=128)
def fib(n):
    return n if n < 2 else fib(n - 1) + fib(n - 2)

# partial - fix some arguments
from operator import mul
double = partial(mul, 2)         # double(5) -> 10

# reduce - fold iterable
reduce(lambda a, b: a + b, [1, 2, 3, 4])  # 10

Type Hints

Basic Type Hints

python
# Variables
name: str = "Alice"
age: int = 30
scores: list[int] = [90, 85, 92]
lookup: dict[str, int] = {"a": 1}

# Functions
def greet(name: str, excited: bool = False) -> str:
    return f"Hello, {name}{'!' if excited else '.'}"

# Optional (value or None)
def find(key: str) -> str | None:       # 3.10+ syntax
    ...

# Union types
def process(val: int | str) -> None:    # 3.10+ syntax
    ...

Advanced Type Hints

python
from typing import (
    TypeVar, Generic, Protocol, Callable,
    TypeAlias, Literal, TypeGuard, Final
)

# TypeVar for generics
T = TypeVar("T")
def first(items: list[T]) -> T:
    return items[0]

# Protocol (structural subtyping / duck typing)
class Drawable(Protocol):
    def draw(self) -> None: ...

# Callable
Handler = Callable[[str, int], bool]

# Literal
Mode = Literal["r", "w", "a"]

# TypeAlias
Vector: TypeAlias = list[float]

# Final (constant)
MAX_SIZE: Final = 100

Dataclasses

python
from dataclasses import dataclass, field

@dataclass
class User:
    name: str
    email: str
    age: int = 0
    tags: list[str] = field(default_factory=list)

    def greeting(self) -> str:
        return f"Hi, I'm {self.name}"

# Auto-generates __init__, __repr__, __eq__
u = User("Alice", "alice@example.com", 30)

# Frozen (immutable)
@dataclass(frozen=True)
class Point:
    x: float
    y: float

# Slots (less memory, faster attribute access, 3.10+)
@dataclass(slots=True)
class Config:
    host: str
    port: int

Virtual Environments & Package Management

venv (Built-in)

bash
python -m venv .venv              # Create virtual environment
source .venv/bin/activate         # Activate (Linux/macOS)
.venv\Scripts\activate            # Activate (Windows)
deactivate                        # Deactivate
pip install requests              # Install package
pip install -r requirements.txt   # Install from file
pip freeze > requirements.txt     # Export installed packages

pip

CommandDescription
pip install pkgInstall package
pip install pkg==1.2.3Install specific version
pip install -e .Install in editable mode
pip install --upgrade pkgUpgrade package
pip uninstall pkgUninstall package
pip listList installed packages
pip show pkgPackage details
pip checkVerify dependencies

Poetry

CommandDescription
poetry new projectCreate new project
poetry initInitialize in existing directory
poetry add requestsAdd dependency
poetry add --group dev pytestAdd dev dependency
poetry remove pkgRemove dependency
poetry installInstall all dependencies
poetry updateUpdate dependencies
poetry lockRegenerate lock file
poetry run python script.pyRun within virtualenv
poetry shellActivate virtualenv shell
poetry buildBuild package
poetry publishPublish to PyPI

uv (Fast Package Manager)

CommandDescription
uv venvCreate virtual environment
uv pip install requestsInstall package (fast)
uv pip compile requirements.inCompile lock file
uv pip sync requirements.txtSync environment
uv run script.pyRun script with deps

Common Patterns

Context Managers

python
# Using built-in
with open("file.txt", "r") as f:
    content = f.read()

# Custom context manager (class)
class Timer:
    def __enter__(self):
        self.start = time.perf_counter()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.elapsed = time.perf_counter() - self.start
        return False  # Do not suppress exceptions

with Timer() as t:
    do_work()
print(f"Took {t.elapsed:.3f}s")

# Context manager with contextlib
from contextlib import contextmanager

@contextmanager
def temp_dir():
    path = tempfile.mkdtemp()
    try:
        yield path
    finally:
        shutil.rmtree(path)

Decorators

python
import functools

# Basic decorator
def log_calls(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        print(f"Calling {func.__name__}")
        result = func(*args, **kwargs)
        print(f"{func.__name__} returned {result}")
        return result
    return wrapper

@log_calls
def add(a, b):
    return a + b

# Decorator with arguments
def retry(max_attempts=3, delay=1.0):
    def decorator(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            for attempt in range(max_attempts):
                try:
                    return func(*args, **kwargs)
                except Exception:
                    if attempt == max_attempts - 1:
                        raise
                    time.sleep(delay)
        return wrapper
    return decorator

@retry(max_attempts=5, delay=2.0)
def fetch_data(url):
    ...

Exception Handling

python
# Basic try/except
try:
    result = risky_operation()
except ValueError as e:
    print(f"Bad value: {e}")
except (TypeError, KeyError):
    print("Type or key error")
except Exception as e:
    print(f"Unexpected: {e}")
    raise  # Re-raise
else:
    print("No exception occurred")
finally:
    cleanup()

# Custom exception
class AppError(Exception):
    def __init__(self, message: str, code: int):
        super().__init__(message)
        self.code = code

Useful One-Liners

python
# Flatten nested list
flat = [x for sub in nested for x in sub]

# Transpose matrix
transposed = list(zip(*matrix))

# Remove duplicates preserving order
unique = list(dict.fromkeys(items))

# Chunk a list
chunks = [lst[i:i+n] for i in range(0, len(lst), n)]

# Merge dicts
merged = {**d1, **d2}                # 3.5+
merged = d1 | d2                     # 3.9+

# Ternary expression
result = "yes" if condition else "no"

# Walrus operator (3.8+)
if (n := len(data)) > 10:
    print(f"Too long: {n}")

# Unpack with star
first, *middle, last = [1, 2, 3, 4, 5]

# Enumerate with start index
for i, val in enumerate(items, start=1):
    print(f"{i}. {val}")

When to Use X vs Y

DecisionChoice AChoice BUse A WhenUse B When
SequencelisttupleMutable, variable sizeImmutable, fixed structure
MappingdictdefaultdictExplicit key initAuto-initialize missing keys
IterationList compGenerator exprNeed all results, small dataLazy eval, large data
Data classdataclassnamedtupleMethods, mutability, defaultsSimple immutable records
ConcurrencythreadingasyncioI/O-bound, legacy codeI/O-bound, modern async
ConcurrencymultiprocessingthreadingCPU-bound (bypass GIL)I/O-bound
Pkg mgmtpip + venvpoetrySimple projectsComplex deps, publishing
String fmtf-string.format()Simple interpolation (3.6+)Dynamic format strings

Test Yourself
  1. What method returns a value from a dict with a default if the key is missing?d.get("key", default)

  2. How do you merge two dicts in Python 3.9+?d1 | d2

  3. What module provides defaultdict, Counter, and deque?collections

  4. How do you create a generator expression that sums squares lazily?sum(x**2 for x in range(n))

  5. What decorator memoizes function results with an LRU cache?@lru_cache(maxsize=128) from functools

  6. How do you remove duplicates from a list while preserving order?list(dict.fromkeys(items))

  7. What is the walrus operator and when was it introduced?:= (Python 3.8+), it assigns a value inside an expression: if (n := len(data)) > 10:

  8. How do you create an immutable dataclass?@dataclass(frozen=True)

  9. What method on pathlib.Path recursively finds all Python files?Path(".").rglob("*.py")

  10. What is the difference between list and tuple?list is mutable and variable-size; tuple is immutable and fixed-structure.

Common Gotchas

  • Mutable default arguments. def f(items=[]) shares the same list across all calls. Use def f(items=None) with items = items or [].
  • Modifying a list while iterating over it. This skips elements or raises errors. Iterate over a copy or use a comprehension to filter.
  • Confusing is with ==. is checks identity (same object), == checks equality (same value). Use is only for None, True, False.
  • Using except Exception without re-raising. Silently swallowing exceptions hides bugs. Always log or re-raise.

One-Liner Summary

Python is the Swiss Army knife of programming -- expressive syntax, batteries-included stdlib, and a massive ecosystem make it the go-to language for scripting, data science, web backends, and automation.

"What I cannot create, I do not understand." — Richard Feynman