How Python's '=' operator works: assignment vs. mutation explained
A tutorial published on DEV Community explains a common source of confusion for Python beginners: why assigning one variable to another can sometimes cause both to change. The key distinction is that '=' in Python does not copy an object — it binds a name to an existing object, meaning two variables can point to the same mutable structure like a list. When a mutable object is modified through one variable, the change is visible through all other variables referencing the same object, a behavior known as mutation. By contrast, reassigning a variable to a new value simply rebinds that name to a different object, leaving other references unaffected. The article also covers related pitfalls, including the difference between '=' and '==' and the fact that Python's input() function always returns a string, not a number.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in