Skip to content

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.

Expected output
Mazda (2020)
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
s08_workshop01_sol_function_hints.py
def label(brand: str, year: int) -> str:
    return f"{brand} ({year})"
print(label("Mazda", 2020))

View the full solution file on GitHub

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 mileages holding the list of whole numbers 12000, 8000, 15000, with a variable hint saying it is a list[int], then print the list and print the highest mileage.
  • Task-2: define a variable prices holding a mapping of car brand to whole-number price, with Mazda at 15000 and Tesla at 42000, and a variable hint saying it is a dict[str, int], then look up Tesla's price and print it.
Expected output
[12000, 8000, 15000]
15000
42000
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
s08_workshop02_sol_variable_and_container_hints.py
# 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"])

View the full solution file on GitHub

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.

Expected output
15000
None
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
s08_workshop03_sol_optional_and_not_enforced.py
def find_car_price(brand: str) -> int | None:
    prices = {"Mazda": 15000, "Tesla": 42000}
    return prices.get(brand)
print(find_car_price("Mazda"))
print(find_car_price("Kia"))

View the full solution file on GitHub

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.

Get the Video Course

Workshop: Functions Next: Workshop: Error Handling and Debugging

🎉 New Course
Python for Absolute Beginners
$15.99 $84.99 81% OFF
Get the Video Course