thecodex.expert · The Codex Family of Knowledge
Language Reference

Python

General-purpose, readable, interpreted — with batteries included and the most active data science ecosystem on Earth.

Language Reference Python 3.13 · October 2024 Dynamic typing Last verified:
New to Python? This is the reference encyclopedia — dip in anytime. To learn step by step, start the Complete Course →
Canonical Definition

Python is a general-purpose, high-level, interpreted, dynamically typed, multi-paradigm programming language designed for readability, with significant whitespace as block delimiters, a comprehensive standard library, and an extensive ecosystem centred on data science, machine learning, and automation.

📑 Python Reference — All Topics

Variables & Types

Built-in types, mutability, type system, operators.

String Methods

40+ methods: case, strip, split, join, find, replace, validation.

match / case

Structural pattern matching, destructuring, class patterns, guards.

Control Flow & Loops

if/elif/else, for, while, range(), match statements.

Comprehensions

List, dict, set, and generator comprehensions.

Iterators & Iterables

iterable vs iterator, the protocol, lazy evaluation, itertools.

Functions & Scope

def, *args/**kwargs, closures, decorators, LEGB.

Lambda Functions

anonymous functions, sort keys, map/filter, late binding.

Closures

Captured variables, cell objects, nonlocal, __closure__, factory functions.

Decorators

@syntax, functools.wraps, stacking, argument decorators, class decorators.

Generators

yield, generator pipelines, yield from, send(), gi_ attributes.

Context Managers

with statement, __enter__/__exit__, @contextmanager, ExitStack.

Object-Oriented Python

Classes, __init__, inheritance, dataclasses, dunder methods.

Dataclasses

@dataclass, fields, defaults, frozen, field(), __post_init__.

Enums

Enum, IntEnum, StrEnum, Flag, auto(), aliases, @unique.

Abstract Base Classes

@abstractmethod, interfaces, register(), collections.abc.

The Data Model

dunder methods, __repr__, containers, operator overloading.

Error Handling

try/except/else/finally, custom exceptions, context managers.

Exception Hierarchy

the BaseException tree, catching by level, custom exceptions.

Walrus Operator (:=)

assignment expressions, while loops, comprehensions.

File I/O

open(), modes, text vs binary, encodings, the with statement.

Modules & Packages

import system, __init__.py, pip, virtual environments.

Type Hints

Annotations, Optional/Union, generics, Protocols, mypy.

Concurrency

threading, multiprocessing, asyncio, the GIL.

Testing

unittest, pytest, fixtures, parametrize, mocking.

Standard Library

11 modules: os, sys, json, re, datetime, asyncio, and more.

One sentence

Python is the language designed to be readable above all else — code that looks almost like plain English, with a huge standard library and the most active ecosystem in data science and machine learning.

What Python is

Python is a general-purpose, high-level, interpreted programming language created by Guido van Rossum and first released in 1991. Its central design principle — readability counts — is enshrined in The Zen of Python (PEP 20). Python uses significant whitespace (indentation) to define code blocks instead of braces, which forces readable structure. It is dynamically typed, multi-paradigm (procedural, OOP, functional), and ships with a comprehensive standard library described as "batteries included."

Pythonhello.py
# Python requires no boilerplate to write useful code
name = input("Your name: ")
print(f"Hello, {name}! Welcome to Python.")

# The Zen of Python (excerpt) — import this
# Beautiful is better than ugly.
# Simple is better than complex.
# Readability counts.

Core syntax at a glance

Pythonsyntax_overview.py
# Variables — no declaration keyword
x = 42
name = "Priya"
pi = 3.14159
is_active = True

# Collections
fruits = ["mango", "apple", "banana"]   # list — mutable, ordered
coords = (10.5, 20.3)                   # tuple — immutable
config = {"host": "localhost", "port": 5432}  # dict — key-value
unique = {1, 2, 3, 4}                   # set — unordered, unique

# Control flow — indentation defines blocks
if x > 10:
    print("large")
elif x > 0:
    print("small positive")
else:
    print("zero or negative")

# Loop
for fruit in fruits:
    print(fruit)

# Function
def greet(name, greeting="Hello"):
    return f"{greeting}, {name}!"

# Class
class Counter:
    def __init__(self, start=0):
        self.count = start

    def increment(self):
        self.count += 1
        return self

# List comprehension — functional style
squares = [x**2 for x in range(10) if x % 2 == 0]
# [0, 4, 16, 36, 64]

Dynamic typing and type hints

Python is dynamically typed: variable types are determined at runtime, not declared at compile time. Python 3.5 introduced type hints (PEP 484) — optional annotations that describe intended types. They are not enforced at runtime by Python itself; external tools like mypy, pyright, and Pyrefly check them statically. Type hints are now standard practice in professional Python code.

Pythontype_hints.py
from typing import Optional

def process(items: list[str]) -> Optional[str]:
    """Return the first item, or None if empty."""
    return items[0] if items else None

# Type hints don't change runtime behaviour — just documentation + static analysis
result: str = process(["a", "b"])   # type checker knows this is str | None

The Python ecosystem

Python's package manager is pip; its package index is PyPI (pypi.org) with over 500,000 packages. The most widely used packages: NumPy (arrays and numerical computing), pandas (dataframes and data manipulation), Matplotlib (plotting), scikit-learn (machine learning), TensorFlow and PyTorch (deep learning), Django and FastAPI (web frameworks), requests (HTTP), SQLAlchemy (ORM).

What Python is used for

Data science and machine learning (dominant language), web backends (Django, FastAPI, Flask), automation and scripting, scientific computing, computer vision (OpenCV), NLP, DevOps tooling, API development, teaching and education. Python is the #1 language on TIOBE and GitHub by most metrics in 2026.

Python versions

Python 2 reached end-of-life on 1 January 2020. Always use Python 3. Current stable: Python 3.13 (October 2024). Significant additions by version: 3.5 — type hints (PEP 484); 3.6 — f-strings; 3.7 — guaranteed dict insertion order, dataclasses; 3.8 — walrus operator (:=); 3.10 — structural pattern matching (match/case); 3.11 — 10–60% speed improvement; 3.12 — improved error messages; 3.13 — experimental free-threaded mode (GIL optional).

The data model and dunder methods

Everything in Python is an object — including integers, functions, and classes. Python's behaviour for built-in operations is defined by dunder methods (double-underscore): __add__ for +, __len__ for len(), __iter__/__next__ for iteration, __enter__/__exit__ for context managers (with statement). Implementing these protocols lets user-defined classes integrate seamlessly with built-in syntax.

Pythondunder_methods.py
class Vector:
    def __init__(self, x, y): self.x, self.y = x, y
    def __add__(self, other): return Vector(self.x+other.x, self.y+other.y)
    def __repr__(self): return f"Vector({self.x}, {self.y})"
    def __len__(self): return 2
    def __eq__(self, other): return self.x==other.x and self.y==other.y

v1 = Vector(1, 2)
v2 = Vector(3, 4)
print(v1 + v2)    # Vector(4, 6) — uses __add__
print(len(v1))    # 2 — uses __len__
print(v1 == v1)   # True — uses __eq__

Generators and lazy evaluation

A generator is a function that uses yield instead of return. Calling a generator function returns a generator object — an iterator that produces values lazily, one at a time. Generators are memory-efficient for large sequences: a generator for all integers uses O(1) memory, where a list would use O(n).

Pythongenerators.py
def fibonacci():
    a, b = 0, 1
    while True:          # infinite sequence
        yield a
        a, b = b, a + b  # never stores the sequence

gen = fibonacci()
for _ in range(10):
    print(next(gen), end=" ")  # 0 1 1 2 3 5 8 13 21 34

# Generator expressions — like list comprehensions but lazy
big_squares = (x**2 for x in range(10_000_000))  # O(1) memory
first = next(big_squares)  # 0 — computed on demand

Decorators

A decorator is a function that takes a function as input and returns a modified function. The @decorator syntax is sugar for func = decorator(func). Decorators enable cross-cutting concerns — caching, timing, authentication, validation — without modifying the underlying function.

Pythondecorators.py
import functools, time

def timer(func):
    @functools.wraps(func)      # preserve function metadata
    def wrapper(*args, **kwargs):
        start = time.perf_counter()
        result = func(*args, **kwargs)
        elapsed = time.perf_counter() - start
        print(f"{func.__name__}: {elapsed:.4f}s")
        return result
    return wrapper

@timer
def slow_sum(n):
    return sum(range(n))

slow_sum(10_000_000)   # slow_sum: 0.1234s

Context managers and the with statement

A context manager implements __enter__ and __exit__. The with statement calls __enter__ on entry and __exit__ on exit — even if an exception occurs. This guarantees cleanup. File handles, database connections, locks, and network sockets are all context managers in Python's standard library.

Pythoncontext_managers.py
# File — automatically closed on exit
with open("data.csv", "r") as f:
    content = f.read()
# f is closed here, even if an exception occurred

# contextlib.contextmanager: decorator-based context manager
from contextlib import contextmanager

@contextmanager
def timer(label):
    import time
    start = time.perf_counter()
    yield
    print(f"{label}: {time.perf_counter()-start:.4f}s")

with timer("my block"):
    result = sum(range(10_000_000))

Standard library highlights

ModulePurposeKey functions
osOS interfaceos.path, os.environ, os.listdir
sysInterpretersys.argv, sys.path, sys.exit
jsonJSON encode/decodejson.dumps, json.loads
reRegular expressionsre.match, re.search, re.findall
datetimeDates and timesdatetime.now(), timedelta
pathlibFile paths (OOP)Path(), Path.read_text()
collectionsSpecialised containersdeque, Counter, defaultdict
itertoolsIterator toolschain, product, combinations
functoolsFunctional toolslru_cache, partial, reduce
threadingThreadsThread, Lock, Event
asyncioAsync I/Oasync/await, gather, run
unittestTestingTestCase, assertEqual, mock

Virtual environments and packaging

venv: python -m venv .venv creates an isolated Python environment with its own packages, preventing version conflicts between projects. source .venv/bin/activate (Unix) or .venv\Scripts\activate (Windows) activates it. Always use a venv per project.

pip: pip install requests installs from PyPI. pip freeze > requirements.txt records dependencies. uv (2024): a Rust-based Python package manager 10–100× faster than pip, rapidly becoming the standard. Poetry and PDM: dependency management + build tools.

Commonly confused
Python 2 and Python 3 are not compatible. print is a statement in Python 2 (print "hello") and a function in Python 3 (print("hello")). Division behaves differently: 7/2 = 3 in Python 2 (integer division), 3.5 in Python 3 (float division). Python 2 reached end-of-life January 2020. Use Python 3 only.
Python is not slow — it depends on what you measure. Pure Python loops are ~100× slower than C. But most Python performance-critical code calls into C extensions (NumPy, pandas, scikit-learn). A NumPy matrix multiplication runs at C/FORTRAN speed. Python's "slowness" is mostly a concern in pure compute loops, not I/O-bound or library-calling code.
The GIL limits CPU parallelism in threads but not all concurrency. Python threads can run concurrently for I/O-bound work (network calls, file reads release the GIL). For CPU-bound parallelism, use multiprocessing (separate processes with separate GILs) or Python 3.13's experimental free-threaded mode (--disable-gil).

CPython internals: the reference implementation

CPython (github.com/python/cpython) is the reference implementation of Python. Python is formally specified by the CPython implementation and the Python Language Reference (docs.python.org/3/reference/). The compilation pipeline: source code → tokeniser → parser → AST → compile to bytecode (.pyc in __pycache__) → CPython virtual machine (a stack-based VM) executes bytecode. The bytecode format is documented in the dis module: import dis; dis.dis(function) shows the bytecode instructions.

Pythoninspect_bytecode.py
import dis

def add(x, y):
    return x + y

dis.dis(add)
# Bytecode output (Python 3.13):
#   LOAD_FAST    0 (x)
#   LOAD_FAST    1 (y)
#   BINARY_OP    0 (+)
#   RETURN_VALUE

Memory management: reference counting + cyclic GC

CPython uses reference counting as its primary memory management strategy. Every Python object has a reference count. When the count reaches 0, the object is freed immediately. Reference counting is deterministic and low-latency — unlike tracing GC, it doesn't require stop-the-world pauses. However, reference counting cannot handle reference cycles (object A points to B, B points to A). Python's cyclic garbage collector (gc module) periodically detects and collects cycles — it runs by default in three generations (gen 0, 1, 2) with collection frequencies configurable via gc.set_threshold().

PEP process and language governance

Python evolves through the PEP (Python Enhancement Proposal) process. Any Python developer can write a PEP; significant ones are reviewed by the Steering Council (a five-person elected body established in PEP 13 following Guido van Rossum's resignation as BDFL in July 2018). Key PEPs: PEP 8 (style guide), PEP 20 (Zen of Python), PEP 484 (type hints), PEP 572 (walrus operator), PEP 634 (structural pattern matching), PEP 703 (no-GIL / free-threaded CPython, accepted 2023, experimental in 3.13).

Performance: CPython vs. PyPy vs. Cython

PyPy: a JIT-compiled Python implementation. Typically 4–10× faster than CPython on pure Python code. Excellent for compute-heavy Python without C extensions. Cython: a superset of Python that compiles to C. Adding type annotations to a Cython file can yield 50–100× speedups for numerical code. Used internally by pandas, scikit-learn, and scipy. Numba: a JIT compiler for NumPy-heavy code; @numba.jit can achieve near-C performance for numerical loops without rewriting in C.

Specification reference

Python Language Reference — docs.python.org/3/reference/ (authoritative). Python Language Reference §3 — Data model (object model, dunder methods, reference counting). PEP 20 — The Zen of Python. PEP 703 — Making the Global Interpreter Lock Optional in CPython (accepted 2023). Van Rossum, G. & Drake, F. L. Jr. (eds.) (2009). The Python Language Reference. Python Software Foundation.

Sources

1
Python Software Foundation. The Python Language Reference. docs.python.org/3/reference/. — Authoritative language specification.
2
Python Software Foundation. The Python Standard Library. docs.python.org/3/library/. — Standard library documentation.
3
PEP 8 — Style Guide for Python Code. peps.python.org/pep-0008/.
4
PEP 20 — The Zen of Python. peps.python.org/pep-0020/.
5
PEP 484 — Type Hints. peps.python.org/pep-0484/.
6
PEP 703 — Making the Global Interpreter Lock Optional. peps.python.org/pep-0703/.
Source confidence: High Last verified: Primary source: Python Language Reference — docs.python.org/3/reference/

For AI & developers — machine-readable Python reference

Every Python page here is available as clean Markdown, and the entire Python corpus is concatenated into a single file for AI ingestion. Official sources only, three reading levels per page.

llms-full.txt — full corpus llms.txt — page index Each page also serves index.md