Which of the following data types is mutable in Python?

Master Python with the PCAP Certification! Explore interactive quizzes and detailed explanations to ensure your exam success. Gain confidence and get certified!

In Python, mutability refers to the ability of an object to be changed after it has been created. Lists are mutable, which means you can modify their contents without creating a new object. You can add, remove, or change elements in a list dynamically.

For example, if you have a list defined as my_list = [1, 2, 3], you can append a new item like this my_list.append(4), and my_list will then contain [1, 2, 3, 4]. Similarly, you can also modify an existing element by indexing into the list, such as my_list[1] = 5, which would change the list to [1, 5, 3, 4].

On the other hand, strings, tuples, and integers in Python are immutable. Once created, their values cannot be changed. For instance, attempting to change a string directly or to modify a tuple results in a TypeError because these data types do not allow for modification of their content. Thus, lists represent the only mutable type among the options provided.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy