<

Variables in Python

Published on
9,075 Points
3,075 Views
Last Modified:
Approved
Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the data type of a variable for what we can store in the reserved space.
Let us see now how we can create a variable in Python. First we need to create a variable name and then we can assign it to a value.

Example of a simple variable:
Let us create a variable name called “X” and assign a values to it for example take 20. As like below:

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> x= 20
>>> 

Open in new window


Note: Type “x=20” and hit enter.

So now we can use variable ‘x’ i.e. 20 can be used where ever we want to use it , where ‘x’ represent the value 18.
If we want to do a sum like 20+ 50 which should result me 70 but in place of 20 I can the assigned variable i.e. ‘x’.
Below example shows how to do that:
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> x= 20
>>> print x+50
70
>>> 

Open in new window


Now for example we want to do an exponent 3 then the result will be like:
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> x= 20
>>> print x+50
70
>>> print x ** 3
8000
>>> 

Open in new window

From the above example we can see how to assign single variable for a value which we can use it later in our code.
Now let see how we can use multiple variables for different values.  So from above we know that x which represents value 20 and now we will take another variable called ‘y’ where we will assign value 10 to it and do a sum for x+y then the result will be 30 i.e. 10+20= 30..
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> x= 20
>>> print x+50
70
>>> print x ** 3
8000
>>> y = 10
>>> print x+y
30
>>> 

Open in new window



Variables are always useful when we are not sure what will be the value that need to be assigned, in that case using variable is useful.
Now for say we don’t know what will be the value that we want to add or say the user need to input the value for x and y then we can do that by using a function called ‘INPUT’  for example :

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> x= input("Enter the first value")
Enter the first value 10
>>> print x
10
>>> 

Open in new window


So when we say ‘INPUT’ it will ask the user to input a value where that value is going to be represented by variable X.
Take another variable called y and let the user assign a value for it. And do a sum for x+y . The below figure shows how to assign variables by input:

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> x= input("Enter the first value")
Enter the first value 10
>>> print x
10
>>> y =input("Enter the second value")
Enter the second value 20
>>> print x+y
30
>>> 

Open in new window


The function input() allows the user to assign their own variable instead of assigning by us to do it .
0
Author:Swadhin Ray
Ask questions about what you read
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.
Get 7 days free