Python Comparison Operators
Table of Contents
In the last lesson, we learned arithmetic operators. Now let’s compare values and get true or false answers.
After learning Python Arithmetic Operators, we can calculate with numbers. Now let’s compare values and turn those comparisons into True or False results. In this tutorial, we’ll learn about Python comparison operators with easy examples.
Let’s Compare Things
Life is full of comparisons.
Is this price higher than that one? Is my score better than last time? Is today hotter than yesterday?
We compare things all the time. And Python can compare things too.
That is what comparison operators are for.
What Are Comparison Operators in Python?
So what do comparison operators do? They compare two values. That’s it. And they give us back a simple answer.
That answer is always True or False.
We compare two things. Python says yes or no.
The Six Comparison Operators in Python
Python has six comparison operators.
| Operator | Name | Example |
|---|---|---|
== | Equal to | 5 == 5 |
!= | Not equal to | 5 != 3 |
> | Greater than | 5 > 3 |
< | Less than | 5 < 3 |
>= | Greater than or equal to | 5 >= 5 |
<= | Less than or equal to | 5 <= 3 |
Let’s see each one.
Python Equal To Operator (==)
So what does == do? It checks if two values are equal.
print(5 == 5) # Trueprint(5 == 3) # Falseprint("hello" == "hello") # Trueprint("hello" == "Hello") # FalseImportant. This is == with two equals signs. Not one.
One equals sign = is for assignment. It means “put this value in this variable.”
Two equals signs == is for comparison. It means “are these two things equal?”
x = 5 # assignment — x is now 5x == 5 # comparison — is x equal to 5? TrueWe should not mix them up. This is one of the most common beginner mistakes.
Python Not Equal To Operator (!=)
So what does != do? It checks if two values are not equal.
print(5 != 3) # Trueprint(5 != 5) # Falseprint("apple" != "orange") # Trueprint("apple" != "apple") # FalseThe ! means “not.” So != means “not equal.”
Python Greater Than Operator (>)
So what does > do? It checks if the left value is greater than the right value.
print(10 > 5) # Trueprint(3 > 7) # Falseprint(5 > 5) # FalseNotice that 5 > 5 is False. Because 5 is not greater than 5. It is equal.
Python Less Than Operator (<)
So what does < do? It checks if the left value is less than the right value.
print(10 < 5) # Falseprint(3 < 7) # Trueprint(5 < 5) # FalseSame thing. 5 < 5 is False. Because 5 is not less than 5.
Python Greater Than or Equal To Operator (>=)
So what does >= do? It checks if the left value is greater than or equal to the right value.
print(10 >= 10) # Trueprint(10 >= 5) # Trueprint(3 >= 7) # FalseNow 10 >= 10 is True. Because it is equal. And equal counts.
Python Less Than or Equal To Operator (<=)
So what does <= do? It checks if the left value is less than or equal to the right value.
print(10 <= 10) # Trueprint(10 <= 5) # Falseprint(3 <= 7) # TrueSame idea.
Comparing Strings in Python
We can compare strings too. But it works a little differently.
Python compares strings alphabetically. Using something called ASCII values. But we do not need to know that. Just remember: it compares letter by letter.
print("apple" == "apple") # Trueprint("apple" == "orange") # Falseprint("apple" < "banana") # TrueWhy is "apple" < "banana" True? Because “a” comes before “b” in the alphabet.
print("apple" < "Apple") # FalseWhy? Because uppercase letters come before lowercase letters in ASCII. So “A” is less than “a”.
This is a bit technical. But for now, let’s remember — string comparison is case sensitive.
If we want to compare without caring about case, we convert both to lowercase first.
print("apple".lower() == "Apple".lower()) # TrueReal-Life Example: Can Someone Vote?
Let’s check if someone can vote.
age = 20
can_vote = age >= 18print(can_vote)Output
TrueBecause 20 is greater than or equal to 18. So the answer is True.
Now let’s check if someone passed an exam.
score = 45passing_score = 50
passed = score >= passing_scoreprint(passed)Output
FalseBecause 45 is not greater than or equal to 50. So the answer is False.
That is how comparison works. Simple.
Comparing Different Types in Python
We can compare numbers with numbers. And strings with strings.
But we cannot compare a number with a string.
print(5 == "5") # FalseWait. Why False? They look the same.
Because 5 is an integer. And "5" is a string. They are different types. So they are not equal.
print(5 == 5.0) # TrueBut 5 and 5.0 are equal. Because Python compares their values. And 5 and 5.0 have the same value.
Interesting, right?
print(5 == "5") # Falseprint(5 == 5.0) # TrueSo let’s be careful. Type matters.
Using Comparisons in Conditions
Comparison operators are mostly used in if statements.
age = 20
if age >= 18: print("You can vote")else: print("You cannot vote")Output
You can voteThe if statement checks the comparison. If it is True, the first block runs. If it is False, the else block runs.
We will learn more about if statements later. For now, let’s just know that comparisons are the foundation of decision-making in Python.
Chaining Comparisons in Python
Python lets us chain comparisons. This is a nice feature.
x = 5print(1 < x < 10)Output
TrueWhat does this mean? It means: is 1 less than x AND x less than 10? Both are true. So the answer is True.
We can chain as many as we want.
x = 5print(1 < x < 10 < 20)Output
TrueThis is cleaner than writing:
print(1 < x and x < 10 and 10 < 20)Both work. But the chained version is easier to read.
Common Mistakes with Python Comparison Operators
Mistake 1: Using = Instead of ==
Here’s a mistake we all make. We write 5 = 5. And we get an error.
print(5 = 5) # Error!Why? Because one equals sign is for assignment. Not comparison.
So what do we do? We use two equals signs.
print(5 == 5) # TrueRemember: one equals is for assignment. Two equals is for comparison.
Mistake 2: Comparing Different Types
Let’s say we write 5 == "5". What do we get? False.
print(5 == "5") # FalseWhy? Because 5 is a number. And "5" is a string. They are different types. So they are not equal.
So what do we do? We convert one to match the other.
print(5 == int("5")) # TrueNow both are numbers. So they are equal.
Mistake 3: Forgetting Case Sensitivity in Strings
Here’s one. We write "apple" == "Apple". And we get False.
print("apple" == "Apple") # FalseWhy? Because “a” and “A” are different in Python’s world. Uppercase comes before lowercase.
So what do we do? We convert both to lowercase first.
print("apple".lower() == "Apple".lower()) # TrueNow they are equal. Because both became “apple”.
Mistake 4: Using <> for Not Equal
If we come from some other languages, we might try <> for not equal. It doesn’t work in Python.
print(5 <> 3) # Error!So what do we do? We use !=.
print(5 != 3) # TrueThat’s the Python way.
Complete Example: Age Checker in Python
Let’s build a simple age checker.
age = int(input("Enter your age: "))
if age < 0: print("That is not a valid age")elif age < 13: print("You are a child")elif age < 20: print("You are a teenager")elif age < 60: print("You are an adult")else: print("You are a senior")If we enter 25, the output is:
You are an adultSee how comparisons work together? Each elif checks a condition. The first one that is True runs. And the rest are skipped.
Next, we will learn about Logical Operators so we can continue step by step.