Which of the following statements is FALSE about mutable and immutable data types in Python?
a. Mutable data types can be changed after they are created, while immutable data types cannot.
b. Mutable data types include lists, dictionaries and sets, while immutable data types include strings, numbers and tuples.
c. Mutable data types can be used as keys in dictionaries, while immutable data types cannot.
d. Mutable data types can cause unexpected errors when they are shared by multiple variables, while immutable data types do not.
Answer:
Answer by student
c. Mutable data types can be used as keys in dictionaries, while immutable data types cannot.
Detailed answer by teachoo
-lock-
Let’s go through each of the options and see why they are correct or incorrect.
- Option a. Mutable data types can be changed after they are created, while immutable data types cannot.
This is TRUE . Mutable data types allow us to modify their values or elements after they are created, while immutable data types do not. For example, we can append a new element to a list, which is a mutable data type, but we cannot concatenate a new character to a string, which is an immutable data type.
- Option b. Mutable data types include lists, dictionaries and sets, while immutable data types include strings, numbers and tuples.
This is TRUE . Lists, dictionaries and sets are examples of mutable data types in Python, while strings, numbers and tuples are examples of immutable data types in Python. There are other mutable and immutable data types as well, such as bytes, bytearray, frozenset etc.
- Option c. Mutable data types can be used as keys in dictionaries, while immutable data types cannot.
This is FALSE . The opposite is true: immutable data types can be used as keys in dictionaries, while mutable data types cannot. This is because dictionary keys need to be hashable, which means they have a unique value that can be used to identify them. Immutable data types are hashable because their values do not change, while mutable data types are not hashable because their values can change. For example, we can use a string or a tuple as a key in a dictionary, but we cannot use a list or a set as a key in a dictionary.
- Option d. Mutable data types can cause unexpected errors when they are shared by multiple variables, while immutable data types do not.
This is TRUE . Mutable data types can cause unexpected errors when they are shared by multiple variables because changing the value of one variable will affect the other variables that refer to the same object.
So, the only false statement among the options is option c.
So, the correct answer is option c. Mutable data types can be used as keys in dictionaries, while immutable data types cannot.
-endlock-