Python Quick Docs
A fast, copy/paste-first Python quick reference focused on what you need most while building: built-ins, data types, core syntax, and the most-used parts of the standard library. Searchable, dark mode, mobile-friendly. Covers Pandas, NumPy, common patterns, practice problems, and a comprehensive cheatsheet.
Built-in Functions (Most Used)
A high-signal set of built-ins you reach for constantly: print(), len(), sum(), min(), max(), abs(), round(), range(), enumerate(), zip(), sorted(), any(), all(), isinstance(), dir(), help(), type(), id(), map(), filter(), open(), input(), iter(), next(), super(), property, classmethod, staticmethod, getattr(), setattr(), hasattr(), repr(), hash(), callable(), int(), float(), str(), bool(), list(), dict(), set(), tuple().
print()
print("a", "b", sep=", ") # a, b
print("no newline", end="") # end controls trailing newline
print({"a": 1}, flush=True) # flush for progress logs / pipes
len()
len([1, 2, 3]) # 3
len("hello") # 5
len({"a": 1}) # 1 (dict length is number of keys)
sum() / min() / max() / abs()
nums = [-3, 5, 2]
sum(nums) # 4
min(nums) # -3
max(nums) # 5
abs(-3) # 3
range()
for i in range(3): # 0,1,2 (no list created)
...
for i in range(10, 0, -1): # 10..1
...
enumerate()
for i, x in enumerate(items):
print(i, x)
for line_no, line in enumerate(open("file.txt"), start=1):
...
zip()
for name, score in zip(names, scores): # stops at shortest
...
pairs = list(zip(names, scores)) # [("a",10), ("b",20)]
sorted() — with reverse, multi-key, and dict sorting
sorted(xs, key=lambda d: d["age"])
sorted(xs, key=lambda d: (d["age"], d["n"])) # multi-key sort
sorted(xs, key=lambda d: d["age"], reverse=True) # descending
# sorted() vs list.sort()
ys = sorted(xs) # new list
xs.sort() # in-place (returns None)
# Reverse a sequence
list(reversed(xs)) # [3, 2, 1] (iterator)
xs[::-1] # [3, 2, 1] (copy)
# Sort a dict by value
sorted(counts.items(), key=lambda kv: kv[1], reverse=True)
any() / all()
any(x < 0 for x in nums) # True if any match
all(x >= 0 for x in nums) # True if all match
isinstance()
isinstance(x, int)
isinstance(x, (str, bytes))
map() / filter()
squares = [x * x for x in nums] # comprehension (preferred)
out = list(map(normalize, raw_strings)) # map with named function
round() — banker's rounding
round(3.14159, 2) # 3.14
round(2.5) # 2 (banker's rounding!)
open() / input()
with open("data.txt", encoding="utf-8") as f:
for line in f: print(line.strip())
name = input("Enter your name: ")
iter() / next() — iterator protocol
it = iter([10, 20, 30])
next(it) # 10
next(it, "done") # default prevents StopIteration
super() — inheritance
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
property / classmethod / staticmethod
@property
def radius(self): return self._radius
@classmethod
def from_diameter(cls, d): return cls(d / 2)
@staticmethod
def is_valid(r): return r >= 0
getattr / setattr / hasattr
getattr(obj, "name", "default")
setattr(obj, "name", "Ada")
hasattr(obj, "name")
Type constructors
int("42"); float("3.14"); str(42); bool(0)
list("abc"); tuple([1,2]); set([1,2,2]); dict([("a",1)])
Data Types
Idiomatic patterns for the core built-in types: str, list, tuple, dict, set, bytes, and dataclasses. Includes encode/decode, isdigit/isalpha/isalnum, count, index, partition, title, capitalize.
str — split, join, strip, replace, find, startswith, endswith, lower, upper, f-strings
s.strip() # "hello"
"-".join(["a", "b"]) # "a-b"
f"{name=} {age=}" # debug output
s.split(",") # ["a", "b", "c"]
"a-b-c".replace("-", "_") # "a_b_c"
"hello".find("ll") # 2
"report.csv".endswith(".csv") # True
"MiXeD".lower() # "mixed"
list — append, extend, pop, remove, slicing, sort, comprehensions
xs.append(4)
xs.extend([5, 6])
xs.pop() # removes last
xs.pop(0) # removes by index
xs.remove(2) # removes first matching value
xs[1:5:2] # slicing with step
evens = [x for x in xs if x % 2 == 0] # comprehension
tuple — immutability, unpacking, swap
a, b = b, a # swap (must-know)
head, *mid, tail = [1, 2, 3, 4]
from collections import namedtuple
User = namedtuple("User", "id name")
dict — get, setdefault, merge, keys, values, items, frequency counting
d.get("a", 0) # safe default
d.setdefault("items", []).append(1)
merged = d | d2 # Python 3.9+
squares = {x: x * x for x in range(5)}
# Frequency counting
counts[x] = counts.get(x, 0) + 1
set — add, remove, discard, intersection, union, difference, membership
2 in s # fast membership
unique = set(xs) # dedupe
a & b # intersection
a | b # union
a - b # difference
s.add("a")
s.discard("a") # safe: does nothing if missing
dataclasses
from dataclasses import dataclass
@dataclass(frozen=True, slots=True)
class Point:
x: float
y: float
Syntax & Core Language
Modern Python patterns: control flow, comprehensions, unpacking, f-strings, context managers, exceptions (custom exceptions, chaining), functions, scope, decorators, generators (yield, yield from, send), walrus operator (:=), dunder methods (__repr__, __str__, __eq__, __hash__), __slots__, abstract base classes (abc), f-string format specs, pattern matching, typing, and async.
Control flow — for, while, break, continue, for-else, ternary
for x in [1, 2, 3]:
print(x)
# for-else: else runs only if the loop did NOT break
for x in data:
if cond(x):
break
else:
found = None
# ternary
label = "big" if x > 10 else "small"
Comprehensions — list, dict, set, generator
squares = [x * x for x in range(10)]
by_id = {u.id: u for u in users}
seen = {normalize(s) for s in raw}
total = sum(x * x for x in nums) # generator (lazy)
Functions & scope — def, *args, **kwargs, return vs print, early returns, global, nonlocal
def f(x, y=0, *args, **kwargs):
...
def first_match(data):
for x in data:
if cond(x):
return x
return None
Gotchas — mutable defaults, == vs is
# Bad: default list is shared across calls
def add_item(x, items=[]):
items.append(x)
# Good
def add_item_safe(x, items=None):
if items is None:
items = []
a == b # True (same value)
a is b # False (different objects)
x is None # identity check for None is correct
Context managers (with)
with path.open("w", encoding="utf-8") as f:
f.write("hello\n")
Exceptions
try:
value = int(user_input)
except ValueError:
value = 0
Pattern matching (match/case)
match msg:
case {"type": "ping"}:
return {"type": "pong"}
case _:
raise ValueError("unknown")
Typing (practical)
def mean(xs: Iterable[float]) -> float:
xs = list(xs)
return sum(xs) / len(xs)
Custom exceptions & chaining
class AppError(Exception): ...
class NotFoundError(AppError):
def __init__(self, resource, id):
super().__init__(f"{resource} {id} not found")
raise ValidationError("bad") from original_error
Decorators — writing your own
import functools
def timer(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
start = time.perf_counter()
result = func(*args, **kwargs)
print(f"{func.__name__} took {time.perf_counter()-start:.4f}s")
return result
return wrapper
Generators — yield, yield from, send
def countdown(n):
while n > 0:
yield n; n -= 1
def flatten(nested):
for item in nested:
if isinstance(item, list):
yield from flatten(item)
else:
yield item
Walrus operator (:=)
if (n := len(data)) > 10:
print(f"processing {n} items")
results = [y for x in data if (y := transform(x)) is not None]
Dunder methods — __repr__, __str__, __eq__, __hash__, __add__
def __repr__(self): return f"Point({self.x!r}, {self.y!r})"
def __eq__(self, other): return (self.x, self.y) == (other.x, other.y)
def __hash__(self): return hash((self.x, self.y))
__slots__ — memory optimization
class LightObj:
__slots__ = ("x", "y")
def __init__(self, x, y): self.x = x; self.y = y
Abstract Base Classes (abc)
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self) -> float: ...
f-string format specs
f"{3.14:.2f}" # "3.14"
f"{1000000:,}" # "1,000,000"
f"{0.85:.1%}" # "85.0%"
f"{42:05d}" # "00042"
Async
import asyncio
async def fetch_one(url: str) -> str:
await asyncio.sleep(0.1)
return url
async def main():
results = await asyncio.gather(*(fetch_one(u) for u in urls))
Standard Library (Most Used)
These modules solve a huge portion of real-world work without extra dependencies: pathlib, datetime, json, re, itertools, functools (lru_cache, partial, reduce), collections, subprocess, logging, argparse, concurrent.futures, sqlite3, bisect, heapq, os, sys, math, random, copy/deepcopy, enum, typing, shutil, tempfile, hashlib, base64, threading, csv.
pathlib — filesystem paths and file I/O
from pathlib import Path
p = Path("data") / "input.txt"
text = p.read_text(encoding="utf-8")
for file in Path(".").glob("**/*.py"):
...
datetime — timezone-aware timestamps
from datetime import datetime, timezone
now_utc = datetime.now(timezone.utc)
dt = datetime.fromisoformat("2026-02-08T12:34:56+00:00")
json — serialize and deserialize data
import json
s = json.dumps(data, indent=2, ensure_ascii=False)
data2 = json.loads(s)
re — regex pattern matching
import re
ids = re.findall(r"#(\d+)", text)
itertools — lazy iteration pipelines
import itertools as it
list(it.chain([0], xs, [4]))
for k, group in it.groupby(items, key=lambda d: d["k"]):
...
functools — caching and partial application
from functools import lru_cache
@lru_cache(maxsize=256)
def fib(n): ...
collections — Counter, defaultdict, deque
from collections import Counter, defaultdict, deque
counts = Counter(["a", "b", "a"])
groups = defaultdict(list)
q = deque([1, 2, 3])
subprocess — run external commands
import subprocess
res = subprocess.run(["python", "--version"], check=True, capture_output=True, text=True)
logging — application logs
import logging
logging.basicConfig(level=logging.INFO)
log = logging.getLogger("myapp")
log.info("user=%s", user_id)
argparse — CLI tools
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--count", type=int, default=1)
concurrent.futures — I/O concurrency
from concurrent.futures import ThreadPoolExecutor
with ThreadPoolExecutor(max_workers=16) as ex:
results = list(ex.map(fetch, urls))
sqlite3 — embedded database
import sqlite3
with sqlite3.connect("app.db") as con:
con.execute("CREATE TABLE IF NOT EXISTS items (id INTEGER PRIMARY KEY, name TEXT)")
bisect — binary search on sorted lists
import bisect
xs = [10, 20, 30, 40, 50]
bisect.bisect_left(xs, 30) # 2
bisect.insort(xs, 25) # [10, 20, 25, 30, 40, 50]
heapq — priority queues
import heapq
heapq.heapify(xs)
heapq.heappush(xs, val)
smallest = heapq.heappop(xs)
heapq.nlargest(3, xs)
os — environment variables and OS operations
import os
os.getenv("HOME")
os.environ.get("DEBUG", "0")
os.makedirs("a/b/c", exist_ok=True)
sys — system parameters
import sys
sys.argv; sys.version; sys.platform
sys.exit(1); sys.path
math — mathematical functions
import math
math.sqrt(16); math.ceil(3.2); math.floor(3.7)
math.gcd(12, 8); math.factorial(5); math.pi; math.inf
random — sampling and shuffling
import random
random.choice(["a", "b"]); random.sample(xs, k=3)
random.shuffle(xs); random.randint(1, 10); random.seed(42)
copy / deepcopy
import copy
shallow = copy.copy(original)
deep = copy.deepcopy(original)
enum — named constants
from enum import Enum, auto
class Status(Enum):
PENDING = auto()
ACTIVE = auto()
typing — practical type hints
from typing import Optional, Callable, TypedDict
def process(items: list[str]) -> dict[str, int]: ...
def maybe(x: int | None) -> str: ...
shutil — high-level file operations
import shutil
shutil.copytree("src", "dst"); shutil.rmtree("dir"); shutil.move("a", "b")
tempfile — temporary files and directories
import tempfile
with tempfile.TemporaryDirectory() as tmpdir: ...
hashlib / base64
import hashlib, base64
hashlib.sha256(b"hello").hexdigest()
base64.b64encode(b"hello")
threading — basic patterns
import threading
t = threading.Thread(target=worker, args=("t1",))
t.start(); t.join()
lock = threading.Lock()
with lock: shared_counter += 1
Most-Used Recipes
Short, production-shaped snippets for common problems: dedupe, group by key, count frequencies, chunk a list, sliding window, retry with backoff, file I/O, CSV, timeit, unittest.
Dedupe while preserving order
list(dict.fromkeys(xs))
Group by key
from collections import defaultdict
out = defaultdict(list)
for it in items:
out[key_fn(it)].append(it)
Count frequencies
from collections import Counter
counts = Counter(words)
top10 = counts.most_common(10)
# dict pattern
counts = {}
for x in data:
counts[x] = counts.get(x, 0) + 1
Sliding window (two pointers)
l = 0
for r in range(len(arr)):
# expand right, shrink left when invalid
...
Retry with exponential backoff
import time
def retry(fn, attempts=5, base_delay=0.2):
for i in range(attempts):
try:
return fn()
except Exception:
if i == attempts - 1:
raise
time.sleep(base_delay * (2 ** i))
Practice Problems (Copy/Paste-Ready)
Clean, idiomatic solutions to common real-world problems with signatures, optimal approaches, complexity analysis, and pitfalls.
1) Longest substring without repeating characters
Technique: sliding window. Time: O(n). Space: O(min(n, charset size)).
def longest_unique_substring(s: str) -> int:
left = 0
seen = set()
best = 0
for right, ch in enumerate(s):
while ch in seen:
seen.remove(s[left])
left += 1
seen.add(ch)
best = max(best, right - left + 1)
return best
2) Top K frequent elements
Technique: Counter + heap or bucket sort. Time: O(n log k) heap, O(n) bucket.
from collections import Counter
import heapq
def top_k_frequent(nums, k):
counts = Counter(nums)
return [x for x, _ in heapq.nlargest(k, counts.items(), key=lambda kv: kv[1])]
3) Simple rate limiter (rolling window, out-of-order timestamps)
Technique: per-user sorted timestamps + bisect. Query: O(log n). Insert: O(n) worst-case.
import bisect
class RateLimiter:
def __init__(self, max_requests, window_seconds):
self.max_requests = max_requests
self.window_seconds = window_seconds
self._user_ts = {}
def allow_request(self, user_id, timestamp):
ts = self._user_ts.setdefault(user_id, [])
bisect.insort(ts, timestamp)
start = timestamp - self.window_seconds + 1
left = bisect.bisect_left(ts, start)
right = bisect.bisect_right(ts, timestamp)
return (right - left) <= self.max_requests
4) Shortest unique prefix per word
Technique: prefix frequency counting. Time: O(n*m). Space: O(n*m).
def shortest_unique_prefixes(words):
freq = {}
for w in words:
p = ""
for ch in w:
p += ch
freq[p] = freq.get(p, 0) + 1
out = []
for w in words:
p = ""
for ch in w:
p += ch
if freq[p] == 1:
out.append(p)
break
else:
out.append(w)
return out
5) Group items by key (without defaultdict)
Uses setdefault pattern. Time: O(n). Space: O(n).
def group_actions(logs):
out = {}
for user, action in logs:
out.setdefault(user, []).append(action)
return out
6) First non-repeating character
Uses Counter then scan original string. Time: O(n). Space: O(charset).
from collections import Counter
def first_unique_char(s):
counts = Counter(s)
for ch in s:
if counts[ch] == 1:
return ch
return None
Pandas Checklist
High-frequency Pandas operations: DataFrame basics, column selection, row filtering, new columns, apply, missing data, groupby, sorting, merge/join, value counts, datetime, pivot.
Basics
import pandas as pd
df = pd.DataFrame({"age": [20, 35], "city": ["NY", "SF"]})
df.head(); df.tail(); df.shape; df.columns; df.dtypes; df.info()
Row filtering
df[df["age"] > 30]
df[(df["age"] > 30) & (df["city"] == "NY")]
GroupBy
df.groupby("city")["salary"].mean()
df.groupby("city").agg(avg_salary=("salary", "mean"), count=("salary", "size"))
Merge / join
pd.merge(df1, df2, on="id", how="inner") # inner, left, right, outer
Sorting and Top-N
df.sort_values("salary", ascending=False)
df.nlargest(3, "salary")
Value counts and datetime
df["city"].value_counts()
df["city"].nunique()
df["date"] = pd.to_datetime(df["date"])
df["year"] = df["date"].dt.year
NumPy Checklist
NumPy core operations: ndarray basics, array creation, reshape, indexing, boolean masks, vectorization, broadcasting, aggregations, concatenate, random, linear algebra.
Basics and array creation
import numpy as np
a = np.array([1, 2, 3])
np.zeros((2, 3)); np.ones((2, 3)); np.arange(0, 10, 2); np.linspace(0, 1, 5); np.eye(3)
Shape, reshape, and indexing
b = a.reshape(3, 4)
b.T # transpose
m[0, 1] # row 0, col 1
m[:, 0] # first column
Boolean masks and vectorization
x[x % 2 == 0] # filter evens
x + y # elementwise add
np.sqrt(y) # elementwise sqrt
Broadcasting and aggregations
m + v # v broadcasts across rows
m.sum(axis=0) # per-column sums
m.mean(axis=1) # per-row means
Random and linear algebra
rng = np.random.default_rng(0)
rng.integers(0, 10, size=5)
A @ B # matrix multiplication
np.linalg.det(A) # determinant
Python Cheatsheet
A dense, scan-friendly quick reference covering core data types, control flow, functions, strings, comprehensions, built-ins, common patterns, error handling, collections module, NumPy, Pandas, performance awareness, and common pitfalls.
Complexity quick reference
- list.append(x) — amortized O(1)
- x in list — O(n)
- x in set / x in dict — average O(1)
- sorted() / list.sort() — O(n log n)
Core data types (lists, tuples, sets, dicts)
# Lists
a.append(x); a.extend([x, y]); a.pop(); a[i:j:k]; sorted(a); a.sort()
# Tuples
a, b = b, a # swap
# Sets
s.add(x); x in s; s1 | s2; s1 & s2
# Dicts
d.get(k, default); d.setdefault(k, []); d.items()
d[x] = d.get(x, 0) + 1 # frequency pattern
Control flow
for x in data: ...
while condition: ...
break; continue
# for-else: else runs only if loop did NOT break
Functions
def f(x, y=0): return x + y
# prefer return over print
# early returns > nested conditionals
Strings
s.split(","); ",".join(lst); s.strip()
s.replace("a", "b"); s.lower(); s.startswith("pre"); s.endswith("txt")
Comprehensions
[x for x in arr if cond(x)]
{k: v for k, v in pairs}
{x for x in arr}
Common patterns
# Grouping
d.setdefault(k, []).append(v)
# First match
for x in data:
if cond(x): return x
# Dedup
list(set(arr))
Performance awareness
- in set/dict is fast; in list is slow
- Vectorized ops beat Python loops for numeric workloads
- Avoid row-by-row logic in tables when vectorized ops / groupby work
Common pitfalls
a = [[]] * 3 # shared reference — use [[] for _ in range(3)]
def f(x=[]): # mutable default — use None then check
== # value comparison
is # identity comparison (use for None)