Python Reference
|
This section documents the current Python release line as published at the official Python documentation, which is the reference these pages are written and verified against. No specific patch version is pinned. This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. This section’s bibliography lists the reference material consulted while preparing these pages. |
Welcome to the Python reference. Python is a high-level, dynamically-typed, multi-paradigm language — it supports procedural, object-oriented, and functional styles, is interpreted (compiled to bytecode and run on the Python Virtual Machine), and favours readable, indentation-based syntax over explicit braces. This section documents the current Python release line, framework-agnostic, written and verified against the official Python documentation.
If you are new to Python, read Getting Started with Python first, then Lexical Structure and Style and Variables and Dynamic Typing, followed by Control Flow — from there the core-language pages build on each other through functions, iteration, modules, files, and exceptions, and the later pages cover object-oriented Python, modern standard-library features, and testing.
What’s covered
Getting started
-
Getting Started with Python — installing Python, the interpreter (script mode vs. the interactive REPL), editors and IDEs, and the source-to-bytecode-to-PVM execution model.
Core language
-
Lexical Structure and Style — indentation-based blocks, comments and statement separators, PEP 8 naming and style, and the Zen of Python.
-
Variables and Dynamic Typing — names bound to objects, mutability, shared references and
isvs.==, and the built-in type hierarchy. -
Numbers —
int/float/complex, numeric operators,decimal/fractionsfor exact arithmetic, and the built-in numeric tools. -
Strings and Text — string literals and slicing, common string methods, f-strings and other formatting, and
strvs.bytes. -
Collections — lists, tuples, sets, and dictionaries, their comprehensions, and when to choose each.
-
Control Flow —
if/elif/elseand truthiness,while/forloops, and thematchstatement. -
Functions — parameters and return values,
args/*kwargsand positional-/keyword-only markers, the LEGB scope rule, andlambda. -
Iterators, Generators and Comprehensions — the iterator protocol, generator functions and expressions, and comprehensions revisited as sugar over iteration.
-
Modules and Packages —
importforms, thename == "main"pattern, packages, and the module search path. -
Files and Context Managers —
open()and thewithstatement, andpathlib.Pathas the modern alternative toos.path. -
Exceptions —
try/except/else/finally, exception chaining, custom exception hierarchies, and exception groups.
Object-oriented programming
-
Classes and Objects —
classbasics, instance vs. class attributes,@staticmethod/@classmethod, and inheritance withsuper(). -
Operator Overloading — the common dunder methods for display, comparison, container behaviour, calling, context management, and arithmetic.
-
Advanced OOP Design — composition vs. inheritance, mixins and multiple inheritance, the Method Resolution Order, and delegation.
-
Managed Attributes —
@property, the descriptor protocol, andgetattrvs.getattribute. -
Decorators and Metaclasses — function decorators, decorator factories and stacking, and a brief, cautious look at metaclasses.
Modern Python & standard library
-
Type Hints — basic annotations, generics and the modern
X | Nonesyntax,TypeVar/Protocol, and static checkers. -
Dataclasses and Enums —
@dataclassand its generated methods, andenum.Enum/IntEnum/Flag. -
Standard Library Tour — filesystem/OS, data, math and random, and CLI/observability modules.
-
Concurrency and Async —
threading,multiprocessingandconcurrent.futures, andasyncio. -
Virtual Environments and Packaging —
venv,pip, andpyproject.toml. -
Debugging and Tooling —
pdb,timeit/cProfile, and linters and formatters. -
Docstrings — what docstrings are, common style conventions (Google, NumPy, reST, Epytext), and generating static documentation sites with Sphinx, pdoc, and MkDocs.
Testing & reference
-
Unit Testing with pytest & Mocking — test discovery, fixtures, parametrization, and mocking with
unittest.mockandmonkeypatch. -
Cheat Sheet (PDF) — a single-page, printable summary of everything in this section, with a downloadable PDF.
Bibliography
-
docs.python.org/3 — the official Python 3 documentation, the source every page in this section is written and verified against; see in particular the tutorial, the language reference, the library reference, and the HOWTOs.
-
python.org — downloads, community, and the documentation hub.
-
PEP 8 — the official style guide, for the lexical structure/style page.
-
pytest’s official documentation — the primary source for the unit-testing page.
-
Lee, Kent D. Python Programming Fundamentals, 2nd ed. Springer (Undergraduate Topics in Computer Science), 2015 (ISBN 978-1-4471-6641-2 print / 978-1-4471-6642-9 eBook). Consulted as part of the bibliography for this section (predates the current Python release line; not the primary or main reference) — see the publisher’s book page and springer.com.
-
Matthes, Eric. Python Crash Course, 3rd ed. No Starch Press, 2023. ISBN-13: 978-1-7185-0270-3 (print) / 978-1-7185-0271-0 (ebook). Consulted as part of the bibliography for this section (predates the current Python release line; not the primary or main reference) — see the publisher’s book page and nostarch.com.
-
Lutz, Mark. Learning Python, 4th ed. O’Reilly Media, 2009. ISBN 978-0-596-15806-4. Consulted as part of the bibliography for this section (predates Python 3.1; no coverage of f-strings, type hints,
pathlib,dataclasses,async/await, thematchstatement, orpytest/mocking) — see the publisher’s book page and oreilly.com.