Software Engineer (SDE)pythonintermediate3 interactive MCQs in app
Type Annotations: Optional, Union, List & Dict
type narrowing for Optional / T | Nonestatic compile/CI checking vs zero runtime enforcementstructural subtyping with Protocol
Core Concept
Type hints (`str | None`, `list[int]`, `dict[str, float]`, `TypedDict`, `Protocol`) document function contracts explicitly so static checkers (`mypy` / `pyright`) catch `None`-dereference bugs and wrong dict shapes BEFORE runtime! Important: Python **does NOT enforce type annotations at runtime** automatically—hints are metadata only unless paired with runtime validators like Pydantic!
Worked Example
`email: str | None = users.get(uid)` makes `email.lower()` fail `mypy`, whereas `if email is not None: return email.lower()` narrows the type safely!
Common Pitfall
assuming `def pay(cents: int)` rejects `"100"` at runtime without validation, or omitting `if user is not None:` narrowing on `User | None` returns.
Recommended Readings & Practice
Practice Type Annotations: Optional, Union, List & Dict with 3 graded MCQs & written feedback.
Take SOARDO’s free 12-question diagnostic to see how strong you are across your target role and unlock adaptive daily missions.