Type Annotations in Python

From the Python cheat sheet ยท Type Hints ยท verified Jul 2026

Type Annotations

Annotate function parameters, return types, and variables.

python
# Function annotations
def greet(name: str, age: int = 0) -> str:
    return f"Hello {name}, age {age}"

# Variable annotations
count: int = 0
names: list[str] = ["Alice", "Bob"]
config: dict[str, int] = {"port": 8080}
๐Ÿ’ก Type hints are not enforced at runtime โ€” they're for documentation and tooling
โšก Use int | str (3.10+) instead of Union[int, str] โ€” it's cleaner
๐Ÿ“Œ Use list[str] directly (3.9+) instead of importing List from typing
๐ŸŸข Run mypy or pyright to check types statically: mypy app.py
typestype-hintsannotations
Back to the full Python cheat sheet