x, y : input objects
Return boolean value True or False. Returns True if both side operands are of same identity, otherwise False
x is not y
x, y : input objects
Return boolean value True or False. Returns True if both side operands are NOT of same identity, otherwise False
a=30
b=30
print(a is b ) # True
a=300
b=300
print(a is b ) # False
Why the second print() returns False ? Identity of the object changed in second case ( Why ?) . (For integer -5 to 256, id will not change.).
By using is we are checking the identity of the object Not the Value so we got False in second case ( Identity remain same for integers between -5 and 256 )
Difference between id and value
Our is operator checks the identity only , not the value of the object.
Example 1
a=-6
b=-6
print("Using is :",a is b)
print("using == :",a==b)
Output
Using is : False
using == : True
Example 2
a=30
b=30
print(a is b ) # True
a=300
b=300
print(a is b ) # False
print(a == b ) # True
Our is operator checks the identity of the variables, == operator checks the value.