Variables have always been considered as a temporary storage location for some data. Most of the languages, allocate storage for the variables on the main memory. Though python also allocates the same way but here, variables are not storages. Instead, a variable is considered to be a reference to the value stored somewhere in the memory.
Consider the syntax below:
Here, ‘Vivek’ is a string stored somewhere in the memory and is being referred to the variable student. Similarly, 12 and 87.46 are two numeric values being referred to variables- roll and percentage respectively.
Python provides the following two specific features related to assignment:
It is the way of assigning values to a variable in which there is no any datatype attached with the variable. We can assign whatever kind of values to the variable.
Consider the syntax below:
After the code is executed, we’ll get the output:
This proves that datatype doesn’t restrict values being assigned to the variable and that is what dynamic typing is all about.
NOTE: In Static Typing, a certain datatype is attached with the variable and it restricts values of other datatypes to be assigned to that variable.
Python allows some wonderful assignments. Consider the following:
here, both the variables, roll1 and roll2 are referring to the same value but changing value of one variable will not have any effect on the other.
here roll1 is referring to 12 and roll2 is referring to 13 and both assignments are being done at the same time.
here values of roll1 and roll2 are swapped together. To understand this weird kind of assignment, we need to recall the fact that python variables are not storage locations but references to the values.