Static vs. Dynamic Typing

In many programming languages, you specify a type for a variable upon creation. In those languages, you can change the value of the variable, but the new value must be consistent with the specified type of the variable. Languages with this property are referred to as being statically typed.

Python is a dynamically typed language. In such a language, we do not specify the type of a variable when we create it. We specify only a value, and Python infers the data type from that value. If we change the value of the variable in a way that is inconsistent with the current type of the variable, then Python simply changes the variable’s type.

Dynamic typing is demonstrated in the two cells below.

p = 4
print(type(p))
p = 3.999
print(type(p))
<class 'int'>
<class 'float'>
q = 7
print(type(q))
q = 7 / 1
print(type(q))
<class 'int'>
<class 'float'>