Typing
Python's type hint system enables optional static type checking. While Python remains dynamically typed, type hints improve code readability, enable IDE autocompletion, and catch bugs with tools like mypy. The guide walks through Basic Type Annotations, Optional, Union & Literal, Protocol (Structural Typing), Generics, Type Aliases & NewType. Annotate variables: `name: str = "Alice"`. Annotate function parameters and return: `def greet(name: str) -> str:`. Built-in types: `int`, `float`, `str`, `bool`, `bytes`, `list`, `dict`, `tuple`, `set`. Use `None` for void functions. `Optional[int]` means `int` or `None`. `Union[str, int]` accepts either type (Python 3.10+: `str | int`). `Literal["red", "green", "blue"]` restricts to specific values. `Any` bypasses type checking. `Never` indicates unreachable code. `Protocol` defines an interface that types satisfy by having the required attributes, without explicit inheritance. Known as structural subtyping or duck typing with static checks. `@runtime_checkable` enables `isinstance()` checks at runtime. The guide is organized into 5 sections that build on each other, each pairing a prose explanation with runnable code.