Chaining Comparison Operators in Python
In Python, you can chain various comparison operators like we do in mathematics without using parentheses. This means you can write (a > 10) and (a < 20 ) as 10 < a < 20, and both will give the same result. The advantage of this comparison is that Python evaluates each condition from left to right and will stop as soon as any part of the chain fails.
How it Works: The interpreter automatically applies a logical operator between the comparison operators when you chain them. This means that a < b < c is interpreted as (a < b) and (b < c).
Example:
Output:
Explanation: Here, the grade of the student was 82. According to the condition, the result was printed.
Recap
Operator | Name | Description | Example | Result |
== | Equal to | Checks if two values are equal | 5 == 5 | True |
!= | Not equal to | Checks if two values are not equal | 5 != 3 | True |
> | Greater than | True if the left operand is greater | 7 > 3 | True |
< | Less than | True if the left operand is smaller | 4 < 9 | True |
>= | Greater than or equal to | True if left is greater or equal | 10 >= 10 | True |
<= | Less than or equal to | True if the left is smaller or equal | 6 <= 6 | True |
Comparison Operators with Different Data Types in Python
Python allows you to compare other data types like strings, tuples, and lists as well, in addition to integers. This means you can compare a string with a string, a tuple with a tuple, and a list with a list. Comparing two incompatible data types is not supported. You cannot compare a string with a tuple, and so on.
Strings in Python
When you are using comparison operators with strings, Python compares each character of the string based on their ASCII Code. This is called the Lexicographical Comparison. Remember, uppercase letters come before the lowercase letters in the ASCII code.
Example:
Output:
Explanation: Here, the first characters M and D are compared. The ASCII value of M is 77, and D is 68. Since 77 > 68, “Mumbai” comes after “Delhi” alphabetically.
Tuples and Lists in Python
With sequential data types like tuples and lists, the interpreter compares each element one by one, from left to right. It moves until it finds one false comparison and returns False then and there.
Example:
Output:
Explanation: Here, in tuples, a is less than b because the third element in a is less than in b. In the last example, list 1 has only two elements, whereas list 2 has 3. Therefore, list1 is considered less than list2. The shorter lists are considered smaller if all earlier elements are equal.
Boolean in Python
The boolean values in Python act like integers. True value takes the integer value of 1, and the False value takes the integer value of 0. Hence, if we compare the boolean values with the less than or greater than operator, the answer will be accordingly.
Example:
Output:
Explanation: Here, True is considered 1, and False is considered 0. The comparison results reflect their integer values (1 for True, 0 for False).
Real-World Example of Comparison Operators in Python
Comparison operators are used nearly everywhere where there are conditional statements. The conditions get checked using these comparison operators. Algorithms use these comparison operators to validate some conditions based on which they move forward. Here we are using the Bubble sort algorithm implementation to showcase the working of comparison operators. We are going to sort the students’ grades in ascending order to determine the rankings.
Bubble sort compares two adjacent values using the greater than (>) comparison operator to check whether the given list is in order or out of order.
Example:
Example:
Output:
Explanation: Here, we compared the adjacent values and sorted the list in ascending order according to the student’s grades.
Conclusion
Comparison operators are essential in programming, especially for tasks like sorting and searching, two of the most common operations. They help define conditions that guide control flow based on how comparisons evaluate. Mastering comparison operators is key to writing clean, logical Python code that dynamically reacts to data. Understanding and practicing them lays the foundation for building smart, responsive applications that assess and act based on varying data conditions.
To take your skills to the next level, check out this Python training course and gain hands-on experience. Also, prepare for job interviews with Python interview questions prepared by industry experts.
Master essential Python Basics like file handling and working with modules through these articles–
How to Parse a String to a Float or Int in Python – Learn how to parse a string to a float or int in Python with this simple and effective guide.
Python Arithmetic Operators – Discover how to use Python arithmetic operators efficiently with this beginner-friendly guide.
Python Assignment Operator – Explore the basics of Python’s assignment operator in this clear and concise guide.
Python Bitwise Operators – Get to know how Python bitwise operators work through this simple explanation.
Python Membership and Identity Operators – Understand how membership and identity operators function in Python with this easy guide.
Python Docstrings – Grasp the concept of Python docstrings quickly with this no-fuss introduction.
Access Environment Variable Values in Python – Find out how to read environment variable values in Python with this practical guide.
Python: How to Get the Last Element of List – Check out how to retrieve the last element of a list in Python using this easy-to-follow tip.
Access Index Using For Loop in Python – See how to access the index in a Python for loop effectively with this beginner’s walkthrough.
Python Comparison Operators – FAQs
Q1. Are string comparisons case-sensitive?
Yes, since uppercase letters have lower ASCII values compared to lowercase ones.
Q2. What if I compare two incompatible types, such as int and str?
It will raise a TypeError in Python.
Q3. Can I chain comparison operators like a < b < c?
Yes, Python allows chaining and interprets it as (a < b) and (b < c).
Q4. Can I compare lists or tuples?
Yes, Python compares them element-They return a boolean value, i.e., True or False.
wise from left to right.
Q5. What do comparison operators return in Python?
They return a boolean value, i.e., True or False.