Variables and Objects¶
Let us get an overview about variables and objects in Python. In Python we need not define data types for variables or objects.* Data types are inherited based up on the values assigned to the variables.
- We can check the type of the variable or object using
type
function. - Python is interpreter based programming language which means it does not go through compilation and hence data types are not validated until run time.
- Python variables or objects are dynamically typed. In case of compiler based programming languages such as Java, Scala etc variables or objects are statically typed.
- We can specify data types for variables or objects starting from Python 3. However it is only informational and does not enforce.
In [1]:
i = 10
In [2]:
type(i) == int
Out[2]:
True
- Starting from Python 3 we can specify the data type for the variables or objects.
- However it is not enforced
In [3]:
j: int = 10 # You can specify data type starting from Python 3
In [4]:
j: int = 'Hello'
# I am able to assign string to j even though data type is specified as int
In [5]:
print(j)
Hello
In [6]:
type(j)
Out[6]:
str
In [7]:
type(j) == str
Out[7]:
True