Python Relational Operators
flowchart TD A["1 < 2 < 3"] --> B["becomes (1 < 2) and (2 < 3)"] B --> C["True"] D["in most other languages"] --> E["(1 < 2) < 3 -> True < 3 -> 1 < 3"] E --> F["a different question entirely"] G["f(1) < f(2) < f(0)"] --> H["f(2) is evaluated ONCE, not twice"] I["1 < 2 > 0"] --> J["legal, and True -- but is it a range check?"] J --> K["mixed directions compile fine; read them twice"]
Navigating Relationships: Relational Operators in Python
Section titled “Navigating Relationships: Relational Operators in Python”Relational operators in Python play a pivotal role in evaluating and expressing relationships between values. They allow you to compare variables, check conditions, and make decisions based on the outcomes of these comparisons. In this comprehensive guide, we’ll explore the realm of relational operators, their syntax, and their applications in Python programming.
The following table lists the relational operators in Python:
| Operator | Description | Example |
|---|---|---|
== | If the values of two operands are equal, then the condition becomes true | x == y |
!= | If values of two operands are not equal, then the condition becomes true | x != y |
> | If the value of the left operand is greater than the value of the right operand, then the condition becomes true | x > y |
< | If the value of the left operand is less than the value of the right operand, then the condition becomes true | x < y |
>= | If the value of the left operand is greater than or equal to the value of the right operand, then the condition becomes true | x >= y |
<= | If the value of the left operand is less than or equal to the value of the right operand, then the condition becomes true | x <= y |
Equality
Section titled “Equality”== (Equality) Operator
Section titled “== (Equality) Operator”The equality operator (==) compares the values of two operands. If the values of both operands are equal, then the condition becomes True. Otherwise, the condition becomes False. The following example demonstrates how to use the equality operator in Python:
# Equality operator
x = 10
y = 5
z = x == y
t = x == 10
print(z)
print(t)Output:
C:\Users\Your Name> python operators.py
False
TrueIn the above example, we have used the equality operator to compare the values of two operands x and y. Since the value of x is not equal to the value of y, the condition becomes False. The result of the equality operator is then assigned to the variable z. The value of z is then printed to the console.
Inequality
Section titled “Inequality”!= (Inequality) Operator
Section titled “!= (Inequality) Operator”The inequality operator (!=) compares the values of two operands. If the values of both operands are not equal, then the condition becomes True. Otherwise, the condition becomes False. The following example demonstrates how to use the inequality operator in Python:
# Inequality operator
x = 10
y = 5
z = x != y
t = x != 10
print(z)
print(t)Output:
C:\Users\Your Name> python operators.py
True
FalseIn the above example, we have used the inequality operator to compare the values of two operands x and y. Since the value of x is not equal to the value of y, the condition becomes True. The result of the inequality operator is then assigned to the variable z. The value of z is then printed to the console.
Greater Than
Section titled “Greater Than”> (Greater Than) Operator
Section titled “> (Greater Than) Operator”The greater than operator (>) compares the values of two operands. If the value of the left operand is greater than the value of the right operand, then the condition becomes True. Otherwise, the condition becomes False. The following example demonstrates how to use the greater than operator in Python:
# Greater than operator
x = 10
y = 5
z = x > y
t = x > 10
print(z)
print(t)Output:
C:\Users\Your Name> python operators.py
True
FalseIn the above example, we have used the greater than operator to compare the values of two operands x and y. Since the value of x is greater than the value of y, the condition becomes True. The result of the greater than operator is then assigned to the variable z. The value of z is then printed to the console.
Less Than
Section titled “Less Than”< (Less Than) Operator
Section titled “< (Less Than) Operator”The less than operator (<) compares the values of two operands. If the value of the left operand is less than the value of the right operand, then the condition becomes True. Otherwise, the condition becomes False. The following example demonstrates how to use the less than operator in Python:
# Less than operator
x = 10
y = 5
z = x < y
t = x < 10
print(z)
print(t)Output:
C:\Users\Your Name> python operators.py
False
FalseIn the above example, we have used the less than operator to compare the values of two operands x and y. Since the value of x is not less than the value of y, the condition becomes False. The result of the less than operator is then assigned to the variable z. The value of z is then printed to the console.
Greater Than or Equal To
Section titled “Greater Than or Equal To”>= (Greater Than or Equal To) Operator
Section titled “>= (Greater Than or Equal To) Operator”The greater than or equal to operator (>=) compares the values of two operands. If the value of the left operand is greater than or equal to the value of the right operand, then the condition becomes True. Otherwise, the condition becomes False. The following example demonstrates how to use the greater than or equal to operator in Python:
# Greater than or equal to operator
x = 10
y = 5
z = x >= y
t = x >= 10
print(z)
print(t)Output:
C:\Users\Your Name> python operators.py
True
TrueIn the above example, we have used the greater than or equal to operator to compare the values of two operands x and y. Since the value of x is greater than the value of y, the condition becomes True. The result of the greater than or equal to operator is then assigned to the variable z. The value of z is then printed to the console.
Less Than or Equal To
Section titled “Less Than or Equal To”<= (Less Than or Equal To) Operator
Section titled “<= (Less Than or Equal To) Operator”The less than or equal to operator (<=) compares the values of two operands. If the value of the left operand is less than or equal to the value of the right operand, then the condition becomes True. Otherwise, the condition becomes False. The following example demonstrates how to use the less than or equal to operator in Python:
# Less than or equal to operator
x = 10
y = 5
z = x <= y
t = x <= 10
print(z)
print(t)Output:
C:\Users\Your Name> python operators.py
False
TrueIn the above example, we have used the less than or equal to operator to compare the values of two operands x and y. Since the value of x is not less than the value of y, the condition becomes False. The result of the less than or equal to operator is then assigned to the variable z. The value of z is then printed to the console.
Combining Relational Operators
Section titled “Combining Relational Operators”You can combine relational operators in Python to create complex conditions. For example, you can combine the equality operator (==) and the greater than operator (>) to create a condition that checks if the value of x is greater than y and equal to z. The following example demonstrates how to combine relational operators in Python:
# Combining relational operators
x = 10
y = 5
z = 10
t = x > y and x == z
print(t)Output:
C:\Users\Your Name> python operators.py
TrueIn the above example, we have combined the equality operator (==) and the greater than operator (>) to create a condition that checks if the value of x is greater than y and equal to z. Since the value of x is greater than y and equal to z, the condition becomes True. The result of the condition is then assigned to the variable t. The value of t is then printed to the console.
Relational Operators with Strings
Section titled “Relational Operators with Strings”You can also use relational operators with strings in Python. For example, you can use the equality operator (==) to check if two strings are equal. The following example demonstrates how to use relational operators with strings in Python:
# Relational operators with strings
x = "Hello"
y = "World"
z = x == y
t = x == "Hello"
print(z)
print(t)Output:
C:\Users\Your Name> python operators.py
False
TrueIn the above example, we have used the equality operator to check if the value of x is equal to the value of y. Since the value of x is not equal to the value of y, the condition becomes False. The result of the condition is then assigned to the variable z. The value of z is then printed to the console.
Conclusion
Section titled “Conclusion”Relational operators are fundamental tools for expressing conditions and making decisions in Python. Whether you’re comparing numerical values, checking string order, or combining conditions with logical operators, relational operators provide the means to navigate relationships between variables.
As you continue your Python journey, experiment with different relational operators, explore their applications in real-world scenarios, and use them to create dynamic and responsive code. For more insights and practical examples, check out our tutorials on Python Central Hub!
Check yourself
Section titled “Check yourself”-
In Python, what does `1 < 2 < 3` mean?
Python chains the comparison into an AND of neighbouring pairs. Most other languages evaluate left to right, which compares a boolean with the third value instead.
pch.quizShowAnswer
B — `(1 < 2) and (2 < 3)` — Python chains the comparison into an AND of neighbouring pairs. Most other languages evaluate left to right, which compares a boolean with the third value instead.
-
`f(1) < f(2) < f(0)`. How many times is `f(2)` called?
Verified: the call log was `[1, 2, 0]`. The middle term appears twice in the expansion but is evaluated once, which matters when it is expensive or has a side effect.
pch.quizShowAnswer
B — Once — Verified: the call log was `[1, 2, 0]`. The middle term appears twice in the expansion but is evaluated once, which matters when it is expensive or has a side effect.
-
What is `False == 0` in Python?
`bool` is a subclass of `int`, so `True == 1` and `False == 0`. That is also why `1 in [True]` is True and `{1: 'a', True: 'b'}` is a one-key dictionary.
pch.quizShowAnswer
B — `True` — `bool` is a subclass of `int`, so `True == 1` and `False == 0`. That is also why `1 in [True]` is True and `{1: 'a', True: 'b'}` is a one-key dictionary.
-
Is `1 < 2 > 0` valid Python?
It is legal and evaluates to True. It reads like a range check and is not one, which is exactly why mixed-direction chains deserve a second look.
pch.quizShowAnswer
B — Yes, and it means `(1 < 2) and (2 > 0)` — It is legal and evaluates to True. It reads like a range check and is not one, which is exactly why mixed-direction chains deserve a second look.
Try it: Relational Operators Exercises
Section titled “Try it: Relational Operators Exercises”Exercise 1 – Basic Comparisons
Section titled “Exercise 1 – Basic Comparisons”Exercise 2 – String Comparison
Section titled “Exercise 2 – String Comparison”Exercise 3 – Chained Comparisons
Section titled “Exercise 3 – Chained Comparisons”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading