In Python, some objects are mutable, meaning you can change their content without changing their identity. Lists are a prime example of mutable objects. Understanding mutability is key to comprehending how Python handles complex data structures.
Mutable objects, like lists, allow in-place modifications, which means that the object retains its ID (identity) even if you alter its contents. In the given exercise:
- When you modify the content of
B using B[0] = 'shrubbery', you are changing the list in-place. - Since
A and B reference the same list, A now reflects this change and becomes ['shrubbery'].
This is different from immutable objects, like strings and tuples, where modifying the object actually creates a new instance with a new ID.