variables in python

Variables


Variables are names used to hold one or more values.. A variable is a way of referring to a storage area in a computer program.Values stored in a variable can be changed. Variables are just like named labels. For example, you can store name of a student in a variable “stud_name” or simply “name”.
When you create a variable you reserve some space in memory.

Unlike other programming languages you don't need to specify the type of variable and there is no command to declare a variable. The declaration happens automatically when you assign a value to a variable.

For example, 

roll_no=121 

Creates a variable namely “roll_no” of numeric type.


Other examples:

name=’Ankita’                 #variable of type string

percentage=76.9             #variable of type floating point

Naming of variables should follow the rules of identifier naming rules.

Dynamic Typing

The data type of variable can be changed at anytime.

For example,

x=12                     #x is an integer

x=”hello ”            #x is now string

Important thing

Variables are not storage containers in python

In many programming languages, variables are best thought of as containers or buckets into which you put data. For example,consider the statement in C or Java 

int a=10;
here variable a is created as a container or bucket at a memory address say 1001, and value 10 is stored in the container a.

Now the statement
a=29;
will change the contents of the variable a.

Note that, the location or address of the variable did not change.

But this is not the case with Python.

In Python, variables are best thought of as pointers. So in Python, when you write

a=10

you are essentially defining a pointer named "a" that points to some other bucket containing the value 10.

It so happens because python preloads some commonly used values in an area of memory. This area is called front-loaded data space. This data space has literals/values at defined locations, and each location has a memory address.

Addresses may vary on different computers.
When you write
a=11
variable "a" will be created and it will point to memory location where 11 is stored.
Now when you write
a=14
variable a will point to the value 14

We can verify this concept  through a program.

Write the following code in IDLE
a=20
print("value of a is ",a)
print("address of a is ",id(a))
print("address of 20 is ",id(20))

Output

value of a is  20
address of a is  1777203424
address of 20 is 1777203424

Now change the value of a
a=22
print("value of a is ",a)
print("address of a is ",id(a))
print("address of 22 is",id(22))

output:

value of a is  22
address of a is  1777203456
address of 22 is 1777203456



Multiple Assignments

Python allows Assigning multiple values or the same value to multiple variables in one line of code.

Example 1:
Assigning same value to multiple variables:

x=y=z=100

x,y,z will refer to same location with value 100. 

Example 2:
Assigning multiple value to multiple variables:

x,y,z=1,2,3

value of x will be 1
value of y will be 2
value of z will be 3

Swapping values of two numbers:

x,y=1,2
print(x,y)
x,y=y,x
print(x,y) 

Output

1 2

2 1

Some examples

While assigning values through multiple assignments, python first evaluates the RHS expression and then assigns them to LHS

for example,


x,y,z=10,15,20
z,y,x=x+1,y-2,z+1
print(x,y,z)

in the above code, x,y,z=10,15,20
will make
x=10
y=15
z=20

now
z,y,x=x+1,y-2,z+1
will cause
z,y,x=11,13,21
so
x=21
y=13
z=11

now
print(x,y,z)
will output


21 13 11


2. What will be the output of following code

x=30 

y,y=x+3,x+10

print(y)

output:

40

Explanation

RHS  x+3,x+10
Will become 33,40
y,y=x+3,x+10 will become
y,y=33,40
firstly it will assign first RHS value to first LHS variable i.e.,
y=33
then it will assign second RHS value to second LHS variable i.e.,
y=40


Questions for you

1. Find the output
x,y=12,18
x,y=y,x-9
print(x,y)

2. Find the output
x,y=10,5
x,y,x=x+2,y+4,x+3
print(x,y)


3. x,y=10,5
y,z=x+2,x+4
print(x,y)





Comments

Popular posts from this blog

working with python IDLE

escape sequence in python

Data Types in Python