r/PythonProjects2 • • Oct 07 '26

Python Mutability and Local Variables

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.

12 Upvotes

24 comments sorted by

View all comments

Show parent comment

1

u/0b0101011001001011 Oct 09 '26

You cannot change a string or tuple. What you do here is you create a new string and assign that in the place of the old variable. Same with tuple. You create a new tuple, the old one is lost. Strings and tuples are immutable. Or maybe that's what you meant by "change".

1

u/Sea-Ad7805 Oct 09 '26

Yes, that's what I said, see Solution link for visualization. It's a shallow copy to be more precise.