How is the property of lists(Case 1
) called that lets the output of print y
be different from Case 2
?
# Case 1: using a list as value
>>> x = ["one", "two", "three"]
>>> y = x
>>> x[0] = "four"
>>> print x
["four", "two", "three"]
>>> print y
["four", "two", "three"]
# Case 2: using an integer as value
>>> x = 3
>>> y = x
>>> x = x + 1
>>> print x
4
>>> print y
3
Edit:
To show that this behavior has nothing to do with lists being mutable and strings not, instead of Case 2, the following case could have been given instead:
>>> x = ["one", "two", "three"]
>>> y = x
>>> x = x + ["four", "five"]
>>> print x
["four", "two", "three", "four", "five"]
>>> print y
["four", "two", "three"]