In Python, variables are created by writing their name and assigning with an equal ("=") the value or the expression that specifies this value. It is not necessary to specify the type of the variable since Python dynamically decides what type of variable it is according to its rules.
Example of declaration of a variable of type "str":
fruit="strawberry"
print(fruit)
Execution result of the above example:
developer@feitamespythontutorial:~$ python3 variablesstr.py
strawberry
developer@feitamespythontutorial:~$
As seen in the previous example, the "fruit" variable has not been declared of any type, but since the value is enclosed in quotes, it is of type "str". In Python, variables of type "str" can be declared with double or single quotes.
To see the type of a variable, use the "type" function:
fruit="strawberry"
print(fruit)
print(type(fruit))
Execution result of the above example:
developer@feitamespythontutorial:~$ python3 variablesstrtype.py
strawberry
<class 'str'>
developer@feitamespythontutorial:~$
Where you can confirm that it is type "str" since it has been declared with quotes.
If we declare the variable without double quotes and with an integer value, this variable would be of type "int":
players=7
print(players)
print(type(players))
Execution result of the above example:
developer@feitamespythontutorial:~$ python3 variablesinttype.py
7
<class 'int'>
developer@feitamespythontutorial:~$
Where you can confirm that it is of type "int" since it has been declared without quotes and with an integer value.
Python is a dynamically typed language, which means that in addition to not having to declare the type of the variable when creating it, it also means that the data type of a variable can change during execution. An example:
color = 20
print(color)
print(type(color))
color = "orange"
print(color)
print(type(color))
Execution result of the above example:
developer@feitamespythontutorial:~$ python3 variablesdynamictyping.py
20
<class 'int'>
orange
<class 'str'>
developer@feitamespythontutorial:~$
Where you can see that the variable "color" which is of type "int" is assigned a value between quotes and becomes of type "str", demonstrating dynamic typing.
Just because Python is a dynamically typed language does not mean that it is not a strongly typed language, which it is. Strong typing means that when an operation occurs on two variables of incompatible types, the language interpreter generates an error for that operation. An example:
cars = 5
motorcyles = "2"
print(cars + motorcyles)
Execution result of the above example:
developer@feitamespythontutorial:~$ python3 variablesstrongtyping.py
Traceback (most recent call last):
File "variablesstrongtyping.py", line 3, in <module>
print(cars + motorcyles)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
developer@feitamespythontutorial:~$
Where it is confirmed that it is a strongly typed language since it does not allow adding the value of an "int" variable and the value of a "str" variable.
Python allows you to put annotations on variables to indicate a static type system, although this will not prevent a variable that we have defined as "str" from changing in run mode to "int". Example:
color: int = 20
print(color)
print(type(color))
color = "orange" # This type change does not generate an error
print(color)
print(type(color))
Execution result of the above example:
developer@feitamespythontutorial:~$ python3 variablesanotationsforpseudostatictyping.py
20
<class 'int'>
orange
<class 'str'>
developer@feitamespythontutorial:~$
We confirm that it does not generate an error even if the typing of the variables is specified by annotation. Does this ability to specify static typing make sense when it is not respected?: really yes, since there are checkers that can be launched on the source code to verify that the type of the variable will not change during execution, some of these checkers are :
- pyre
- pytype
- mypy
- pyright: Available as the Visual Studio Code pylance extension