Skip to main content
Module 9 · Lesson 1 of 433/36 in the course~16 min
Module lessons (1/4)

Type hints: annotating types

Lesson preview5 hands-on exercises readyPython Course · Module 9 — Modern Python in practice

Parameter and return annotations, list[int] / dict[str, int], union | None, and static checking with mypy.

What you'll learn in this lesson

  • Annotate parameters, return values, and local variables with modern type hints syntax (Python 3.10+).
  • Use the native generics list[int], dict[str, float], tuple[int, str] and the concise unions int | None.
  • Distinguish between static analysis (mypy, pyright, IDE) and Python runtime behavior.
  • Avoid the mistake of believing that type annotations automatically block the wrong types from running.

Overview

The type hints introduced in Python allow you to formally document the expected type of parameters, return values and attributes directly in the source code. They do not change the dynamic nature of Python at runtime: the interpreter does not raise errors if you pass a string where int was noted, but IDE and static type checkers (mypy,…

The syntax is nome: tipo for parameters and variables, and -> tipo_ritorno after the function parentheses. Python automatically saves this information in the __annotations__ dictionary.

Sections covered in this lesson

  1. 11. Write down the parameters, variables, and return values
  2. 22. Native Generic Collections (list[...], dict[...])
  3. 33. Unions and optional values with | and None
  4. 44. Callable, Any and Literal for expressive APIs
  5. 5Check for understanding
  6. 6Test yourself
Pro Lesson · Module 9 — Modern Python in practice

Unlock "Type hints: annotating types" and the full Python Course curriculum

Parameter and return annotations, list[int] / dict[str, int], union | None, and static checking with mypy.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in Python Course plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.