Python Comparison Operators

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.

OperatorNameExample
==Equal to5 == 5
!=Not equal to5 != 3
>Greater than5 > 3
<Less than5 < 3
>=Greater than or equal to5 >= 5
<=Less than or equal to5 <= 3

Let’s see each one.

Python Equal To Operator (==)

So what does == do? It checks if two values are equal.

print(5 == 5) # True
print(5 == 3) # False
print("hello" == "hello") # True
print("hello" == "Hello") # False

Important. 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 5
x == 5 # comparison — is x equal to 5? True

We 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) # True
print(5 != 5) # False
print("apple" != "orange") # True
print("apple" != "apple") # False

The ! 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) # True
print(3 > 7) # False
print(5 > 5) # False

Notice 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) # False
print(3 < 7) # True
print(5 < 5) # False

Same 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) # True
print(10 >= 5) # True
print(3 >= 7) # False

Now 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) # True
print(10 <= 5) # False
print(3 <= 7) # True

Same 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") # True
print("apple" == "orange") # False
print("apple" < "banana") # True

Why is "apple" < "banana" True? Because “a” comes before “b” in the alphabet.

print("apple" < "Apple") # False

Why? 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()) # True

Real-Life Example: Can Someone Vote?

Let’s check if someone can vote.

age = 20
can_vote = age >= 18
print(can_vote)

Output

True

Because 20 is greater than or equal to 18. So the answer is True.

Now let’s check if someone passed an exam.

score = 45
passing_score = 50
passed = score >= passing_score
print(passed)

Output

False

Because 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") # False

Wait. 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) # True

But 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") # False
print(5 == 5.0) # True

So 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 vote

The 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 = 5
print(1 < x < 10)

Output

True

What 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 = 5
print(1 < x < 10 < 20)

Output

True

This 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) # True

Remember: 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") # False

Why? 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")) # True

Now 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") # False

Why? 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()) # True

Now 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) # True

That’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 adult

See 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.