Skip to content

Relational operators in Python

  • by

Python Relational operators are used for comparing the operand values on either side. It returns a boolean value, i.e., either True or False based on the value of operands.

The following are the relational operators:

OperatorNameDescriptionSyntax
==Equal ToEqual to operator returns True if the first operand is equal to the second operand. Else, it returns False.op1 == op2
!=Not Equal ToNot Equal to operator returns True if the first operand is not equal to the second operand. Else, it returns False.op1 != op2
>Greater ThanThe Greater Than operator returns True if the first operand is greater than the second operand. Else, it returns False.op1 > op2
<Less ThanThe less Than operator returns True if the first operand is lesser than the second. Else, it returns False.op1 < op2
>=Greater Than or Equal ToGreater Than or Equal To operator returns True if the first operand is greater than or equal to the second operand. Else, it returns False.op1 >= op2
<=Lesser Than or Equal ToThe less Than or Equal To operator returns True if the first operand is less than or equal to the second operand. Else, it returns False.op1 <= op2

Relational operators in Python

Simple example code.

operand1 = 100
operand2 = 99

print(">", operand1 > operand2)
print("<", operand1 < operand2)
print("==", operand1 == operand2)
print("!=", operand1 != operand2)
print(">=", operand1 >= operand2)
print("<=", operand1 <= operand2)

Output:

Relational operators in Python

Here’s a simple example demonstrating the use of relational operators in Python:

x = 5
y = 10

print(x == y)   # Output: False
print(x != y)   # Output: True
print(x > y)    # Output: False
print(x < y)    # Output: True
print(x >= y)   # Output: False
print(x <= y)   # Output: True

Do comment if you have any doubts or suggestions on this Python operators topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading