Workshop: Type Hints (Optional, Self-Study)¶
View workshop code on GitHub Get the Video Course
From the lesson
Workshop problems for Section 08 Concepts. Build each yourself first, then expand the solution to check.
Three optional workshop problems, one per concept step, in a car scenario. Self-study: check python-workshop/solutions.
Step-01: Function Parameter and Return Hints¶
Concepts: parameter and return type hints.
Write a function label that takes a car brand and a year and returns the text "<brand> (<year>)", with type hints saying it takes a str and an int and returns a str. Call it with "Mazda" and 2020 and print the result.
Need a hint?
Mix the hint types on one def line: def label(brand: str, year: int) -> str:, then return an f-string like f"{brand} ({year})".
Show the solution
Pages for this step: Concept | Practice | Workshop
Python Files for this step: Concept | Practice | Practice Solutions | Workshop | Workshop Solutions
Step-02: Variable Hints and Container Hints¶
Concepts: list[int], dict[str, int].
Write a program that:
- Task-1: define a variable
mileagesholding the list of whole numbers12000,8000,15000, with a variable hint saying it is alist[int], then print the list and print the highest mileage. - Task-2: define a variable
pricesholding a mapping of car brand to whole-number price, with Mazda at15000and Tesla at42000, and a variable hint saying it is adict[str, int], then look up Tesla's price and print it.
Need a hint?
Hint each variable at definition: mileages: list[int] = [...] and prices: dict[str, int] = {...}. Use max(mileages) for the highest and prices["Tesla"] for the lookup.
Show the solution
# Task-1: typed mileage list, print and highest
mileages: list[int] = [12000, 8000, 15000]
print(mileages)
print(max(mileages))
# Task-2: typed brand-to-price map, look up Tesla
prices: dict[str, int] = {"Mazda": 15000, "Tesla": 42000}
print(prices["Tesla"])
Pages for this step: Concept | Practice | Workshop
Python Files for this step: Concept | Practice | Practice Solutions | Workshop | Workshop Solutions
Step-03: Optional Values, and Hints Are Not Enforced¶
Concepts: int | None (optional), .get().
Write a function find_car_price that takes a car brand and looks it up in a price list (Mazda at 15000 and Tesla at 42000), returning the matching price or nothing when the brand is missing, with type hints saying it takes a str and returns either an int or None. Call it for "Mazda" (known) and "Kia" (unknown) and print each result.
Need a hint?
Use -> int | None so a missing brand can return None, and prices.get(brand) gives None instead of raising when "Kia" is not a key.
Show the solution
Pages for this step: Concept | Practice | Workshop
Python Files for this step: Concept | Practice | Practice Solutions | Workshop | Workshop Solutions
Finished this section?
Check your work against the solutions above, then head back to the Section 08 lesson, or try the Section 08 practice for more reps.
Prefer to learn by watching?
The full video course teaches every concept on screen and builds the projects with you, step by step.
Workshop: Functions Next: Workshop: Error Handling and Debugging