Codzy

Type hints

Type hints say what type of value a variable, a parameter or a function expects: age: int = 20, or def average(scores: list[float]) -> float:. The arrow -> gives the type of the returned value.

Python doesn't check these hints while running: they make the code clearer and let editors and tools like mypy spot mistakes before you even run the program.

def average(scores: list[float]) -> float:
    return sum(scores) / len(scores)

name: str = "Sara"
age: int = 20
print(average([12.0, 15.5, 18.0]))
print(average.__annotations__)

Start the lesson