Python != Operation VS "Is Not"

Python != operation vs is not

== is an equality test. It checks whether the right hand side and the left hand side are equal objects (according to their __eq__ or __cmp__ methods.)

is is an identity test. It checks whether the right hand side and the left hand side are the very same object. No methodcalls are done, objects can't influence the is operation.

You use is (and is not) for singletons, like None, where you don't care about objects that might want to pretend to be None or where you want to protect against objects breaking when being compared against None.

Operator != vs

From the documentation -

The forms <> and != are equivalent; for consistency with C, != is preferred; where != is mentioned below <> is also accepted. The <> spelling is considered obsolescent.

(Emphasis mine)

They are equivalent , but you should use != . <> operator is not there in Python 3.x as well.

Is there a not equal operator in Python?

Use !=. See comparison operators. For comparing object identities, you can use the keyword is and its negation is not.

e.g.

1 == 1 #  -> True
1 != 1 # -> False
[] is [] #-> False (distinct objects)
a = b = []; a is b # -> True (same object)

Is there an operation for not less than or not greater than in python?

Instead of a == 0 or a > 0 you could just use a >= 0.

https://docs.python.org/library/stdtypes.html#comparisons

Python Difference between not and !=

The first uses the object’s __ne__ method, while the second uses the object’s __eq__ method and negates its result.

While both methods should pair their result so a == b implies not a != b this is actually not required or enforced.

There are no implied relationships among the comparison operators. The truth of x==y does not imply that x!=y is false. Accordingly, when defining __eq__(), one should also define __ne__() so that the operators will behave as expected.

In your case, where choice is a string (I guess?) it does not make a real difference. The built-in types all ensure the inverse relationship between == and !=. So the only difference is really how understandable it is when looking at the code. And I personally prefer the more concise first version.

Is there a difference between != and operators in Python?

The Python documentation says that they are equivalent.

The comparison operators <> and != are alternate spellings of the same operator. != is the preferred spelling; <> is obsolescent.

The <> operator has been removed from Python 3.

python 'is not' operator

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)

When is the `==` operator not equivalent to the `is` operator? (Python)

In Python, the == operator is implemented in terms of the magic method __eq__, which by default implements it by identity comparison. You can, however, override the method in order to provide your own concept of object equality. Note, that if you do so, you will usually also override at least __ne__ (which implements the != operator) and __hash__, which computes a hash code for the instance.

I found it very helpful, even in Python, to make my __eq__ implementations comply with the rules set out in the Java language for implementations of the equals method, namely:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

the last one should probably replace null with None, but the rules are not as easy here in Python as in Java.

Is there a difference between == and is?

is will return True if two variables point to the same object (in memory), == if the objects referred to by the variables are equal.

>>> a = [1, 2, 3]
>>> b = a
>>> b is a
True
>>> b == a
True

# Make a new copy of list `a` via the slice operator,
# and assign it to variable `b`
>>> b = a[:]
>>> b is a
False
>>> b == a
True

In your case, the second test only works because Python caches small integer objects, which is an implementation detail. For larger integers, this does not work:

>>> 1000 is 10**3
False
>>> 1000 == 10**3
True

The same holds true for string literals:

>>> "a" is "a"
True
>>> "aa" is "a" * 2
True
>>> x = "a"
>>> "aa" is x * 2
False
>>> "aa" is intern(x*2)
True

Please see this question as well.



Related Topics



Leave a reply



Submit