To learn the first lesson about programming in Python, please visit Start programming in python - python tutorials - Lesson 1
In this tutorial, we are going to learn a little about variablesUsing variables in Python:
In the last tutorial, we learned a very few about variables. In this lesson, let us see a little more about them.
Variables are characters or group of characters that can be used to save other values within them. These stored values can be changed at any time. Hence, a variable's value can be changed.
Consider the following example:
>>> a = 32
>>> print (a)
Here, we are first assigning the variable a
to a value of 32
. Hence, the value 32
is stored in a
. Then, when we ask the compiler to print a
, it prints the value of a
and not exactly a
Hence, it is understood that always a constant value is preferred to be printed first and not a variable.
Don't be confused whether to leave space in between the characters or not. Python usually omits the blank spaces in most cases. Hence the above program can also be written as :
Assigning values and operating the variables:
Instead of directly operating two or more numbers, we can assign the values to specific variables and can then use those variables to multiply. Consider the following example :
>>> a = 324
>>> b = 18
Here, when we add the two variables , the values are added and are displayed on the screen. Also, see the following example:
>>> a = 324
>>> b = 18
>>> c = a+b
In the above example, when we give c = a + b
the value 342 is saved in c and it is printed only when we type c
Programming in Python is case sensitive. This means that if we use Print
instead of print
instead of input
, it will result in error.
You can also use more than two variables at a time.
>>> p = 324Operating constants and variables:Example 1 :
>>> q = 233
>>> r = 75
>>> s = 941
>>> a = 231Example 2:
>>> b = 23
>>> a + b + 19 + 98
>>> a = 43How to make the user to assign any value to a variable:
>>> a ** 2
Till now, we just saw how to assign variables while programming. The following example is going to show you how to assign a value to a variable during the execution of the program.
Consider the following example :
>>> p = input ("Please enter your number : ")
Please enter your number : 24
(Case : If the input number is 24)
In the above example, when we enter the number as 24, its value is saved inside the variable p
. Thus we can actually assign a value to a variable using the input()
The string inside the bracket will be printed before the input value.
Thanks for reading! In the next lesson, we'll learn about functions and modules.
*** Please do not forget to leave your comments about this article ***