Generic hints (list[str], dict[str, int])
The brackets after list or dict say what kind of things the container holds.
Used in 51 lessons of the course, from 00.01.
A type hint like list only says "this is a list". A generic hint adds what is inside: list[str] is a list of strings.
In dict[str, int] there are two types: the first is for the keys and the second is for the values. In tuple[str, str] there is one type per position.
Hints can nest. Lesson 00.01 has GLOSSARY: dict[str, tuple[str, str]] = {: a dict whose keys are strings and whose values are tuples of two strings.
Python does not check these hints when it runs. They help you, your editor and a static type checker understand the code. So you can write them freely: they don't change what the program does.
names: list[str] = ["Ana", "Luis"]
Reach for it when: you want to make clear what a list, dict or tuple contains, especially in variables and function parameters.
Examples
Common mistake
scores: dict[str] = {"Ana": 3}
print(scores)Why: A dict needs two types, key and value. Python doesn't complain at runtime, but the hint is wrong and a type checker would flag it.
How to write it: Write dict[str, int]: key type and value type.
