Is there a "not equal" operator in Python?

Aj Entity picture Aj Entity · Jun 16, 2012 · Viewed 1.6M times · Source

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"?

Answer

tskuzzy picture tskuzzy · Jun 16, 2012

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)