×

python · level 3 · unit 3 · lesson 3

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

Type hints say what each value should be.

A is a note about types written into the code. def total(prices: list[int]) -> int: says prices should be a list of whole numbers, and the function hands back one. Variables can have hints too: name: str = "Sam".

Write dict[str, float] for a dictionary from text to decimals, and a like str | None when a value might be missing.

Python ignores hints when it runs. A such as mypy reads them and warns you about mistakes before your code ever runs. Your editor uses them too.

def find_price(menu: dict[str, int], item: str) -> int | None:
    if item in menu:
        return menu[item]
    return None

menu: dict[str, int] = {"tea": 180, "cake": 325}
print(find_price(menu, "cake"))
print(find_price(menu, "pie"))

To check for a missing value, write if price is None:, because is asks whether it is that exact object.