How to use not equal operator in python

422    Asked by LisaHenderson in Python , Asked on Jul 26, 2021

How would you say does not equal sign python?

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

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

Answered by Hellen Cargill

You can use "!=" as does not equal sign 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")

Your Answer


Parent Categories