Type hints specify expected types for variables, arguments, and return values.
1# Basic type hints2def greet(name: str) -> str:3 return f"Hello, {name}"45# Variables6age: int = 257name: str = "Alice"8is_active: bool = True910# Collections11from typing import List, Dict, Optional, Union1213def process(items: list[str]) -> dict[str, int]:14 return {item: len(item) for item in items}1516def find_user(user_id: int) -> dict | None:17 if user_id in users:18 return users[user_id]19 return None2021# Complex types22from typing import Callable, TypeAlias2324Handler: TypeAlias = Callable[[str, int], bool]2526def register_handler(handler: Handler) -> None:27 handlers.append(handler)2829# TypedDict30from typing import TypedDict3132class UserDict(TypedDict):33 name: str34 age: int35 email: str
Benefits: