Objects in Python can be Mutable or immutable. 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() value will not change , whereas for immutable objects id() value will change. List is a mutable object but tuple is a immutable object.
Above code will generate error saying TypeError: 'tuple' object does not support item assignment
Comparing list( Mutable ) with string ( immutable )
When we assign a different value to a string variable we are not modifying the string object, we are creating a new string object.
But when we change any element of a list we are not creating a different object, we are only modifying the same object.
my_list=[1,2,3]
str1='welcome'
print("address of list : ", id(my_list))
print("address of string str1 : ",id(str1))
# change the values
# modified the list object, not a new object , address is same.
my_list[1]=7
print("address of list : ", id(my_list))
# str1[1]='p' # this will generate error
# new string object is created now,
# not modified the old one, address changed
str1='Python'
print("address of string str1 : ", id(str1))
Output
address of list : 140301581527624
address of string str1 : 140301582741432
address of list : 140301581527624
address of string str1 : 140302155689016