Python is not Operator check if the two operands refer to the same memory reference. It returns true If they do not have the same memory reference else it returns False.
a == b
and a != b
test whether two objects have the same value. You can override an object’s __eq__
and __ne__
methods to determine what that means.
a is b
and a is not b
test whether two objects are the same thing. It’s like doing id(a) == id(b)
Python is not Operator
Simple example code use is not an operator in the Python If statement.
a = [5, 8]
b = [5, 8]
c = a
if a is not b:
print('a is not b')
else:
print('a is b')
if a is not c:
print('a is not c')
else:
print('a is c')
Output:
The difference between != and is not an operator in Python
Answer: In Python != is defined as not equal to the operator. It returns True if operands on either side are not equal to each other, and returns False if they are equal. Python does not check if the two operands refer to the same memory reference.
a = 10
b = 10
c = 9
print(a is not b)
print(a != b)
print(a != c)
Output:
False
False
True
Do comment if you have any doubts or suggestions on this Python operator topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.