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:
Operator | Name | Description | Syntax |
---|---|---|---|
== | Equal To | Equal to operator returns True if the first operand is equal to the second operand. Else, it returns False. | op1 == op2 |
!= | Not Equal To | Not Equal to operator returns True if the first operand is not equal to the second operand. Else, it returns False. | op1 != op2 |
> | Greater Than | The Greater Than operator returns True if the first operand is greater than the second operand. Else, it returns False. | op1 > op2 |
< | Less Than | The less Than operator returns True if the first operand is lesser than the second. Else, it returns False. | op1 < op2 |
>= | Greater Than or Equal To | Greater 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 To | The 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:
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.