Which of the following is a mutable data type in Python?
a) tuple
b) string
c) list
d) int
Answer:
c) list
Explanation:
In Python, a list is a mutable data type, meaning its elements can be changed after the list is created. You can add, remove, or modify elements within a list. This flexibility makes lists one of the most commonly used data structures in Python.
Here’s an example of modifying a list:
my_list = [1, 2, 3]
my_list[0] = 10 # my_list is now [10, 2, 3]
On the other hand, tuples, strings, and integers are immutable, meaning their values cannot be changed after they are created. For instance, attempting to modify a string or tuple will result in an error.
Understanding the difference between mutable and immutable types is crucial when working with collections and data structures, as it affects how you can manipulate data in your programs.