Is there a "not equal" operator in Python?

1,711,676

Solution 1

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)

Solution 2

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.

Solution 3

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.

Solution 4

You can use both != or <>.

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

Solution 5

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...

Share:
1,711,676
Aj Entity
Author by

Aj Entity

Criminology student, playing around with computers. Lol.

Updated on December 29, 2020

Comments

  • Aj Entity
    Aj Entity over 3 years

    How would you say does not equal?

    Like

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

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

  • Michal aka Miki
    Michal aka Miki almost 9 years
    How would you compare two binary data?
  • S.A.
    S.A. about 6 years
    What value would you assign to the variables hi and bye? Whatever it would be, the elif clause would never be reached. Lastly, this example does not clearly provide an answer to the question.
  • J...S
    J...S almost 5 years
    Just some info, PEP401 mentioned in the comments was an April Fool joke. <> is not supported in Python3 now.
  • Ocaso Protal
    Ocaso Protal almost 5 years
    Just for the record: Comparison operators in Python 3.7
  • Philzen
    Philzen almost 4 years
    Note that is not won't work for string comparison
  • Timo
    Timo over 3 years
    @LéoLéopoldHertz준영 Did not know that you can compare binary data. I thought that objects are the most you can compare.
  • kaya3
    kaya3 almost 3 years
    This answer is simply wrong; is and is not do not test equality, they test identity.
  • Amir Md Amiruzzaman
    Amir Md Amiruzzaman almost 3 years
    @kaya3, please help me to understand why "is not" will not work for my example.
  • kaya3
    kaya3 almost 3 years
    I suggest you read this other Q&A: stackoverflow.com/questions/13650293/…
  • Amir Md Amiruzzaman
    Amir Md Amiruzzaman almost 3 years
    Thank you @kaya3
  • Thierry Lathuille
    Thierry Lathuille over 2 years
    <> no longer exists in Python 3, you can only use !=.
  • ljden
    ljden about 2 years
    @S.A. I'm confused, Ehsan posted this nearly a year before user128364?
  • S.A.
    S.A. about 2 years
    Mhh looking at it now that seems right @Ijden. Maybe I misread a date back then (>4 years ago). I'll delete my comment. Thanks for making me aware and sorry for any trouble this might have caused.