Python Comparison Operators are the basic and essential blocks of conditional statements. They are used to compare two values and return the boolean result, True or False. Other than conditional statements, these are also widely used in decision-making, control flow, validation, sorting, filtering, and more. In this article, you will understand Python comparison operators, their syntax, and how they are applied in the real world in detail.
Table of Contents:
What are Comparison Operators in Python?
Comparison operators are one of the several types of Python operators. They help you compare two values and evaluate their relationship. These operators are mostly used in conditional statements like if, elif, and while to control the flow of the program based on specific criteria. The result of these expressions is always a boolean value, either True or False. There are six fundamental types of Comparison Operators in Python.
Types of Comparison Operators in Python
The comparison operators in Python are equal to, not equal to, greater than, less than, greater than or equal to, and less than or equal to. Let us learn them with code examples.
Equal to (==) Operator
This operator checks whether the two values, or operands, are equal or not. If they are equal, it will return True, and if they are not, it will return False.
Example:
Output:
Explanation: Here, the values were compared, and based on the result, the if block or else block was printed.
Not Equal To (!=) Operator
This operator returns True for unequal values or operands. If they are equal, the operator will return False.
Example:
Output:
Explanation: Here, since x is not equal to 5, the if block condition was validated, and hence, the code in the if block got printed. Whereas y was equal to 5, hence nothing was printed on the output console.
Greater Than (>) Operator
This operator will return True if the first operand is greater than the second operand. If it is not, it will simply return False.
Example:
Output:
Explanation: Here, since b is greater than a, it validates the condition in the elif block, and hence, the elif block was implemented.
Less Than (<) Operator
This operator compares the two values and checks whether the left value is smaller than the right value. If that is the case, it will return True or otherwise, it will return False.
Example:
Output:
Explanation: Here, since a was less than b, the ‘if’ block code was executed.
Greater Than or Equal to (>=) Operator
This operator is a mix of the equal to operator and the greater than operator. This operator, when used in an expression, will return True if the first operand is greater than or equal to the second operand.
Example:
Output:
Explanation: Here, a was greater than or equal to b. Therefore, it was printed on the output console based on the condition check.
Less Than or Equal to (<=) Operator
This operator is made from the ‘less than’ symbol and the ‘equal to’ symbol. This operator returns True for the expression that has the left operand less than or equal to the right operand. Else it returns False.
Example:
Output:
Explanation: Here, a is less than b. This made the condition in the if block true. Therefore, the if block was executed.
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.
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.