Skip to main content

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.

Operators Description Syntax
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
Submitted by devanshi.srivastava on June 25, 2021

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.