Click for All Topics
In Python, mutability refers to the ability of an object to be changed after it has been created. If, an object can change it’s value after being created then it is known as mutable otherwise if it’s unable to change the value then it is immutable.
Mutable Objects:
Example –
my_list=[1,2,3,4,5]
print("Original list:",my_list)
my_list.append(6)
print("Original list after add a new element:\n",my_list)
my_list.remove(3)
print("Original list after remove 3 from list:\n",my_list)
# Output -
# Original list: [1, 2, 3, 4, 5]
# Original list after add a new element:
# [1, 2, 3, 4, 5, 6]
# Original list after remove 3 from list:
# [1, 2, 4, 5, 6]
my_dict={'a':1,'b':2,'c':3,'d':4}
print("Original Dictionary:",my_dict)
my_dict.update({'e':5})
print("Original Dictionary after add new item:\n",my_dict)
del my_dict['b']
print("Original Dictionary after removing a item from dictinaory:")
print(my_dict)
# Output -
# Original Dictionary: {'a': 1, 'b': 2, 'c': 3, 'd': 4}
# Original Dictionary after add new item:
# {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
# Original Dictionary after removing a item from dictinaory:
# {'a': 1, 'c': 3, 'd': 4, 'e': 5}
my_set={1,2,3,4,5,6}
print("Original set:",my_set)
my_set.add(7)
print("Original set after add new item 7:\n",my_set)
my_set.discard(4)
print("Original set after removing 4 from set:\n",my_set)
# Output -
# Original set: {1, 2, 3, 4, 5, 6}
# Original set after add new item 7:
# {1, 2, 3, 4, 5, 6, 7}
# Original set after removing 4 from set:
# {1, 2, 3, 5, 6, 7}
Example –
# Original integer
a = 5
# Performing an operation on the integer (creating a new object)
b = a + 3
# Printing the original integer and the new one
print("Original integer:", a)
print("New integer:", b)
# Output -
# Original integer: 5
# New integer: 8
Example –
# Original floating-point number
a = 3.5
# Performing an operation on the floating-point number
# (creating a new object)
b = a * 2
# Printing the original floating-point number and the new one
print("Original float:", a)
print("New float:", b)
# Output -
# Original float: 3.5
# New float: 7.0
Example –
my_string='edSlash'
print("Original String:",my_string)
my_string[1]='anurag' # will give an error
# Output -
# Original String: edSlash
# Traceback (most recent call last):
# File "/home/main.py", line 4, in
# my_string[1]='anurag' # will give an error
# TypeError: 'str' object does not support item assignment
# Creating a new string
my_string = 'Hello'
print("Orignal String:",my_string)
new_string = my_string + 'World!'
print("New string:",new_string)
# Output -
# Orignal String: Hello
# New string: HelloWorld!
Example –
my_tuple = (1,2,3,4)
print("Original tuple:",my_tuple)
# try to modify my_tuple
my_tuple[0]=9 # will give an error
# Output -
# Original tuple: (1, 2, 3, 4)
# Traceback (most recent call last):
# File "main.py", line 5, in
# my_tuple[0]=9
# TypeError: 'tuple' object does not support item assignment
Understanding mutability is crucial when you’re working with data structures:
Office:- 660, Sector 14A, Vasundhara, Ghaziabad, Uttar Pradesh - 201012, India