What are Type Hints?
Type hints (PEP 484) let you annotate variables and function parameters with expected types. They improve code readability and IDE support.
Basic Types
name: str = "Alice"
age: int = 25
height: float = 1.75
def greet(name: str) -> str:
return f"Hello, {name}!"
def add(a: int, b: int) -> int:
return a + b
Complex Types
from typing import List, Dict, Optional, Union
names: List = ["Alice", "Bob"]
scores: Dict[str, int] = {"Alice": 90}
email: Optional = None
id_val: Union[int, str] = "ABC123"
Python 3.10+ Syntax
names: list = ["Alice", "Bob"]
id_val: int | str = "ABC123"
email: str | None = None
Summary
- Type hints improve code documentation and IDE support
- Use typing module for complex types (List, Dict, Optional)
- Python 3.10+ supports list[], dict[], and | for unions
- Type hints are not enforced at runtime
YouTip