Theoperator does not match the values of the variables, but the
x = [1, 2, 3]
y = [1, 2, 3]
print x is y #It prints false!
You misunderstood what the
is operator tests. It tests if two variables point the same object, not if two variables have the same value.
From the documentation for the
is nottest for object identity:
x is yis true if and only if
yare the same object.
== operator instead:
print x == y
y are two separate lists:
x = 4 print y # prints [1, 2, 3] print x == y # prints False
If you use the
id() function you'll see that
y have different identifiers:
>>> id(x) 4401064560 >>> id(y) 4401098192
but if you were to assign
x then both point to the same object:
>>> x = y >>> id(x) 4401064560 >>> id(y) 4401064560 >>> x is y True
is shows both are the same object, it returns