What is a Variable?
A variable is like a container that can store a value in
it. For example: x=2. Here, ‘x’ is the variable(or the container) which stores
the value ‘2’. Now if you print ‘x’ using the command ‘print(x)’, the output
will be 2.
Using
multiple Variables:
You can also use multiple variables to store multiple
values. For example:
x=2
y=3
Now, print the values of ‘x’ and ‘y’ by typing the
following commands:
print(x)
print(y)
And run the program. The output will be :
2
3
Variables are
called so because YOU CAN CHANGE THE
VALUES STORED IN THEM ANYTIME AND ANYWHERE IN THE PROGRAM. For example,
x=2
x=3
Now, what do you think the output would be when you print
‘x’??
It will be ‘3’
It is because ‘3’ is the newest value you assigned to the
variable ‘x’ and now the value ‘2’ is completely lost and the new value of ‘x’
is ‘3’.
Adding
Variables:
Adding variable means adding the values stored in them
using the ‘+’ operator.(I will tell you what an operator is and what are the
different types of operators in later posts, so stay tuned)
For example,
Here we added ‘x’ and ’y’ which had the value ‘2’ and ‘3’
respectively and got the result ‘5’.
We can also increment the values in the variables in the
following ways:
OR
Now we have incremented the value of ‘x’ by ‘10’.
Multiplying
Variable:
Similarly, we can also multiply two or more variables
using the ‘*’ operator.
For example,
Here we multiplied the values stored in variables ‘x’ and
‘y’. And got the result ‘6’.
Just like in addition, we can also multiply variables
with numbers thus increasing their value just like in this example:
OR
Here we multiplied the value of ‘x’ by ‘5’ and changed
the value of ‘x’ to ‘10’.
String
variables:
Just like in the above examples where we had integers as
values stored in variables, we can also have Strings as values stored in
variables. For example:
CONGRATULATIONS!!!! You now have a basic idea about variables in Python. We will discuss on how to use these variables on a higher level in later posts, so stay tuned!!
If you are a beginner, intermediate, advanced or just someone interested in programming, feel free to join or telegram channel and be among people like you:
0 Comments
Welcome to the comments section, this is where you can contact me personally for any doubts or feedback