Python: Comparison Operators

It is also known as Relational operators. It is used to compare values. It returns either True or False according to the condition.

OperatorsDescriptionSyntax
Greater than(>)If left operand is greater than the right it returns True.a > b
Less than(<)If left operand is less than right it returns True.a < b
Equal (==)If left and right operands are equal it returns True.a == b
Not Equal(!=)If operands are not equal it returns True.a != b
Greater than or equal to(>=)If left operand is greater than or equal to the right it returns True.a >= b
Less than or equal to(<=)If left operand is less than or equal to the right it returns True.a <= b

Example:

a=3
b=5

if (a == b):
    print ("a is equal to b")
else:
    print ("a is not equal to b")

if (a != b):
    print ("a is not equal to b")
else:
    print ("a is equal to b")

if (a < b):
    print ("a is less than b" )
else:
    print ("a is not less than b")

if (a > b):
    print ("a is greater than b")
else:
    print ("a is not greater than b")

if (a <= b):
    print ("a is either less than or equal to  b")
else:
    print("a is neither less than nor equal to  b")

if (b >= a):
    print ("b is either greater than  or equal to b")
else:
    print ("b is neither greater than  nor equal to b")

Output:

a is not equal to b
a is not equal to b
a is less than b
a is not greater than b
a is either less than or equal to  b
b is either greater than  or equal to b
Tags