<

Variables in Python

Published on
8,749 Points
2,749 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
Comment
Author:Swadhin Ray
  • 4
  • 3
7 Comments
 
LVL 29

Expert Comment

by:pepr
Hi slobaray,

My objections start with the very first paragraph. Simply said, this is not true for Python.

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.

Variables in Python have no data type. Variables are just string names bound to the reference value to the target object. The target object is the one that occupies the memory of the size related to its type and content. The target object already exists when the assignment is to be done. Creating a Python variable means to allocate memory for the string that captures its name and say 4 or 8 bytes to capture the reference value to the target object. They become the key and the value in the internal dictionary. Assignment in Python simply means copying the reference value.
0
 
LVL 29

Expert Comment

by:pepr
input() is very dangerous operation. You should avoid using it. Python 3 removed the old input() function. The Python 3 input() is the same as is raw_input() in Python 2.
0
 
LVL 17

Author Comment

by:Swadhin Ray
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....
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
LVL 29

Expert Comment

by:pepr
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.
0
 
LVL 17

Author Comment

by:Swadhin Ray
When ever we create a variable it reserve some space in memory.
0
 
LVL 17

Author Comment

by:Swadhin Ray
We can also say as variable is something like a bucket where we can put anything that we want.
But space I still agree.
0
 
LVL 29

Expert Comment

by:pepr
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.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Join & Write a Comment

Suggested Articles

Title Views Activity
Python illustrated (part 2) 4,291
Python illustrated (part 3) 5,204
Modules in Python 2,178
Strings in Python 2,293
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Next Article:

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month