Which of the following data types is immutable in python?
Which of the following data types is immutable in python?
A. list
B. set
C. tuple
D. dict
The answer is option “C” Tuple.
The tuple is immutable i.e we can change the elements, delete the existing elements, and cannot add new elements. Here is an example of what will happen if you try to reassign an element in a tuple:
a = (1, 2, 3, 4)
a[0] = 1
On running the above code, you will get an error message “TypeError: 'tuple' object does not support item assignment”.
The datatypes like int, float, bool, str, tuple, and Unicode are immutable. Datatypes like list, set, dict are mutable.