# Python
## Python — The Codex Coding
**URL:** https://thecodex.expert/coding/languages/python/
**Type:** language-reference | **Language:** Python | **Confidence:** high | **Last verified:** 2026-06-01

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

## 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.

## BEGINNER
> 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."

```python
# 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

```python
# 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.

```python
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).

## INTERMEDIATE
## 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.

```python
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).

```python
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.

```python
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.

```python
# 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

| Module | Purpose | Key functions |
| --- | --- | --- |
| `os` | OS interface | os.path, os.environ, os.listdir |
| `sys` | Interpreter | sys.argv, sys.path, sys.exit |
| `json` | JSON encode/decode | json.dumps, json.loads |
| `re` | Regular expressions | re.match, re.search, re.findall |
| `datetime` | Dates and times | datetime.now(), timedelta |
| `pathlib` | File paths (OOP) | Path(), Path.read_text() |
| `collections` | Specialised containers | deque, Counter, defaultdict |
| `itertools` | Iterator tools | chain, product, combinations |
| `functools` | Functional tools | lru_cache, partial, reduce |
| `threading` | Threads | Thread, Lock, Event |
| `asyncio` | Async I/O | async/await, gather, run |
| `unittest` | Testing | TestCase, 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.

How this connects

Requires first

[Variables](/coding/concepts/variables/)[Functions](/coding/concepts/functions/)

Enables

[JavaScript (comparison)](/coding/languages/javascript/)[All data structures](/coding/data-structures/)

Primary source

[docs.python.org/3/ — official](https://docs.python.org/3/)

## EXPERT
## 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.

```python
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
- Python Software Foundation. The Python Language Reference. docs.python.org/3/reference/. — Authoritative language specification.
- Python Software Foundation. The Python Standard Library. docs.python.org/3/library/. — Standard library documentation.
- PEP 8 — Style Guide for Python Code. peps.python.org/pep-0008/.
- PEP 20 — The Zen of Python. peps.python.org/pep-0020/.
- PEP 484 — Type Hints. peps.python.org/pep-0484/.
- PEP 703 — Making the Global Interpreter Lock Optional. peps.python.org/pep-0703/.

**Primary source:** docs.python.org/3/reference/

*The Codex Coding — thecodex.expert/coding/ — Free forever — Official sources only — Mumbai, India*