Variables in Python

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
>>>

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
>>>

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
>>>

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
>>>

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
>>>

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
>>>

The function input() allows the user to assign their own variable instead of assigning by us to do it .

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s