Explanation:
Tuple t Creation:
- t is a tuple with three elements:
- 1 → an integer
- [2, 3] → a mutable list
- 4 → another integer
- So, t looks like this:
- t = (1, [2, 3], 4)
Tuple Immutability:
- In Python, tuples are immutable. You cannot change the tuple itself (e.g., reassign or delete elements directly).
- However, tuples can hold mutable objects like lists. If a tuple contains a list, you can modify the list.
Modifying the List:
- t[1] refers to the list [2, 3] (the second element of the tuple).
- t[1][0] = 100 changes the first element of the list [2, 3] to 100.
- After this operation, the list becomes [100, 3].
Resulting Tuple:
- The tuple t remains intact (as a container), but the list inside it has been modified.
- The final tuple now looks like:
Output:
(1, [100, 3], 4)Key Takeaways:
- Tuples are immutable, but they can hold mutable objects like lists.
- You can modify the contents of mutable objects inside a tuple.
- Direct reassignment like t[1] = [100, 3] would raise an error because it tries to modify the tuple structure.
0 Comments:
Post a Comment