Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a "not equal" operator in Python?

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

like image 215
Aj Entity Avatar asked Jun 16 '12 03:06

Aj Entity


People also ask

What is not equal operator in Python?

Python not equal operator returns True if two variables are of same type and have different values, if the values are same then it returns False . Python is dynamic and strongly typed language, so if the two variables have the same values but they are of different type, then not equal operator will return True .

What is == and != In Python?

Variables with the same value are often stored at separate memory addresses. This means that you should use == and != to compare their values and use the Python is and is not operators only when you want to check whether two variables point to the same memory address.

What is opposite of == in Python?

You can use the not equal Python operator for formatted strings (f-strings), introduced in Python 3.6. To return an opposite boolean value, use the equal operator ==. Keep in mind that some fonts change !=


2 Answers

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) 
like image 198
tskuzzy Avatar answered Sep 28 '22 06:09

tskuzzy


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.

like image 45
Levon Avatar answered Sep 28 '22 07:09

Levon