Asked By – aniskhan001
isoperator does not match the values of the variables, but the
What does it really mean?
I declared two variables named
y assigning the same values in both variables, but it returns false when I use the
I need a clarification. Here is my code.
x = [1, 2, 3] y = [1, 2, 3] print(x is y) # It prints false!
Now we will see solution for issue: Understanding the “is” operator [duplicate]
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
Remember that in Python, names are just labels referencing values; you can have multiple names point to the same object.
is tells you if two names point to one and the same object.
== tells you if two names refer to objects that have the same value.