Relational operators in Python are also called as Comparison operators. They are used to compare the operands on either side and determine the relation between them. The output of the comparison results in a Boolean value.
Below is the list of relational operators in Python. Relational operators are the same in all versions of Python (Python 2.x, Python 3)
Operator | Name | Description |
== | Equal to | Checks whether two operands are equal |
!= | Not equal to | Checks whether two operands are not equal |
> | Greater than | Checks whether the left side operand is greater than the right side operand |
< | Less than | Checks whether the left side operand is less than the right side operand |
>= | Greater than or equal to | Checks whether the left side operand is either greater or equal to the right side operand |
<= | Lesser than or equal to | Checks whether the left side operand is either less than or equal to the right side operand |
As the name implies relational operators are used in identifying the relationship between two operands. These comparative operators are used along with the if, else and while statements.
An Example Program Using Relational Operators
Let us consider two variables a & b of values 10 and 20 to understand relational operations better.
a, b = 10, 20
print(a == b) # since the values 10 and 20 are different, the output of this print statement is False
print(a != b) # since both the values are not equal, the output of this print statement is True
print(a > b) # the value 10 is less than 20, hence the output of this print statement is False
print(a < b) # since the value 10 is less than 20, the output of this print statement is True
print(a >= b) # Since the value 10 is less than 20, the output of this print statement is False
print(a <= b) # Since the value 10 is less than 20, the output of this print statement is True
Output:
False
True
False
True
False
True
Precedence and Associativity of Relational Operators in Python
All the relational operators have the same precedence and hence it is important to consider the associativity of these operators. The associativity is left to right. This simply means the relational operator that appears first while reading an expression gets evaluated first.
Consider, the below program to understand associativity better.
a, b = 10, 20
print(a == b or a < b and a != b)
Output: True
Explanation: Here, the =
operator appears first. Hence the condition a == b
will be executed first. The result is False.
Even though the first condition is False
, the Interpreter evaluates the second condition because the first logical operator is or
. The next relational operator is <
. The condition is True
. The last relational operator is !=
. The condition is True. Hence, the final output is True
.
Relational Operators in Python FAQ
These operators are used to find the relation between two operands in the given program.
There are 6 relational operators in Python which are discussed in the above article with examples.
If an expression contains more than one relational operators, these they are evaluated from left to right.