Hi all I know what this code does:
1.] My first problem
x = 4
y = x
But what about this. Why same addresses even for this case?
x = 4
y = 4
id(x)
12345678
id(y)
12345678
2.] My second problem
x = 42
y = x
x = x + 1
print x # Prints 43
print y # Prints 42
x = [1, 2, 3]
y = x
x[0] = 4
print x # Prints [4, 2, 3]
print y # Prints [4, 2, 3]
But why is it that in the case of list, both x
& y
got mutated together by the command x[0] = 4
.
What is so different with lists, in such a behavior?
What makes them behave like this?
And most importantly what is the benefit of such a behavior?
why cant list, variables, tuples have all the properties of each other?