One of the most crucial part of learning any programming language is to understand how the data is stored and manipulated in that language. Python users are often inclined towards python because of its ease of use and various number of versatile features it provides. One of those features is dynamic typing.
In python, unlike statically typed languages like C or Java, there is no need to specifically declare the data type of the variable. In Dynamically Typed languages such as Python, the interpreter itself predicts the datatype of the variable based on the type of value assigned to that variable.
Watch this Python Datatype video
In C, the declaration of any variable might look like the following code block:
The equivalent code for the above operation in Python would look like the following code block:
As we can observe from the above example, in C, all the variables are explicitly declared with their respective datatypes while in Python, we have directly taken a variable and then just assigned a value to it rather than declaring the datatype of that variable as well. In Python the datatypes of the variables are dynamically inferred. You can assign any value to the variable and the Python interpreter will identify the datatype. You can also re-assign a different type of value to an already assigned variable, unlike in C or Java where if you have declared a variable integer and then assign a string value to it, you will get an error.
The equivalent code for the above operation in C would look like:
This type of flexibility that comes as a result of being dynamically typed language is one of the reasons that Python is considered to be easy and convenient to learn and use.
As the name suggests, Datatype is the classification of type of values that can be assigned to variables. We have already seen that here in Python, we don’t need to declare a variable with explicitly mentioning the data type, but it’s still important to understand what are the different types of values that can be assigned to variables in Python. Based on the type of variable’s value, the datatype of that variable is inferred by the Python interpreter.
Python datatypes are categorized in two categories, that is:
Following diagram lists the datatypes that fall under the category of mutable datatype and immutable datatype.
Following are the above mentioned standard datatypes in python:
This brings us to the end of this module. The above-mentioned Datatypes will be discussed in detail in their respective separate modules. See you there!Previous Next
Download Interview Questions asked by top MNCs in 2019?