So far, Python can make decisions.
It can say:
If this is true, do something
Else, do something else
Elif this other thing is true, do another thing
But there’s a quiet question hiding underneath all of that:
How does Python actually decide whether something is true or false?
That’s where comparison operators come in.
They’re the tools Python uses to compare values and answer simple questions like:
Is this bigger than that?
Are these two things the same?
Is this at least as large as that?
Think of them as Python’s judgment tools.
No emotions. No opinions. Just comparisons.
What Is a Comparison Operator?
A comparison operator compares two values and produces a yes-or-no answer.
In Python terms:
Yes →
TrueNo →
False
For example:
5 > 3
That question answers:
True
Python isn’t printing anything here, it’s just evaluating the comparison.
The Most Common Comparison Operators
Here are the ones you’ll use constantly:
| Operator | Meaning | Example |
|---|---|---|
== | Equal to | age == 18 |
!= | Not equal to | name != "Alex" |
> | Greater than | score > 50 |
< | Less than | price < 100 |
>= | Greater than or equal to | age >= 18 |
<= | Less than or equal to | level <= 10 |
Each one asks a clear question.
The Most Common Beginner Mistake (And How to Avoid It)
This one gets everyone once.
age = 18
if age = 18: # ❌ This is wrong
print("Allowed")
Why is this wrong?
Because:
=means assign==means compare
So this is correct:
if age == 18:
print("Allowed")
Rule to remember:
One equals sign (
=) gives a valueTwo equals signs (
==) ask a question
If Python yells at you about this, it’s not being rude — it’s saving you.
Comparing Numbers
Numbers are the easiest place to start.
score = 72
if score >= 70:
print("You passed!")
Python checks the comparison:
Is 72 greater than or equal to 70?
Yes → run the code
You can test comparisons directly too:
print(10 > 5) # True
print(3 < 1) # False
print(7 == 7) # True
Seeing True and False printed helps things click.
Comparing Strings (Text)
You can compare strings too, usually for equality.
name = input("What's your name? ")
if name == "Alex":
print("Welcome back, Alex!")
Python compares strings exactly:
Uppercase matters
Lowercase matters
Spaces matter
So "alex" is not the same as "Alex".
That’s why you’ll often see this:
name = input("What's your name? ").strip().lower()
if name == "alex":
print("Welcome back!")
Normalize first.
Compare second.
Much fewer surprises.
What Do Comparisons Actually Return?
result = 5 > 3
print(result)
Output:
True
That result is what your if statement uses to decide what to do next.
Comparisons Inside If Statements
An if statement is basically asking:
“Is this comparison true?”
temperature = 30
if temperature > 25:
print("It's warm today.")
else:
print("It's cool today.")
The comparison controls the flow.
Nothing mystical is happening, just a yes-or-no check.
Comparing the Same Value Multiple Times
Sometimes you’ll see comparisons chained through logic:
age = 16
if age >= 13 and age < 18:
print("Teen")
Don’t worry about and yet, that’s coming next.
For now, just notice that comparisons are the building blocks.
Thinking Like Python
When Python sees this:
if score >= 70:
It mentally asks:
“Is score greater than or equal to 70?”
If yes → run the block
If no → skip it
That’s it.
If your comparison makes sense in plain English, it usually works in code.
What You’ve Learned
You now know:
What comparison operators are
How Python compares values
The difference between
=and==How comparisons return
TrueorFalseHow comparisons control
if / elif / else
This is core logic.
Everything else builds on this.
Mini Quiz
Try these:
What’s the difference between
=and==?What does
>=mean?Will
"5" == 5be true or false? Why?What does this print?
print(10 <= 10)
5. What comparison would you use to check if a number is not equal to 3?