Tuesday, August 16, 2016

Mutable & Immutable data types in Python



In my previous article, I have already explained :

  1. Installation of Python
  2. Python Syntax
  3. Printing “Hello Python”
  4. Different data types and their declarations
  5. Function, Loop and Conditional Controls 

I have also mentioned that, String Number and Tuple are Immutable whereas List and Dictionary are Mutable in nature. But, can we prove it ? Yes, of course, Python has a function id(), which returns the memory id of a variable. This we can use to understand Python data types and their memory allocations.


Now, we will declare a variable for each below mentioned data types and then we will change it’s value to something else and validate its object id.

String (Immutable)


>>> name="AppTech Solution"
>>> print name
AppTech Solution
>>> id(name)
4503631496        #object id
>>> name="Welcome to AppTech Solution"
>>> print name
Welcome to AppTech Solution
>>> id(name)
4503615728        #object id changed
>>> 

Number (Immutable)


>>> emp_id=12
>>> print emp_id
12
>>> id(emp_id)
140410018119968      #object id
>>> emp_id=20
>>> print emp_id
20
>>> id(emp_id)
140410018119776      #object id changed
>>> 

List (Mutable)

The values of List are enclosed with curly bracket []. We expects object id should remain same, even we manipulate its content.


>>> var_list=[3,5,"Ram"]
>>> print var_list
[3, 5, 'Ram']
>>> id(var_list)
4503638456           #object id
>>> var_list[0]=30   #changing value for existing index
>>> print var_list
[30, 5, 'Ram']
>>> id(var_list)
4503638456           # object id remains same
>>>

Tuple (Immutable)

Similar to List, but values are enclosed with small bracket (). We expects object id should change whenever value gets changed



>>> var_tup=("alpha",34,"beta")
>>> print var_tup
('alpha', 34, 'beta')
>>> id(var_tup)
4503411760                                #object id
>>> var_tup=(“alpha",34,"beta","gamma")   #assigning new value
>>> print var_tup
('alpha', 34, 'beta', 'gamma')
>>> id(var_tup)
4503316888                                #object id changed
>>> 

Dictionary (Mutable)

They are similar to hash-map and values are enclosed with curly bracket {}. We expects object id should remain same, even we manipulate its content.


>>> laptop={}
>>> laptop["hp"]=30000
>>> laptop["dell"]=35000
>>> print laptop
{'hp': 30000, 'dell': 35000}
>>> id(laptop)
4503636800                       #object id
>>> laptop[“dell"]=45000         #changing value for existing key
>>>print laptop
{'hp': 30000, 'dell': 45000}
>>> id(laptop)
4503636800                       #object id remains same
>>> laptop[“acer"]=15000         #adding new value
>>> print laptop
{'acer': 15000, 'hp': 30000, 'dell': 45000}
>>> id(laptop)
4503636800                       #object id remains same
>>>



You could see that, for String, Number and Tuple object id gets changed when we changes its value, but this wasn’t the case for List and Dictionary. This means every time, when we change values for String, Number and Tuple, then its actually creating a new object for it and assigning value to it.



No comments:

Post a Comment