How to use not equal operator in python

0 votes

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

Dec 21, 2018 in Python by aryya
• 7,450 points
359,610 views

7 answers to this question.

0 votes

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

e.g.

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

answered Dec 21, 2018 by charlie_brown
• 7,720 points
0 votes

You can use "!=" and "is not" for not equal operation in Python. The python != ( not equal operator ) return True, if the values of the two Python operands given on each side of the operator are not equal, otherwise false . Python is dynamically, but strongly typed , and other statically typed languages would complain about comparing different types . So if the two variables have the same values but they are of different type, then not equal operator will return True.

 str = 'halo'
if str == 'halo':     # equal
   print ("halo")
elif str != 'halo':   # not equal
   print ("no halo")

answered Mar 17, 2020 by rahul
• 360 points
0 votes