In Python, everything is treated as an object, making it a highly object-oriented programming language. Every object in Python has three core attributes:
a
, b
, and c
.These attributes together determine the behavior and characteristics of an object in Python.
my_str='plus2net.com'
print(my_str[4])
my_str[4]='5'
Above code will generate error saying TypeError: 'str' object does not support item assignment
my_list=[1,2,3]
my_list[1]=8
print(my_list) # [1, 8, 3]
Using a tuple ( immutable )
my_tuple=(1,2,3)
my_tuple[1]=8
print(my_tuple)
Above code will generate error saying TypeError: 'tuple' object does not support item assignment
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
Address (id ) of the string is changed when we assign a different value to it, as string is immutablemy_list = [5, 7, 5, 4, 3, 5]
print(id(my_list)) # e.g., 139787163608584
my_list += [9, 8]
print(id(my_list)) # e.g., 139787163608584
print(my_list) # [5, 7, 5, 4, 3, 5, 9, 8]
my_tuple = (1, 2, 3)
print(id(my_tuple)) # e.g., 139787173397704
my_tuple += (4, 5)
print(id(my_tuple)) # e.g., 139787174601112
print(my_tuple) # (1, 2, 3, 4, 5)
We can make elements of an iterator immutable or unchangeable by using frozenset()