Lesson 1
Introduction to Type Annotations in Python
Lesson Introduction

Welcome to learning about type annotations in Python! Understanding type annotations can significantly improve the readability and maintainability of your code. By providing explicit types for your variables and function parameters, you not only help yourself but also others who might be reading your code understand your intentions more clearly. Our goal today is to ensure you are comfortable with the syntax and usage of type annotations in Python.

What are Type Annotations?

Type annotations explicitly specify the data types of variables, function parameters, and return values. While Python is dynamically typed, meaning it doesn't require explicit data types, adding type annotations is beneficial:

  • Code Clarity: Makes your code more readable.
  • Error Prevention: Helps catch errors early with tools like mypy.
  • Documentation: Serves as documentation for your code's data types.
Basic Syntax of Type Annotations

Let's break down the basic syntax using the add function.

Without type annotations:

Python
1def add(a, b): 2 return a + b

With type annotations:

Python
1def add(a: int, b: int) -> int: 2 return a + b

In the function signature def add(a: int, b: int) -> int::

  • a: int indicates a should be an int.
  • b: int indicates b should be an int.
  • -> int specifies the function returns an int.
Annotating Variables

You can also annotate variables outside of functions to ensure their expected types:

Python
1if __name__ == "__main__": 2 x: int = 5 3 y: int = 10 4 print(x, y) # 5 10

Here, x and y are explicitly annotated as integers, clarifying their types.

Practical Example with Functions

Let's look at another example. Consider a function that greets a user:

Python
1def greet(name: str) -> str: 2 return f"Hello, {name}!"
  • name: str indicates name should be a str.
  • -> str specifies the function returns a str.

Example:

Python
1def greet(name: str) -> str: 2 return f"Hello, {name}!" 3 4if __name__ == "__main__": 5 greeting = greet("Alice") 6 print(greeting) # Output: Hello, Alice!
Complex Example 1

Now, let's see an example of a function that takes a list of integers and returns a boolean:

Python
1def has_even(numbers: list[int]) -> bool: 2 return any(n % 2 == 0 for n in numbers)
  • numbers: list[int] indicates numbers should be a list of integers.
  • -> bool specifies the function returns a boolean.
Complex Example 2

And here is another example of a function that finds sum of numbers from the given list:

Python
1def sum(nums: list[int | float]) -> int | float: 2 total = 0 3 for num in nums: 4 total += num 5 return total

The operator | indicates that the variable can be of multiple types (also known as a union type). In this case, elements in the nums list can be either integers or floats, and the function's return type can also be an integer or a float. This flexibility allows the function to handle a mix of numeric types seamlessly.

Lesson Summary

You learned about the importance and usage of type annotations in Python. We covered:

  • What type annotations are and their benefits.
  • How to use type annotations for function parameters and return types.
  • How to annotate variables.

This knowledge helps you write more readable and maintainable code, making your intentions clear to anyone who reads it.

Let's move on to practice exercises. You'll apply your knowledge of type annotations in various coding tasks, solidifying your understanding and improving your Python skills.

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.