Python Not Equal Operator Tutorial

Comparison operators are used to compare different objects, variables, and values. The not equal operator is used to check if the specified objects, variables, or values are not equal. If they are not equal the boolean True value is returned. If they are equal the boolean False value is returned. The not equal operator is expressed with the != signs.

Not Equal Operator Syntax

The equal sign has the following syntax where objects, variables, or values are provided on the left and right sides of the not equal operator.

OBJECT1 != OBJECT2
  • OBJECT1 can be an object, variable, or value that will be checked against OBJECT2.
  • OBJECT2 can be an object, variable, or value that will be checked against OBJECT1.

Check If Variables Are Not Equal

Variables can be compared with the not equal operator. In the following example, we use variables to compare each other and with the provided values.

a = "ismail"

b = "ismail"

c = "ahmet"

d = 3 

e = 4

result = a != b
#result is False


result = a != c
#result is True


if a!=b:
   print("a and b are not equal")
#Output is "a and b are not equal"


result = a != "ismail"
#result is False


result = d != e
#result is True

Check If Values Are Not Equal

The not equal operator can be also used with the values. Even if this is not so popular it can be useful in some rare situations.

result = "ismail" != "ismail"
#result is False

result = "ismail" != "ali"
#result is True

Check If Strings Are Not Equal

Strings can be checked if they are equal or not. Strings can be string values or string variables. In the following example, we compare different strings and store them in the variable named result .

a = "ismail"

b = "ahmet"

result = a != b
#result is True


result = a != "ismail"
#result is False

Check If Numbers Are Not Equal

Numbers like variables and values can be used to compare if they are not equal.

a = 1

b = 2


result = a != b
#result True


result = a != 1
#result False

Leave a Comment