Py Typing Hints — Free Python Tutorial
Learn Py Typing Hints in Python with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.
TL;DR: Learn Py Typing Hints in Python with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.
Written & reviewed by the Syllab.in Academic Team (CBSE/NCERT subject experts) · Updated
Py Typing Hints in Python
Type hints (PEP 484, Python 3.5+) let you annotate variables and functions with expected types. Python does NOT enforce them at runtime — they exist for editors, linters (mypy, pyright), and human readers.
Basic syntax: variable: Type = value and def fn(param: Type) -> ReturnType:. For built-in generics Python 3.9+ allows list[int], dict[str, int] directly. For older Python import from the typing module.
Special types: Optional[T] means T | None (can be missing). Union[A, B] means A or B. Any disables checking. Callable[[int, str], bool] annotates functions. TypeVar creates generic functions.
Gradual typing: you can add hints one file or one function at a time. Start with function signatures — they give the most value. Run mypy . to see all type errors across a project.
Py Typing Hints — Syntax
# Variables
count: int = 0
names: list[str] = []
# Functions
def add(a: int, b: int) -> int:
return a + b
# Optional / Union (Python 3.10+)
def find(name: str) -> str | None: ...
# from typing (Python 3.8)
from typing import Optional, List, Dict
def greet(name: str, title: Optional[str] = Non
Learn Py Typing Hints step by step with Syllab's free interactive Python tutorial — runnable code examples, practice exercises and instant AI feedback, all free with no signup. Explore the full Python course →