Variable is used to store the value in the memory. Thus the name given to the memory that stores the value is called variable.
Let's see how to assign a value to a variable.
a=10
print(a)
# output:
#10
Description:-
= is the assignment operator. The assignment operator can be called value on the right hand side and variable on the left hand side. Let's see how the above program works. First what the line a = 10 does is that first a memory is created for the variable name a
Stores the value 10 in the created memory. Print(a) line displays the value 10 in the variable name a.
a=10
b=20
print(a)
print(b)
# output:
#10
#20
Description:-
First, memory is created for the variable a and the value 10 is stored in it.
Next, memory is created for the variable b and the value 20 is stored in it.
Next the 2 values a and b are printed using the print function.
a,b=30,40
print(a)
print(b)
# output:
#10
#20
Description:-
First memory is created for 2 variables a and b
In which the value 30 is stored in the variable a and the value 40 is stored in the variable b.
The next line uses the print function to display the values stored in the a and b variables.
What is given in two lines is given in one line. This reduces the number of lines.
a=b=5
print(a)
print(b)
# output:
#5
#5
Description:-
A memory is created for the variables a and b and the value 5 is stored in it.
Next, the value of the variable is printed using the print( ) function.
Terms of Use of Variable:-
A variable's name must begin with an alphabet or an underscore.
Variable cannot start with numbers.
Ex:
2 values
A python variable is case sensitive.
Ex:
a=5
print(A)
Here I am using the variable a in lowercase, but when printing I am printing in uppercase. This is giving me an error. This is called case sensitive. The variable we are using should be used similarly everywhere else we will get an error.
Do not use python keyword for variable name.
Ex:
if, while, for…Etc.