PY-101 ยท Advanced Python
Type hints
โฑ 8 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
Type hints are optional notes that say what type a value should be, such as def total(prices: list[float]) -> float:. Python does not enforce them at run time, but editors and checkers like mypy use them to catch mistakes early and to autocomplete. They also make code easier for people and AI tools to read.
- Annotate parameters and return types
- Use list[str], dict[str, int] and | None
- Know hints are not enforced
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
def average(marks: list[float]) -> float:return sum(marks) / len(marks)def find_user(users: dict[str, int], name: str) -> int | None:return users.get(name)print(average([72.0, 88.0, 91.0]))print(find_user({"asha": 29}, "asha"), find_user({}, "ravi"))print(average.__annotations__["return"])
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
83.66666666666667
29 None
<class 'float'> ๐ฏ Add type hints so area takes two floats and returns a float, then print area(2.5, 4.0). The output is 10.0.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
def area(width: float, height: float) -> float:
- The problemA teammate calls your function with a price as text, "99", and the bug appears weeks later. A type hint would have made the editor underline it the moment they typed it.
- The Python idea
def f(x: int, names: list[str]) -> float:Type hints are optional notes that say what type a value should be, such as def total(prices: list[float]) -> float:. - In real lifeFastAPI reads your type hints to validate incoming JSON and generate API documentation automatically.
Start with a question
A teammate calls your function with a price as text, "99", and the bug appears weeks later. A type hint would have made the editor underline it the moment they typed it.
Modern Python codebases, FastAPI and AI SDKs rely on type hints for validation, docs and autocomplete.
Explain it like I'm 10
Type hints are labels on jars: "sugar only". Python will not stop you putting salt in, but everyone (and your editor) can see the label.
The technical explanation
def greet(name: str) -> str: says name is a str and the function returns a str.
list[int], dict[str, float] and tuple[int, int] describe collections. str | None means "a string or None".
Python ignores hints at run time. greet(5) still runs; a checker like mypy reports it.
Syntax
def f(x: int, names: list[str]) -> float:
...
age: int | None = None Real-world scenario
FastAPI reads your type hints to validate incoming JSON and generate API documentation automatically.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: Reading tracebacks โQuestions people ask
Do type hints make Python faster?
No. Standard Python ignores them at run time. They help editors, checkers, documentation and readers.
Should beginners use type hints?
Yes on function signatures. They make your intent clear and your editor much more helpful, at very little cost.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner Do type hints make Python faster?
No. Standard Python ignores them at run time. They help editors, checkers, documentation and readers.
beginner Should beginners use type hints?
Yes on function signatures. They make your intent clear and your editor much more helpful, at very little cost.
intermediate Does Python stop you calling def f(x: int) with a string?
No, hints are not enforced at run time. Tools check hints; Python does not.
intermediate How do you write a return type?
def f() -> int:. Use -> before the colon.
Quick revision
Remember
- Annotate parameters and return types
- Use list[str], dict[str, int] and | None
- Know hints are not enforced
- Hints are documentation that tools can check. Add them to function signatures first; that is where they help most.
Syntax
def f(x: int, names: list[str]) -> float:
...
age: int | None = None Official documentation
- typing โ support for type hints (docs.python.org)