Python Relational Operators

Jeremiah Taguta (BSc in Computer Science, Masters in Information Systems)
Published on 19-05-2023
Unveiling Python's Relational Operators: Understanding the power of comparison using relational operators


Relational operators are basic programming language features that allow us to compare operands or values and make decisions based on such comparisons. Relational operators are essential in Python for assessing conditions and establishing the relationship between operands or values. 

This article provides an in-depth review of relational operators in Python, demonstrating their functionality, syntax, and applicability in different contexts. We shall cover equality, inequality, greater than, greater than or equal to, less than, and less than or equal to relational operators. Based on the comparison result, these operators return a Boolean value (True or False). The operators are useful for decision-making or selection as conditions of such, which we shall cover later. Here are the relational operators in Python along with explanations.

1 Equality Operator (==)

The equality operator, ==, checks if two operands or values are equal. It returns True if they are equal, otherwise, it returns False.

1 x = 5
2 y = 10
3 print(x == y)  # False
4 print(x == 5)  # True
5 print("Hello" == "Hello")  # True
6 print("Hello" == "hello")  # False
7 print(True == 1) # True
8 print(True == 6) # True
9 print(True == "True") # False

In line 3 above, x is not equal to y, so the result is False. In line 4, x is equal to 5, so the result is True. For strings, as in lines  5 and 6, the comparisons are case sensitive thus "Hello" is not equal to "hello". For line 7, True has an integer value of 1 while False is 0. True, is not equal to "True" as the first is a boolean and the second is a string as in line 9

2 Inequality Operator (!=)

The inequality operator, !=, checks if two operands or values are not equal. It returns True if the operands are not equal, otherwise, it returns False.

1 print(x != y)  # True
2 print(x != 5)  # False
3 print("Hello" != "hello")  # True

In line 1 above, x is not equal to y, so the result is True. In line 2, print statement, x is equal to 5, so the result is False. In line 3, the output is True as "Hello" and "hello" are not equal.

3 Greater Than Operator (>)

The greater than operator, >, checks if the left operand or value is greater than the right operand or value. It returns True if the left operand or value is greater; otherwise, it returns False.

1 print(x > y)  # False
2 print(y > x)  # True

In line 1 above, x is not greater than y, so the result is False while in line 2, y is greater than x, so the result is True.

4 Less Than Operator (<)

The less than operator, <, checks if the left operand or value is less than the right operand or value. It returns True if the left operand or value is less, otherwise, it returns False.

1 print(x < y)  # True
2 print(y < x)  # False

In line 1 above, x is less than y, so the result is True whereas in line 2, y is not less than x, so the result is False.

5 Greater Than or Equal To Operator (>=)

This operator,>=, checks if the left operand or value is greater than or equal to the right operand or value. It returns True if the left operand or value is greater than or equal, otherwise, it returns False.

1 print(x >= y)  # False
2 print(y >= x)  # True

In line above 1, x is neither greater than nor equal to y, so the result is False while in line 2, y is greater than or equal to x, so the result is True.

6 Less Than or Equal To Operator (<=)

The less than or equal to operator, <=, checks if the left operand or value is less than or equal to the right operand or value. It returns True if the left operand or value is less than or equal, otherwise, it returns False.

1 print(x <= y)  # True
2 print(y <= x)  # False

In line 1 above, x is neither greater than nor equal to y, so the result is False while in line 2, y is greater than or equal to x, so the result is True.

In this article, we discussed the use of equality, inequality, greater than, greater than or equal to, less than, and less than or equal to relational operators that return True or False. These operators are used when comparing values and determining whether one value is less than, greater than or equal to another. Besides many use cases, the equality and inequality operators are also used in string comparison whereas all apply to numbers.

Thanks for reading. If you have any questions, feel free to ask in the comments section, I'll gladly help. Check out my programming tutorials on YouTube and don't forget to subscribe, comment, like, and share on both YouTube and on this Blog. Lets code together!!!

207 Like

Drop your comment

© Copyright 2023 Jeremiah Taguta. All Rights Reserved.

Product of ‌

Jeremiah Taguta T/A Charis Technologies