Comparison operators compare two values and always give back a Boolean: True or False. They are the backbone of every if statement and loop condition you will write, for example “is the marks value at least 33?” or “has the user typed the right PIN?”
Python has six comparison operators (also called relational operators).
The Six Comparison Operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
== |
Equal to | 5 == 5 |
True |
!= |
Not equal to | 5 != 3 |
True |
> |
Greater than | 7 > 9 |
False |
< |
Less than | 7 < 9 |
True |
>= |
Greater than or equal to | 10 >= 10 |
True |
<= |
Less than or equal to | 4 <= 3 |
False |
Basic Example
a = 15
b = 10
print(a == b)
print(a != b)
print(a > b)
print(a < b)
print(a >= 15)
print(b <= 9)
Output:
False
True
True
False
True
False
Using Comparisons in if Statements
This is where you will use comparison operators most. Python checks each condition from the top and runs the first block whose condition is True:
marks = 72
if marks >= 90:
print("Grade A")
elif marks >= 60:
print("Grade B")
else:
print("Grade C")
Output:
Grade B
72 is not >= 90, so the first block is skipped. 72 >= 60 is True, so “Grade B” is printed and the rest is ignored.
Chaining Comparisons
Python lets you write a range check the way you would in maths. 1 < x < 10 means 1 < x and x < 10.
x = 7
print(1 < x < 10)
print(1 < x > 10)
print(10 > x >= 7)
Output:
True
False
True
The second line is read as 1 < x and x > 10, and 7 is not greater than 10, so the answer is False. Chaining works with any comparison operators, but keep it readable; a chain like 1 < x > 10 confuses readers.
Comparing Strings
Strings are compared character by character using each character’s Unicode number (you can see it with ord()). This is why capital letters come before small letters.
print("apple" == "apple")
print("Apple" == "apple")
print("apple" < "banana")
print("Zebra" < "apple")
print(ord("Z"), ord("a"))
print("10" < "9")
Output:
True
False
True
True
90 97
True
Two results surprise most beginners:
"Zebra" < "apple"is True because “Z” is 90 and “a” is 97. To compare without caring about case, convert both first:"Zebra".lower() < "apple".lower()."10" < "9"is True because the strings are compared character by character, and “1” comes before “9”. Numbers typed by a user arrive as strings frominput(), so convert them withint()orfloat()before comparing.
Comparing Lists and Tuples
Lists and tuples are compared item by item from the left. The first pair of items that differ decides the result.
print([1, 2, 3] < [1, 2, 4])
print((2, 5) > (2, 3, 9))
print([1, 2] == [1, 2])
Output:
True
True
True
In the second line the first items are equal (2 and 2), then 5 > 3 decides the answer. The extra 9 is never looked at.
Comparing Different Types
print(5 == 5.0)
print(5 == "5")
try:
print(5 < "5")
except TypeError as e:
print("TypeError:", e)
Output:
True
False
TypeError: '<' not supported between instances of 'int' and 'str'
An integer and a float can be compared freely. == between unrelated types such as a number and a string simply returns False, but ordering operators like < raise a TypeError.
The Floating-Point Trap
print(0.1 + 0.2 == 0.3)
print(0.1 + 0.2)
import math
print(math.isclose(0.1 + 0.2, 0.3))
Output:
False
0.30000000000000004
True
Computers store most decimal numbers as close approximations, so a tiny error creeps in. Never use == to compare results of float arithmetic. Use math.isclose() instead. (For money, many programs store paise as whole numbers, or use the decimal module.)
== vs is
== asks “do these have the same value?”, while is asks “are these the very same object?” Two separate lists with the same items are == but not is. Use == for comparing values, and is only for checks like if result is None:. See identity operators in Python for details.
Practice Questions
print("cat" > "Cat")print(3 == 3.0 == 3)print(2 < 4 < 3)
Answers: 1) True, because “c” (99) is greater than “C” (67). 2) True, because both comparisons in the chain are True. 3) False, because the chain means 2 < 4 and 4 < 3, and 4 < 3 is False.
Frequently Asked Questions
How many comparison operators are there in Python?
Six: ==, !=, >, <, >= and <=.
What is the difference between = and == in Python?
= assigns a value to a variable. == compares two values and returns True or False.
Can I compare a string with a number?
With ==, yes, but the result is always False. With < or >, Python raises a TypeError. Convert the string with int() or float() first.
Does Python support a < b < c?
Yes. Comparison chaining is built into Python, and a < b < c means a < b and b < c.
Next: Python bitwise operators · Previous: Python logical operators · All operators: Python operators overview