Objects can be Mutable or immutable.
Mutable objects values can be chagned , whereas immutable object values can't be changed.
Identities remain same for mutable objects but changes for immutable objects.
Mutable and immutable object and id()
For mutable objects by changing the element the id() vlaue will not change , whereas for immutable objects id() value will change.
List is a mutable object but tuple is a immutable object.
Check how the identities of list and tuple are changing after adding elements.
Immutable objects are tuple, string, int
mutable objects are list, dictionary , set
Let us try one string ( immutable )
Above code will generate error saying TypeError: 'str' object does not support item assignment
Using List ( mutable )
Using a tuple ( immutable )
print(my_list) # [1, 8, 3]
Above code will generate error saying TypeError: 'tuple' object does not support item assignment
We can make elements of an iterator immutable or unchangeable by using frozenset()
in and not in: The Membership operators