Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python 'is not' operator

Tags:

python

I notice there is a comparison operator is not. Should I literally translate it into

!=  

instead of

== not 
like image 725
nos Avatar asked Dec 19 '10 21:12

nos


People also ask

Is and is not which operator?

is and is not are the identity operators in Python. They are used to check if two values (or variables) are located on the same part of the memory. Two variables that are equal does not imply that they are identical.

Is not condition in Python?

The 'not' is a Logical operator in Python that will return True if the expression is False. The 'not' operator is used in the if statements. If x is True, then not will evaluate as false, otherwise, True.


1 Answers

To expand on what Ignacio said:

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)

like image 156
Thomas K Avatar answered Sep 19 '22 17:09

Thomas K