Which of the following data types is immutable in python?

883    Asked by RutujaMishra in Python , Asked on Apr 4, 2021

Which of the following data types is immutable in python?

A. list

B. set

C. tuple

D. dict

Answered by Rutuja Mishra

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.



Your Answer

Interviews

Parent Categories