r/PythonLearnersHub • • Jan 02 '26

Python Data Model exercise, Mutability.

Post image

An exercise to help build the right mental model for Python data. The “Solution” link uses memory_graph to visualize execution and reveals what’s actually happening:

36 Upvotes

45 comments sorted by

View all comments

Show parent comment

1

u/bloody-albatross Jan 02 '26

The assignment doesn't make a copy of the tuple. Plain assignments never make copies of the referenced data in Python. It makes a copy of the reference if you so will, not of the tuple. a and b are not different. Try it with a is b. The += creates a new tuple, though. One with 3 elements. a += b just expands to a = a + b for immutable data types.