Python Reference
Free reference guide: Python Reference
About Python Reference
The Python Reference is a structured, searchable cheat sheet covering the full breadth of the Python programming language. It spans eight core categories — Basics, Data Types, Control Flow, Functions, Classes, Modules, File I/O, and Error Handling — giving developers a single place to look up any syntax or built-in pattern they need. Each entry includes a concise description and a runnable code example, making it practical for everyday development rather than just theoretical reading.
Python is used across web development, data science, automation, machine learning, and system scripting, meaning its practitioners range from beginner coders learning `print()` and `if/else` to senior engineers working with `async/await`, `dataclasses`, decorators, generators, and type hints. This reference is organized to serve all experience levels: beginners can browse the Basics and Data Types sections, while intermediate and advanced users can quickly locate entries like `walrus operator :=`, `match/case` (Python 3.10+), `@property`, or context managers without scrolling through documentation pages.
The reference is organized into eight categories that mirror how Python developers actually think about their code. The Modules section covers frequently needed standard library modules — `os`, `pathlib`, `json`, `collections`, `itertools`, and `re` — with ready-to-paste examples. The Functions section includes comprehensions, lambdas, map/filter/reduce, generators, and the full decorator pattern. Whether you are writing a quick script or architecting a production application, this reference gives you instant access to the patterns and syntax you need.
Key Features
- Covers 8 categories: Basics, Data Types, Control Flow, Functions, Classes, Modules, File I/O, Error Handling
- Includes Python 3.6+ f-strings, 3.7+ dataclasses, 3.8+ walrus operator, and 3.10+ match/case
- Every entry has a description and a real, runnable code example
- Filter by category or use the search bar to find any built-in, keyword, or syntax instantly
- Covers async/await, generators, decorators, type hints, and advanced OOP patterns
- Standard library modules (os, pathlib, json, collections, itertools, re) with practical examples
- Dark mode support and fully responsive layout for desktop, tablet, and mobile
- 100% client-side — no sign-up, no download, no data sent to any server
Frequently Asked Questions
What categories does this Python reference cover?
The reference covers eight categories: Basics (print, input, f-strings, type, isinstance, import), Data Types (int, float, str, list, tuple, dict, set, bool, None), Control Flow (if/elif/else, for, while, break/continue, match/case), Functions (def, *args/**kwargs, lambda, comprehensions, decorators, generators, type hints), Classes (class, inheritance, @property, dataclass, magic methods), Modules (os, pathlib, json, collections, itertools, re), File I/O (open/with, readlines, csv, context managers), and Error Handling (try/except, raise, custom exceptions, async/await).
Does this reference cover modern Python 3 syntax?
Yes. The reference explicitly covers Python 3.6+ f-strings, Python 3.7+ dataclasses, Python 3.8+ walrus operator (:=), Python 3.10+ match/case pattern matching, and Python 3.5+ type hints. All examples are written for Python 3 and will not work with Python 2.
How do I use f-strings in Python?
F-strings (formatted string literals) are introduced with a leading f before the quote. Expressions inside curly braces are evaluated at runtime: `f"Hello, {name}!"`. You can also apply format specifiers: `f"{3.14159:.2f}"` produces "3.14", and `f"{42:08b}"` produces "00101010". F-strings are available in Python 3.6 and later.
What is the difference between a list and a tuple in Python?
Both are ordered sequences, but lists are mutable (you can append, insert, remove, and sort elements) while tuples are immutable (once created, their elements cannot be changed). Tuples support unpacking — `x, y = (3, 4)` — and are commonly used for fixed-size data like coordinates. Because tuples are immutable, they are slightly faster and can be used as dictionary keys, while lists cannot.
How do decorators work in Python?
A decorator is a function that takes another function as an argument, wraps it with additional behavior, and returns the wrapper. You apply it with the `@decorator` syntax above a function definition. For example, a `@timer` decorator can measure how long a function takes by recording the time before and after calling the original function. Decorators are widely used for logging, authentication checks, caching, and retry logic.
What is the walrus operator (:=) in Python?
The walrus operator (:=), introduced in Python 3.8, is an assignment expression that assigns a value to a variable and returns that value in the same expression. It is useful inside `if`, `while`, and comprehension conditions to avoid evaluating the same expression twice. For example, `if (n := len(items)) > 10: print(n)` computes the length once and uses it both for the condition and in the body.
What is the difference between a generator and a list comprehension?
A list comprehension (`[x**2 for x in range(10)]`) evaluates all elements immediately and stores them in memory as a list. A generator expression (`(x**2 for x in range(10))`) produces values lazily one at a time, which is much more memory-efficient for large sequences. Generator functions using `yield` work the same way — they pause execution and yield one value per `next()` call. Use generators when you do not need all results at once.
How does Python handle exceptions?
Python uses `try/except` blocks to catch exceptions. You can catch specific exception types like `ZeroDivisionError` or `FileNotFoundError`, or use a broad `except Exception as e` clause. The optional `else` block runs if no exception was raised, and `finally` always runs regardless of whether an exception occurred. You can raise your own exceptions with `raise ValueError("message")` and define custom exception classes by subclassing `Exception`.