The
isoperator does not match the values of the variables, but the
instances themselves.
What does it really mean?
I declared two variables named x and y assigning the same values in both variables, but it returns false when I use the is operator.
I need a clarification. Here is my code:
x = [1, 2, 3]
y = [1, 2, 3]
print(x is y) # False
You misunderstood what the
isoperator tests. It tests if two variables point the same object, not if two variables have the same value.From the documentation for the
isoperator:Use the
==operator instead:This prints
True.xandyare two separate lists:If you use the
id()function you’ll see thatxandyhave different identifiers:but if you were to assign
ytoxthen both point to the same object:and
isshows both are the same object, it returnsTrue.Remember that in Python, names are just labels referencing values; you can have multiple names point to the same object.
istells you if two names point to one and the same object.==tells you if two names refer to objects that have the same value.