Content · Programming · Python

Python Start with Simple Function Parameter Annotations

Start annotating small functions with built-in types.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

Start annotating small functions with built-in types.

What It Does and Why It Matters


Write parameter types after colons and a return type after ->. Use int, float, str and bool for simple inputs; use list[str] when the function receives a list of text. A function that only prints or performs a side effect can have -> None. Add annotations incrementally, beginning with public functions.

The notation does not coerce a str into a float; conversion remains explicit runtime code.

Step-by-Step Example


Start with the complete example below. Read the inputs and predict what the final print statement will show before you run it.

def total_cost(prices: list[float]) -> float:
    return sum(prices)
def show_total(prices: list[float]) -> None:
    print(f"Total: {total_cost(prices):.2f}")
show_total([3.5, 2.0, 7.5])

How the Code Works


  • The definition of total_cost, show_total creates reusable behavior; its body runs only when called or instantiated as appropriate.
  • The print calls display the results. Compare these results with the method’s documented return type and the input used.

Expected output:

Total: 13.00

For the demonstrated inputs, Python displays 'Total: 13.00'. This result follows from the operation described above; change one input at a time to understand which rule matters.

What to Notice


Pay attention to the accepted input types, the returned value and the boundary behavior. Predict what changes if you replace the example input with an empty, zero, negative or missing value, where that value is meaningful for this operation.

If the task uses a library function, inspect its documented exceptions and return type before reusing it inside a larger function.

Where You Would Use This


This helps maintain larger projects where functions and data shapes cross module boundaries. Check the code with a static analyzer, and still validate data received from files, users or remote APIs. Annotation constructs do not normally enforce types automatically at runtime.

Practice Exercise


Annotate a function that filters positive integers and returns a list of integers.

Before running your edited example, write down the result you expect. Then test at least one valid boundary or deliberately invalid value, using appropriate exception handling where necessary.

Reference: official Python documentation for the complete API contract and version notes.

Common Beginner Mistakes


  • Using list instead of list[float] when the element type is important, or writing -> None for a function that returns a value.
  • Overlooking the documented condition: a function that only prints or performs a side effect can have -> None.

Key Takeaways


  • Start annotating small functions with built-in types.
  • Write parameter types after colons and a return type after ->.
  • Annotate a function that filters positive integers and returns a list of integers.
Share this resourceSend this page to someone who may find it useful.