CognitoCoding All posts
10 July 2026 3 min read by Eris Taylor

What Is a Comparison Operator? (Your Code's Referee)

pythonbeginnerscomparison-operatorscoding-basicsfarmer-was-replaced

Your code has a referee — and that referee decides everything.

Every time your program needs to make a decision, it uses a comparison operator to weigh up two things. Is this number bigger than that one? Are these two values the same? Is this score high enough to unlock the next level?

The result is always the same: True or False. Win or lose. Yes or no.

If you haven't seen the 60-second version yet, the Comparison Operators Short shows the see-saw analogy in action. Then come back here for the full picture.


The Six Operators You Need to Know

Operator What It Asks Example
> Is the left bigger? score > 100
< Is the left smaller? age < 18
== Are they equal? name == "Alice"
!= Are they different? colour != "red"
>= Left bigger or same? health >= 1
<= Left smaller or same? attempts <= 3

Think of it like a see-saw. The operator is the pivot. Python tips the result left (True) or right (False) depending on which side is heavier.


The Mistake Every Beginner Makes: = vs ==

This one trips up everyone at least once.

crop = "ready"    # = means "store this value"
crop == "ready"   # == means "is this the same value?"

= is an assignment. It stores something.
== is a question. It asks something.

Use = inside an if statement and Python will either throw an error or quietly do something you didn't intend. The rule: one equals to store, two equals to compare.


Three Real Examples from The Farmer Was Replaced

Here's how comparison operators show up in a real project — the kind you'd write working through The Farmer Was Replaced.

1. Harvesting only when the crop is ready

if crop_stage >= 4:
    harvest()

The farmer doesn't hack at a seedling. >= makes sure the crop is mature enough before the harvester moves in. If it's exactly at stage 4, that still counts — that's the "or equal to" part.

2. Checking power levels before carrying on

if drone_power > 20:
    keep_farming()
else:
    return_to_base()

If the drone's battery is above 20%, carry on. Otherwise, head home. One comparison, two completely different outcomes. No power level 20 exactly — that's >, not >=, so the drone plays it safe.

3. Catching a mismatch in item counts

if seeds_planted != seeds_needed:
    plant_more_seeds()

!= catches the difference. If what we've planted doesn't equal what we need, get back out there and plant more. Only when those two numbers match does the code skip this block entirely.


How This Flows into If Statements

Every comparison operator produces a Boolean (True or False), and every if statement consumes one:

score = 85

if score >= 50:
    print("You passed!")
else:
    print("Try again.")

score >= 50 evaluates to True (because 85 is greater than or equal to 50), so Python runs the first block and prints "You passed!". Change score to 30 and you get False — straight to else.

The comparison operator is doing the deciding. The if is just acting on the decision.


Your Challenge

Write one if statement using any comparison operator. It can be as simple as this:

my_score = 85
if my_score >= 50:
    print("You passed!")

Or try something more interesting — check a name, compare two counters, gate a move on a power level. Drop it in the comments. What condition did you write, and what does it control?