In previous tutorial, I had mentioned how to create a python file and run it. Lets move a step further and lets understand what is a variable and how to declare it.
Lets consider a situation. Suppose in my program code, I need to print 5 times the addition value of 5 and 10. Therefore the code would be as below.
The above code would print 15 five times.
Now suppose instead of 5 times, lets say the requirement is to print the addition of 5 and 10 for 10000 times. Therefore, I need to copy paste the above print code for 10000 times. (Ofcourse there are plenty other best solution available to repeat a task (printing addition of 10 and 5) for 10000 times but this tutorial purpose lets go with the most simple solution that is copy and paste 5 and 10 for 10000 times).
So far there is no problem. Now assume, after copy pasting the print code for 10000 times there is another requirement came to print addition of 20 and 10 instead of 5 and 10.
Then I have to remove all the ten thousand line and copy paste again print(20+10) for 10000 times. What if another requirement comes to print addition of 30 and 50 instead of 20 and 10.
So there must be some smart way to proceed in the world of programming. And that is, storing value first in something and then perform the required task.
Here the task is to print addition value of two numbers. So if we store these two numbers first in two different number holder and then print the addition of the two number for the required times is definitely a wise approach.
It will still print the addition value of 20 and 30. And if for any reason a task comes to print addition value of 50 and 100 then I need to change the value of number 1 and number 2 and I do not require to touch any other code. It will fullfill the new requirement.
Here I have stored 20 in number1 and 30 in number2. number1 and number 2 are known as “VARIABLE”
We declare variable to store values in it. We can use any name that we want for a variable name and the name should not contain space or special character.
Above are few examples of variable declaration.
What is a variable?
Variable is a name reference to a memory location where an assigned value is stored.
In above example,
name=”srikant” means at the backend a memory space is created which is referred as ‘name’ and that memory location contains the value ‘srikant’
If you look at above example, then you would notice there are few value which are kept in between double inverted quote and others are not.
When ever we assign a text information to a variable, we always keep the text information in between single quote or double quote. Where rest of other types of information are not kept in between single or double quote.
In the above example, srikant, web developer and django framework are text information, so I had kept in between quotes. How ever 30 and 35.6 are numbers, so I had not kept any quote around these values.
In programming world, text value are known as STRING, number are known as INTEGER and decimal number are known as float.
In some other tutorial, I am going to discuss in detail different types of data types.
So this is the way we declare a variable and assign value to it.