Lesson 45 / 47

Type Hints

Optional annotations that document intent and catch bugs early.

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.