In the low-level programming languages like Java, C, C++, and C#, variables are strictly typed - which means you have to indicate the variable type when creating a variable.

For example, creating variables in Java:

String name = "John";       

int age;                    # only an integer can be assigned to age

age = 23;                   # assigning a value to age


Unlike Java, Python gives us the flexibility to declare and assign variables without specifying their types :

name = "John"

age = 23

In this type of coding, Python automatically figures out the type of data that has been assigned to a variable. You can verify that by using the type() function:

print(type(name))              # <class 'str'>
print(type(age))               # <class 'int'>


Type Hints in Python

Though Python is not a strictly typed language, you can still give a hint of a variable's data type.

To specify a variable type when declaring it,

variable_name: variable_type = value

weather: str = "cloudy"

degrees: int                    # you can declare a variable before assigning
degrees = 32


Also note that even after a variable type is indicated in Python, you can still assign a different data type to the variable:

degrees: int = 32
print(degrees)                      # 32

degrees = "thirty-two"
print(degrees)                      # thirty-two


Specifying a variable type in Python does not mean the variable can only accept values of that type. They are just hints that inform a user of the type which a variable is.


Adding types to functions

Type hints are very useful in Functions,  mostly when declaring function parameters.

For example, here is a simple function that takes in two arguments and returns the sum.

def sum(a, b):
    return a + b

By looking at this code, one cannot be sure if the caller is going to supply integers as the argument or strings or any other data type.

A call to this function works when supplied with int values and with other values such as strings, lists, and tuples:

sum(3, 5)                               # 8

sum([1, 0, 5], ['java', 'c++'])         # [1, 0, 5, 'java', 'c++']

sum('2', '3')                           # 23

Here you can see that the sum() function works well when it is invoked with either int values, string values, or even lists.

But the goal of the sum() function is to add two integers, and not two lists or strings. We can now provide type hints for the parameters in the Function definition indicating the type that we want to allow.

To indicate that we only want to allow int types we can change our function definition to look like:

def sum(a: int, b: int):
    return a + b

This informs the function caller that the arguments required for the sum() function should be integers not otherwise.

Similarly, to indicate only str types are allowed, we'd change our function to specify it as:

def sum(a: str, b: str):
    return a + b


Specifying A function's return type.

One could also indicate the return value of a function call :

def sum(number1, number2) -> int :
    return number1 + number1

This informs the caller that the return type of the sum() function should be an integer.