434

How would you say "does not equal"?

if hi == hi:
    print "hi"
elif hi (does not equal) bye:
    print "no hi"

Is there something similar to == that means "not equal"?

cottontail
  • 10,268
  • 18
  • 50
  • 51
Aj Entity
  • 4,741
  • 4
  • 17
  • 13
  • 6
    Are you asking about `else`, `!=` (optionally `<>`) or `is not`? – Tadeck Jun 16 '12 at 03:25
  • 21
    Attention that <> doesn't work any more in python 3, so use != – Antonello Mar 07 '16 at 10:38
  • 4
    from python documentation: `Python3 : The operators <, >, ==, >=, <=, and != compare the values of two objects.` https://docs.python.org/3/reference/expressions.html#value-comparisons – hamed Nov 07 '16 at 13:00
  • 1
    from python documentation: `python2: ` https://docs.python.org/2/reference/expressions.html#not-in – hamed Nov 07 '16 at 13:05

10 Answers10

684

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)
dimo414
  • 47,227
  • 18
  • 148
  • 244
tskuzzy
  • 35,812
  • 14
  • 73
  • 140
70

Not equal != (vs equal ==)

Are you asking about something like this?

answer = 'hi'

if answer == 'hi':     # equal
   print "hi"
elif answer != 'hi':   # not equal
   print "no hi"

This Python - Basic Operators chart might be helpful.

Levon
  • 138,105
  • 33
  • 200
  • 191
32

There's the != (not equal) operator that returns True when two values differ, though be careful with the types because "1" != 1. This will always return True and "1" == 1 will always return False, since the types differ. Python is dynamically, but strongly typed, and other statically typed languages would complain about comparing different types.

There's also the else clause:

# This will always print either "hi" or "no hi" unless something unforeseen happens.
if hi == "hi":     # The variable hi is being compared to the string "hi", strings are immutable in Python, so you could use the 'is' operator.
    print "hi"     # If indeed it is the string "hi" then print "hi"
else:              # hi and "hi" are not the same
    print "no hi"

The is operator is the object identity operator used to check if two objects in fact are the same:

a = [1, 2]
b = [1, 2]
print a == b # This will print True since they have the same values
print a is b # This will print False since they are different objects.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Samy Vilar
  • 10,800
  • 2
  • 39
  • 34
13

You can use both != or <>.

However, note that != is preferred where <> is deprecated.

Malek B.
  • 1,104
  • 1
  • 12
  • 14
  • 2
    `<>` no longer exists in Python 3, you can only use `!=`. – Thierry Lathuille Aug 05 '21 at 08:22
  • From [a comment](https://stackoverflow.com/questions/40211270/is-there-a-difference-between-and-operators-in-python#comment67686660_40211270): *"`<>` is [deprecated and removed in Python 3](http://stackoverflow.com/questions/11060506/is-there-a-not-equal-operator-in-python)."* – Peter Mortensen Nov 11 '22 at 18:07
7

Seeing as everyone else has already listed most of the other ways to say not equal I will just add:

if not (1) == (1): # This will eval true then false
    # (ie: 1 == 1 is true but the opposite(not) is false)
    print "the world is ending" # This will only run on a if true
elif (1+1) != (2): #second if
    print "the world is ending"
    # This will only run if the first if is false and the second if is true
else: # this will only run if the if both if's are false
    print "you are good for another day"

in this case it is simple switching the check of positive == (true) to negative and vise versa...

gabeio
  • 1,282
  • 1
  • 23
  • 37
0

There are two operators in Python for the "not equal" condition -

a.) != If values of the two operands are not equal, then the condition becomes true. (a != b) is true.

b.) <> If values of the two operands are not equal, then the condition becomes true. (a <> b) is true. This is similar to the != operator.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user128364
  • 4,533
  • 3
  • 20
  • 12
0

You can use "is not" for "not equal" or "!=". Please see the example below:

a = 2
if a == 2:
   print("true")
else:
   print("false")

The above code will print "true" as a = 2 assigned before the "if" condition. Now please see the code below for "not equal"

a = 2
if a is not 3:
   print("not equal")
else:
   print("equal")

The above code will print "not equal" as a = 2 as assigned earlier.

Amir Md Amiruzzaman
  • 1,911
  • 25
  • 24
0

You can use the != operator to check for inequality.

Moreover in Python 2 there was <> operator which used to do the same thing, but it has been deprecated in Python 3.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hadi Mir
  • 4,497
  • 2
  • 29
  • 31
  • From [a comment](https://stackoverflow.com/questions/40211270/is-there-a-difference-between-and-operators-in-python#comment67686660_40211270): *"`<>` is [deprecated and removed in Python 3](http://stackoverflow.com/questions/11060506/is-there-a-not-equal-operator-in-python)."* – Peter Mortensen Nov 11 '22 at 18:06
0

The standard operator module holds ne method which is a wrapper for != a.k.a. not equal operator.

import operator
operator.ne(1, 1)   # False
operator.ne(1, 3)   # True

This is especially useful if you need to make comparisons in a setting where a function is expected.

a = [1, 2, 3, 4]
b = [2, 2, 3, 3]
list(map(operator.ne, a, b))  # [True, False, False, True]
cottontail
  • 10,268
  • 18
  • 50
  • 51
-3

Use != or <>. Both stands for not equal.

The comparison operators <> and != are alternate spellings of the same operator. != is the preferred spelling; <> is obsolescent. (Reference: Python language reference)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ehsan
  • 1,338
  • 14
  • 13
  • You may *[link](https://stackoverflow.com/posts/42448590/edit)* to that reference, preferably with page anchors. But ************** ***without*** ************** "Edit:", "Update:", or similar - the answer should appear as if it was written today. – Peter Mortensen Nov 11 '22 at 16:39
  • From [a comment](https://stackoverflow.com/questions/40211270/is-there-a-difference-between-and-operators-in-python#comment67686660_40211270): *"`<>` is [deprecated and removed in Python 3](http://stackoverflow.com/questions/11060506/is-there-a-not-equal-operator-in-python)."* – Peter Mortensen Nov 11 '22 at 18:07