Lesson 47 / 49

Type Hints

Add optional type hints to document intent and let mypy and editors catch bugs before runtime.

Annotating functions

Type hints don't change runtime behaviour — Python ignores them at execution — but editors and tools like mypy use them to catch mistakes before you run the code.

def greet(name: str, times: int = 1) -> str:
    return (f"Hi, {name}! " * times).strip()

age: int = 25
scores: list[int] = [90, 85, 78]

Why bother?

Type hints are especially valuable on public function signatures and large codebases — they act as always-up-to-date documentation.