

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 3
click for more info
Not enough gems
Cost: 6 gems
1: Type Hints
incomplete
2: Basic Types
incomplete
3: Function Parameters
incomplete
4: Return Types
incomplete
5: Fixing Type Hints
incomplete
6: List and Set Hints
incomplete
7: Dictionary Hints
incomplete
8: Tuple Hints
incomplete
9: Specific Container Types
incomplete
10: Nested Types
incomplete
11: Optional Values
incomplete
12: Fix Code With Type Hints
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Some functions accept numbers as arguments; others accept strings. Some return lists; others return dictionaries, booleans, or None.
When a program is small, you can usually remember the types of your variables. But as programs grow, it's easy to forget:
level an int or a str?get_item() always return an item name (str), or sometimes None if it can't find one?inventory a list of strings, or a dictionary of item counts?Type hints let us write those expectations directly in our code:
def get_damage(weapon: dict, level: int) -> int:
return weapon["damage"] + (level * 2)
The weapon: dict, level: int, and -> int parts are type hints. They tell humans and code editors what kinds of values the function expects and returns.
Type hints don't make Python stop being Python. It's still a dynamically typed language, and it won't automatically reject the wrong value just because a type hint says so.
Type hints are for: