Is there a "not equal" operator in Python?
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...
Aj Entity
Criminology student, playing around with computers. Lol.
Updated on December 29, 2020Comments
-
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 almost 9 yearsHow would you compare two binary data?
-
S.A. about 6 yearsWhat value would you assign to the variables
hi
andbye
? 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 almost 5 yearsJust some info, PEP401 mentioned in the comments was an April Fool joke.
<>
is not supported in Python3 now. -
Ocaso Protal almost 5 yearsJust for the record: Comparison operators in Python 3.7
-
Philzen almost 4 yearsNote that
is not
won't work for string comparison -
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 almost 3 yearsThis answer is simply wrong;
is
andis not
do not test equality, they test identity. -
Amir Md Amiruzzaman almost 3 years@kaya3, please help me to understand why "is not" will not work for my example.
-
kaya3 almost 3 yearsI suggest you read this other Q&A: stackoverflow.com/questions/13650293/…
-
Amir Md Amiruzzaman almost 3 yearsThank you @kaya3
-
Thierry Lathuille over 2 years
<>
no longer exists in Python 3, you can only use!=
. -
ljden about 2 years@S.A. I'm confused, Ehsan posted this nearly a year before user128364?
-
S.A. about 2 yearsMhh 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.