Junior — Middle
What categories of data can be changed after their definition?
sobes.tech AI
Answer from AI
In Python, mutable data types are:
- Lists — elements can be changed, added, or removed.
- Dictionaries — key-value pairs can be modified.
- Sets — elements can be added or removed.
Immutable types include:
- Numbers (int, float, complex)
- Strings
- Tuples
Example of a mutable list:
my_list = [1, 2, 3]
my_list.append(4) # Now my_list = [1, 2, 3, 4]
Example of an immutable string:
my_str = "hello"
# my_str[0] = 'H' # Error, strings cannot be changed directly