Relational operators, also known as comparison operators, are fundamental in Python for comparing two values. They return a Boolean result (True or False), making them essential for decision-making in programming. These operators are frequently used in control structures like if
, while
, and for
loops, allowing programs to execute specific logic based on conditions.
In this guide, we will explore relational operators, their applications, precedence, associativity, and real-world examples to help you master their usage.
Relational operators compare two values and return a Boolean outcome: True
if the condition holds, otherwise False
. These operators work consistently across all Python versions.
Operator | Description | Example | Output |
---|---|---|---|
== | Checks if two values are equal | 10 == 20 | False |
!= | Checks if two values are not equal | 10 != 20 | True |
> | Checks if the left operand is greater than the right operand | 10 > 5 | True |
< | Checks if the left operand is less than the right operand | 10 < 5 | False |
>= | Checks if the left operand is greater than or equal to the right operand | 10 >= 10 | True |
<= | Checks if the left operand is less than or equal to the right operand | 5 <= 10 | True |
Relational operators are often combined with logical operators (and
, or
, not
) to form complex conditional expressions, which are crucial for control flow in Python.
# Define variables
a, b = 10, 20
# Using relational operators
print(a == b) # Output: False
print(a != b) # Output: True
print(a > b) # Output: False
print(a < b) # Output: True
print(a >= b) # Output: False
print(a <= b) # Output: True
All relational operators have the same precedence level. When multiple relational operators appear in an expression, Python evaluates them from left to right.
a, b = 10, 20
result = a == b or a < b and a != b
print(result) # Output: True
a == b
→ False
a < b
→ True
a != b
→ True
False or (True and True)
→ True
age = 18
if age >= 18:
print("Eligible to vote.")
else:
print("Not eligible to vote.")
Output:
Eligible to vote.
count = 0
while count < 5:
print(count)
count += 1
Output:
0
1
2
3
4
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = [num for num in numbers if num % 2 == 0]
print(even_numbers)
Output: [2, 4, 6]
While relational operators compare two values, logical operators (and
, or
, not
) combine multiple conditions.
x, y = 5, 10
if x < y and y > 0:
print("Both conditions are True.")
Output:
Both conditions are True.
Relational operators compare two values and return True
or False
based on the comparison.
Python has six relational operators: ==
, !=
, <
, >
, <=
, >=
.
Yes, Python compares strings lexicographically (dictionary order).
print("apple" < "banana") # Output: True
Relational operators follow left-to-right associativity.
Relational operators in Python are fundamental for comparison-based decision-making. Mastering operators like ==
, !=
, >
, <
, >=
, and <=
allows you to write efficient, readable, and logical programs. By combining these operators with conditional statements, loops, and logical operators, you can develop interactive and dynamic Python applications.