Is There a "Not Equal" Operator in Python

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)

Python not equal operator

Python 2 supports both, in python 3 the <> operator has been removed.

There is no difference between the two, but != is the preferred form.

not equals operator(!=) not working in python string comparison

Your condition:

if location.lower() != 'united states of america' or location.lower() != 'usa':

will never be False, since location.lower() can't be 2 different strings at the same time.

I suspect you want:

if location.lower() != 'united states of america' and location.lower() != 'usa':

Why does Python's != operator think that arguments are equal and not equal at the same time?

Python’s “Data model” explains it all:

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 C(1) != C(2), it’s using the default implementation, where objects are equal only to themselves and unequal to everything else.

Defining __cmp__ can be simpler, as it is used as a fallback for all comparison operations, not just some of them:

...   def __cmp__(self, o):
... return 0
>>> C(1) != C(2)
False

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.

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.

Why a != b != c is not equal to a != b and a != c and b != c?

When you use a != b != c you are actually using chained comparison, from the documentation:

Formally, if a, b, c, …, y, z are expressions and op1, op2, …, opN are
comparison operators, then a op1 b op2 c ... y opN z is equivalent to
a op1 b and b op2 c and ... y opN z, except that each expression is
evaluated at most once.

So a != b != c is actually a != b and b != c, which is different from a != b and a != c and b != c, for example:

a, b, c = 1, 2, 1
print(a != b != c)
print(a != b and a != c and b != c)

Output

True
False

Python Modulo Operator without An Equal Sign and A Not-Equal Sign

The expression number % 2, where number is even, will give you zero. That's a falsey value in Python, hence the if will fail and it will output Even. It's no different to:

>>> if 0:
... print('zero')
... else:
... print('nonzero')
...
nonzero

If you wanted the if statement to succeed for an even number, you would be better off using number % 2 == 0, which will give you True for even numbers. That equivalent statement would be written as:

return 'Even' if number % 2 == 0 else 'Odd'

However, given your constraints in the title (no == or !=), that's not allowed(a). The use of "reversed" logic gets around this problem.


By the way, other methods to do this could include (amongst probably a great many more):

return 'Even' if not number % 2 else 'Odd'
return ['Even', 'Odd'][number % 2]

(a) Though I rather dislike artificial limitations of this type educators often slip into assignments. After all, when will a developer really be called upon to implement something without the = key? Maybe if their keyboard is broken? But then, your bigger issue is surely working for a company that won't shell out $5 for a new keyboard :-)



Related Topics



Leave a reply



Submit