A variable represents a memory location in which some value of a particular data type is stored. This data type can be integer, string, list, tuple,
and so on. A variable is an alternative easy way to store and retrieve data in the memory.
When we store anything in the memory, our operating system allocates the required amount of space and give it a memory address. But these memory addresses are in alphanumeric form and very hard to remember, e.g. 02tt34, 8765th, etc. So, if we want to retrieve the stored data we have to provide these addresses to the system. It would be a very hectic process.
To avoid this problem, a human readable name is given these address spaces so that we can access them very easily. Ex: name of a song, movie, file stored in your PC.
A variable is also a name or an identifier of a memory location that has some data stored in it. So instead of using memory addresses, one can easily access that data by using its variable name. For example: name="Jack", age=45; the variables are name
and age
with values Jack
and 45
.
Allocates is a name which is used to refer memory location. In simple it is a reserved memory location to store values, also known as identifier. Variable while declaring it. The interpreter implicitly binds the value with its type. Python enables us to check the type of the variable used in the program.
Python is a type infer language, i.e., variable's data type need not to be declared. Just store valid value in the variable and Python will automatically detect its data type. It is to be noted that each variable in Python is an object (Read: Data types in Python).
To store data in a python variable just write variable_name=value
. Ex. a=10
, name="Rohit"
. You have to remember that variable name is case sensitive. What does it mean? Continue reading below!
Variable names can be very short of only one character(a=10, f=5, _ab="true") and also very long as needed by the developer. But it is always recommended to use meaningful variables names(Student_name, address) rather than single characters.
Variables names are bounded within certain rules. If these rules are violated then it will be an invalid variable, the rules are:
In Python, variables are assigned values using assignment operator '='
. For example: a=10, Employee_name="Kylie". Have you noticed the use of inverted commas in string type data? It is a rule to enclose a string within single(' ') or double(" ") or triple(''' ''') inverted commas, otherwise it will be an invalid string.
For integer and float data type just enter the values as they are without any inverted commas, and it will be stored in the memory. Check out the example below to see how to assign values for different data types in python.
You can change variable's value by assigning a different value to the same variable. However, when you change values then different data types behave differently. Some override the existing value, others leave the previous memory space and allocate a new memory space for the new value. You can learn more about it in our data type in python tutorial.
Look at the example below to understand how to change variable value in python.
In python, you can assign multiple variables in a single line. Just write all variable names to the left side of assignment operator and the respective values to its right side. Look at the example below:
If you have more than one variable with same value, then there is no need to assign values separately for each variable. Look at the example below to understand how to assign same value to multiple variables at once:
Follow Us: