Since Python is a dynamically typed language we can declare a variable or the parameter of a function without having to declare their type. Python allows us to use something called annotations, which lets us specify the data type of a variable or the parameter of a function if we want to.
Examples
Without annotations
x = 3
def add(a, b):
return a + b
With annotations
x: int = 3
def add(a: int, b: int) -> int:
return a + b
NOTE: -> int
means that the function will return an integer.
Since annotations are ignored by Python, we use a package called mypy
. This package will check the code automatically for any type errors while coding.