Mutable and Immutable Data Types in Python

Working of Python: In python, variables are stored as references to a value object. Each time you change the value, the variable’s reference memory address changes. Python frontloads some commonly used values in memory. Each variable referring to that value actually stores that memory address of the value. Multiple variables/identifiers can refer to a single value. Internally python keeps count of how many identifiers/variables are referring to that value.

>>> a=10
>>> id(a)
1848147470928

>>> b=10
>>> id(b)
1848147470928

Concept of Mutability: Data Types/Objects are called mutable whose value can be changed in place ie if once created, we can change the objects without changing its address. In python Following types are mutable
1) Lists
2) Dictionary
3) Sets
By in-place, we mean that, any operation that changes the content of anything without making a separate copy of it.

Lists are mutable: We can directly add, remove, or update the values of a list in Python, without creating a new list.

>>> list1=[3,4,5]
>>>list1
[3, 4, 5]
>>> id(list1)
2428427644416

>>> list1[2]=50
>>> list1
[3, 4, 50]
>>> id(list1)
2428427644416

>>> list1.append(20)
>>> list1
[3, 4, 50, 20]
>>> id(list1)
2428427644416

>>> list1.remove(4)
>>> list1
[3, 50, 20]
>>> id(list1)
2428427644416

>>> list1.extend([30,40])
>>> list1
[3, 50, 20, 30, 40]
>>> id(list1)
2428427644416

Dictionaries are mutable: We can directly add, remove, or update the values of a dictionary in Python, without creating a new dictionary.

>>> dict1={1:"garg",5:"academy"}
>>> dict1
{1: 'garg', 5: 'academy'}
>>> id(dict1)
1343175979328

>>>#updating an already existing value in dictionary
>>> dict1[5]="math and computer academy"
>>> dict1
{1: 'garg', 5: 'math and computer academy'}
>>> id(dict1)
1343175979328

>>>#adding a new key:value pair in dictionary
>>> dict1[7]=9467863365
>>> dict1
{1: 'garg', 5: 'math and computer academy', 7: 9467863365}
>>> id(dict1)
1343175979328

>>>#removing from dictionary
>>> del dict1[1]
>>> dict1
{5: 'math and computer academy', 7: 9467863365}
>>> id(dict1)
1343175979328

Sets are mutable: We can directly add, remove, or update the values of a set in Python, without creating a new set.

>>> set1={1,2,3}
>>> set1
{1, 2, 3}
>>> id(set1)
1788730805600

>>> set1.add(4)
>>> set1
{1, 2, 3, 4}
>>> id(set1)
1788730805600

>>> set1.update({5,6,7})
>>> set1
{1, 2, 3, 4, 5, 6, 7}
>>> id(set1)
1788730805600

>>> set1.remove(2)
>>> set1
{1, 3, 4, 5, 6, 7}
>>> id(set1)
1788730805600

Concept of Immutability: Data Types/Objects are called immutable whose value can’t be changed in place ie if once created, we can’t change the objects without changing its address. If we will change the contents, a new object with the same name is created. In python Following types are immutable
1) Numeric types (int, float, complex)
2) boolean
3) tuples and
4) Strings

Numeric types are immutable: Once created, we cant change the contents of the numeric types in place.
If we will change the contents, a new object with the same name is created.

Integers are immutable:

>>> a=10
>>> a
10

>>> id(a)
1788688886352

>>> a=200
>>> a
200

>>> id(a)
1788689080976

Floats are immutable:

>>> b=10.5
>>> b
10.5

>>> id(b)
1788730352816

>>> b=200.50
>>> b
200.5

>>> id(b)
1788730353648

Complex Numbers are immutable:

>>> c= 5  + 8j
>>> c
(5+8j)

>>> id(c)
1788730355280

>>> c=10 + 7j
>>> c
(10+7j)

>>> id(c)
1788730355344

Boolean Types are immutable: Once created, we cant change the contents of the boolean types in place.
If we will change the contents, a new object with the same name is created.

>>> a=True
>>> a
True

>>> id(a)
140706062690408

>>> a=False
>>> a
False

>>> id(a)
140706062690440

Tuples are immutable: Once created, we cant change the contents of the tuple.

>>> tuple1=(2,3,4)

>>> tuple1[2]=20
Traceback (most recent call last):
  File "<pyshell#28>", line 1, in <module>
    tuple1[2]=20
TypeError: 'tuple' object does not support item assignment

>>> tuple1.append(10)
Traceback (most recent call last):
  File "<pyshell#29>", line 1, in <module>
    tuple1.append(10)
AttributeError: 'tuple' object has no attribute 'append'

>>> tuple1.remove(4)
Traceback (most recent call last):
  File "<pyshell#30>", line 1, in <module>
    tuple1.remove(4)
AttributeError: 'tuple' object has no attribute 'remove'

Strings are immutable: Once created, we cant change the contents of the strings.

>>> string1="gargs academy"
>>> string1
'gargs academy'

>>> string1[3]='P'
Traceback (most recent call last):
File "<pyshell#45>", line 1, in <module>
string1[3]='P'
TypeError: 'str' object does not support item assignment
error: You can only copy the programs code and output from this website. You are not allowed to copy anything else.