Junior — Middle
How do you determine if a data type's value can be changed after its declaration?
sobes.tech AI
Answer from AI
In Python, data types can be divided into mutable and immutable. Mutable objects can be changed after creation, while immutable ones cannot.
Examples:
- Mutable: lists (list), dictionaries (dict), sets (set)
- Immutable: numbers (int, float), strings (str), tuples (tuple)
To determine if a value can be changed, you need to know the object's type. For example, a list can be modified:
lst = [1, 2, 3]
lst[0] = 10 # change the first element
But a string cannot be changed directly:
s = "hello"
# s[0] = 'H' # will cause an error
Thus, mutability depends on the data type, not on the variable declaration.