Skip to content

Practice: Type Hints

View practice code on GitHub Get the Video Course

From the lesson

Practice problems for Section 08 Concepts. Build each yourself first, then expand the solution to check.

Three practice problems, one per concept step. The numbers line up. Build each yourself, then check python-practice/solutions. Some end with a short input() "Write a Program" exercise.

These problems continue the online-store story: this time you are adding typed store functions (type-hinted helpers, a typed product list, optional lookups) so the catalog code reads clearly and tools can check it later.

Step-01: Function Parameter and Return Hints

Concepts: parameter and return type hints, input().

Problem-1: Typed Price Helpers

Write a function price_with_tax that takes a price (a decimal number, a float) and a tax rate (also a float) and returns the price plus that tax, with type hints saying it takes two float values and returns a float. Call it with a price of 100.0 and a rate of 0.1 and print the result with the label "with tax:". Then write a function label_product that takes a product name (a str) and returns it in upper case, with type hints saying it takes a str and returns a str. Call it with "wireless mouse" and print the result.

Expected output
with tax: 110.0
WIRELESS MOUSE
Need a hint?

Put the hint on the def line: def price_with_tax(price: float, rate: float) -> float: and def label_product(name: str) -> str:. The -> float / -> str after the parentheses is the return type; .upper() returns the upper-cased name.

Show the solution
s08_practice01_sol_function_hints.py
# Problem-1 Task-1: price plus tax, typed floats
def price_with_tax(price: float, rate: float) -> float:
    tax = price * rate
    return price + tax
print("with tax:", price_with_tax(100.0, 0.1))

# Problem-1 Task-2: upper-case the product name
def label_product(name: str) -> str:
    return name.upper()
print(label_product("wireless mouse"))

View the full solution file on GitHub

Problem-2 (Write a Program): Build a SKU

Write a function format_sku that takes a product code (a str) and returns the label "SKU-<code>", with type hints saying it takes a str and returns a str. Ask the user for a product code, call format_sku with it, and print the returned label.

Example run
Enter a product code: 2024-15
SKU-2024-15
Need a hint?

format_sku returns a str, so build it with an f-string (f"SKU-{code}") and return it. The input() value is already a str, which matches the code: str hint.

Show the solution
s08_practice01_sol_function_hints.py
# Problem-2 Task-1: build SKU label from user code
def format_sku(code: str) -> str:
    return f"SKU-{code}"
code = input("Enter a product code: ")
print(format_sku(code))

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: variable hints, list[int], dict[str, int].

Problem-1: A Typed Price List

Define a variable product_prices holding the list of whole-number prices 40, 95, 25, with a variable hint saying it is a list[int]. Print it with the label "product_prices:", then print the highest price with the label "top price:".

Expected output
product_prices: [40, 95, 25]
top price: 95
Need a hint?

A variable hint goes right after the name: product_prices: list[int] = [40, 95, 25]. max() on the list[int] returns the highest element.

Show the solution
s08_practice02_sol_variable_and_container_hints.py
# Problem-1 Task-1: typed price list, print and max
product_prices: list[int] = [40, 95, 25]
print("product_prices:", product_prices)
print("top price:", max(product_prices))

View the full solution file on GitHub

Problem-2: A Typed Stock Map

Define a variable stock_counts holding a mapping of product names to whole-number quantities in stock, with "mouse" at 12 and "keyboard" at 7, and a variable hint saying it is a dict[str, int]. Print it with the label "stock_counts:", then look up the mouse count and print it with the label "mouse stock:".

Expected output
stock_counts: {'mouse': 12, 'keyboard': 7}
mouse stock: 12
Need a hint?

Write stock_counts: dict[str, int] = {...} where str is the key type (product names) and int is the value type (quantities). Look a value up by key with stock_counts["mouse"].

Show the solution
s08_practice02_sol_variable_and_container_hints.py
# Problem-2 Task-1: typed stock map, print and look up mouse
stock_counts: dict[str, int] = {"mouse": 12, "keyboard": 7}
print("stock_counts:", stock_counts)
print("mouse stock:", stock_counts["mouse"])

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, hints not enforced, input().

Problem-1: An Optional Price Lookup

Write a function lookup_price that takes a product name and looks it up in a small catalog ("mouse" at 25 and "monitor" at 120), returning the matching price or nothing when the product is missing, with type hints saying it takes a str and returns either an int or None. Call it for "mouse" and for "cable" (not in the catalog) and print each result with a label. Then write a function triple_units that takes a quantity (an int) and returns it multiplied by 3, with type hints saying it takes an int and returns an int. Call it with 4 and print the result with the label "triple 4:", then call it with the text "ab" and print the result with the label "triple text:" to show that Python does not enforce the hint at runtime.

Expected output
mouse: 25
cable: None
triple 4: 12
triple text: ababab

A type hint is not a runtime check

Calling triple_units("ab") runs without an error and returns "ababab", because * repeats a string. Python does not enforce type hints while the program runs, so a wrong type slips through until a tool like a type checker or a later line catches it.

Need a hint?

int | None means the return is either an int or None; .get(name) already returns None for a missing key. The triple_units("ab") call still runs because hints are not enforced at runtime, so "ab" * 3 gives "ababab".

Show the solution
s08_practice03_sol_optional_and_not_enforced.py
# Problem-1 Task-1: optional price lookup, returns int or None
def lookup_price(name: str) -> int | None:
    catalog = {"mouse": 25, "monitor": 120}
    return catalog.get(name)
print("mouse:", lookup_price("mouse"))
print("cable:", lookup_price("cable"))
# Problem-1 Task-2: triple units, showing hints are not enforced
def triple_units(n: int) -> int:
    return n * 3
print("triple 4:", triple_units(4))
print("triple text:", triple_units("ab"))

View the full solution file on GitHub

Problem-2 (Write a Program): Look Up a Typed Price

Ask the user for a product name and print what lookup_price returns for it.

Example run
Enter a product: monitor
120
Need a hint?

lookup_price returns int | None, so print whatever it returns directly. The input() value is a str, matching the name: str parameter hint.

Show the solution
s08_practice03_sol_optional_and_not_enforced.py
# Problem-2 Task-1: look up user-entered product price
typed = input("Enter a product: ")
print(lookup_price(typed))

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 workshop 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

Practice: Functions Next: Practice: Error Handling and Debugging

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