Relational Operators In Python
Relational operators are widely used in almost every Python program because real-world applications constantly need to compare values in order to make decisions. Whether we are validating user input, checking conditions inside loops, filtering records, sorting values, or implementing authentication logic, comparisons play an essential role everywhere.
Relational operators are also commonly referred to as comparison operators because their entire purpose is to compare two values and determine the relationship between them. Instead of returning another numerical value like arithmetic operators, relational operators return Boolean results, meaning the final answer will always be either True or False.
Types of Relational Operators in Python
Python provides several relational operators that help compare two values in different ways.
The commonly used relational operators are:
| Operator | Meaning |
|---|---|
| < | Less than |
| <= | Less than or equal to |
| > | Greater than |
| >= | Greater than or equal to |
| == | Equal to |
| != | Not equal to |
Comparing Numerical Values Using Relational Operators
The simplest and most common use of relational operators is comparing numbers. When numbers are compared, Python evaluates the relationship between the two operands and returns either True or False depending on whether the condition is satisfied.
Consider the following example:
a = 10
b = 20
print(a < b) # True
print(a <= b) # True
print(a > b) # False
print(a >= b) False
Relational Operators with Strings
Many beginners assume that relational operators work only with numbers, but Python also allows relational comparisons between strings. This is one of the most interesting and important behaviors to understand because string comparison internally depends on Unicode values.
When strings are compared, Python does not compare them based on word meaning or dictionary meaning. Instead, Python compares characters based on their Unicode values.
This concept is extremely important because without understanding Unicode-based comparison, string relational behavior may initially appear confusing.
Understanding Unicode Values
Every character internally has a numerical Unicode value associated with it. For example:
| Character | Unicode Value |
|---|---|
| a | 97 |
| b | 98 |
| A | 65 |
| B | 66 |
This means characters are internally represented as numbers, and comparisons happen using these numerical representations.
The ord() Function
Python provides a built-in function called ord() that helps us find the Unicode value of a given character.
Example:
print(ord('a'))
print(ord('A'))
Output:
97
65
Here:
Small a has Unicode value 97
Capital A has Unicode value 65
This clearly shows that uppercase and lowercase letters have different Unicode representations.
The chr() Function
Python also provides another useful built-in function called chr() which performs the reverse operation.
If we know the Unicode value and want to find the corresponding character, we can use chr().
Example:
print(chr(97))
print(chr(68))
Output:
a
D
So:
- ord() converts character → Unicode value
- chr() converts Unicode value → character
These functions are extremely useful while understanding string comparisons.
Comparing Two Different Strings
Now let us understand how relational operators behave with strings.
Consider:
s1 = "Durga"
s2 = "Sunny"
print(s1 < s2) # True
print(s1 <= s2) # True
print(s1 > s2) # False
print(s1 >= s2) # False
Internal Rule of String Comparison
The internal rule Python follows while comparing strings can be summarized like this:
- Compare characters from left to right
- Compare Unicode values of corresponding characters
- Stop comparison immediately after the first unequal character
- Decide the result using that character comparison
This mechanism makes string comparison extremely efficient because Python usually does not need to compare the entire string.
Relational Operators with Boolean Values
One of the most interesting features of Python is that relational operators can also be applied to Boolean values.
This behavior initially surprises many students because they usually think Boolean values only represent True and False. However, internally Python treats:
True = 1
False = 0
This means Boolean comparisons internally become integer comparisons.
print(True > False) # True
print(True >= False) # True
print(True < False) # False
print(True <= False) # False
Error Example
print(10 < "Durga")
Output:
TypeError: '<' not supported between instances of 'int' and 'str'
This error message clearly tells us that comparison is not supported between incompatible types.
Chaining of Relational Operators
Python also supports an extremely useful feature called chaining of relational operators.
This means multiple relational comparisons can be written together in a single expression.
For example:
print(10 < 20 < 30) # True
This may initially look unusual, but internally Python interprets it as:
10 < 20 and 20 < 30
So multiple comparisons are evaluated together.
Important Observations About Relational Operators
- Relational operators always return boolean Values
- String comparison depends on Unicode values
- Boolean values behave like integers
- Incompatible types raise errors