Is there a "not equal" operator in Python?
!= (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
# 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"
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 valuesprint a is b # This will print False since they are different objects.