LEARN Python - 2.4 Comparison Operators How Python Judges Things - zerotopyhero.com

Learn Python – 2.4: Comparison Operators – How Python Judges Things

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 → True

  • No → 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:

OperatorMeaningExample
==Equal toage == 18
!=Not equal toname != "Alex"
>Greater thanscore > 50
<Less thanprice < 100
>=Greater than or equal toage >= 18
<=Less than or equal tolevel <= 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 value

  • Two 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?

Every comparison returns a boolean value:

  • True

  • False

Even when you don’t see it.

For example:

				
					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 True or False

  • How comparisons control if / elif / else

This is core logic.
Everything else builds on this.

Mini Quiz

Try these:

  1. What’s the difference between = and ==?

  2. What does >= mean?

  3. Will "5" == 5 be true or false? Why?

  4. What does this print?

				
					print(10 <= 10)

				
			

     5. What comparison would you use to check if a number is not equal to 3?

ZeroToPyHero