Variables in Python

Swadhin Ray
CERTIFIED EXPERT
Published:
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
3,388 Views
Swadhin Ray
CERTIFIED EXPERT

Comments (7)

CERTIFIED EXPERT

Author

Commented:
Great catch PEPR....

It is based on the data type of a value that we need or planing to assign it.

Let me know if anything else I need to change....
CERTIFIED EXPERT

Commented:
It is based on the data type of a value that we need or planing to assign it.
But this is not related to a variable in Python. Any variable in Python takes the space for the name plus 4 or 8 bytes (depending on whether it is 32-bit or 64-bit Python. There is nothing like a placeholder in memory that will later be filled by some value.
CERTIFIED EXPERT

Author

Commented:
When ever we create a variable it reserve some space in memory.
CERTIFIED EXPERT

Author

Commented:
We can also say as variable is something like a bucket where we can put anything that we want.
But space I still agree.
CERTIFIED EXPERT

Commented:
Well, but if the object takes say 1 kB, it is created before any Python variable for that object is created. Variable in Python means something else than variable say in C++. A variable in Python is unrelated to the memory footprint of the object. When the unnamed object exists, the variable is simply the name bound externally to the object. The name is not translated to the address of memory space of the object. The name is not directly related to the placeholder of the object.

Assigning the above mentioned object with say 1 kB foot print means that say few bytes more are allocated -- much less than the object size. The object is not copied. It is only shared via the variable. The variable can immediately be assigned another object. If the other object already existed, no extra memory is allocated in the case.

View More

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.