I'm just beginning to learn and read about Python and have question that I'm having trouble understanding while reading the first few chapters of the book. I came across this while playing around with the interpreter.
Here is my question, how come the values differ in both of these expressions. In the first example, the value of y
remains the same after changing x
, while in the next example when changing x
, it also changes the value of y
.
Example 1:
>>> x = 5
>>> y = x
>>> x += 1
>>> x
6
>>> y
5
Example: 2
>>> x = [5]
>>> y = x
>>> x[0] = 6
>>> x
[6]
>>> y
[6]