What Are Python Operators? I Learned Them by Coding a Farm
(Episode 2 of I Taught an AI to Farm — watch it here: https://youtu.be/qB2nKoU1dkI)
The Problem With Episode 1
At the end of Episode 1, the drone could plant and harvest a single row. But it had a big problem: it didn't know where it was.
It just kept going — past the edge of the farm, out into nowhere, looping forever.
To farm a proper 3×3 grid, the drone needed to be able to ask a question: "Am I in the right spot yet?"
That's where operators came in.
What Is an Operator?
An operator is a symbol that compares or combines two things.
You already use operators in real life without thinking about it:
- Is the pizza bigger than the box? (
>) - Are my two friends the same age? (
==) - Is the shop not closed? (
!=)
In Python, you write those comparisons using operator symbols. The drone uses them to make decisions.
The Three Operators That Built the Farm
1. Equals (==)
This checks if two things are the same. Not to be confused with = (which sets a value — that's assignment, not comparison).
if j == 2:
till()
In plain English: "If I'm at column 2, turn the soil over."
The drone checks its position each step and only acts when it reaches the right spot.
2. Less Than (<) and Greater Than (>)
These check which of two values is bigger.
if j < 3:
move(North)
"If I haven't reached column 3 yet, keep moving north."
This is how the drone stays inside the 3×3 grid instead of wandering off the edge forever.
3. Knowing Where You Are: get_pos_x() and get_pos_y()
Before operators could do anything useful, the drone needed to know its own position. Two built-in functions hand it exactly that:
x = get_pos_x()
y = get_pos_y()
Once the drone knows where it is, every comparison becomes meaningful. Without these, the == checks have nothing to compare against.
The Code That Built the Full Farm
Here's the kind of code that filled the 3×3 grid with carrots, grass, and bushes all running in rotation:
for j in range(get_world_size()):
if j == 2:
till()
if j == 2:
plant(Entities.Carrot)
Not very long, is it?
The loop (for j in range(...)) visits every column in turn. The if j == 2 checks work like traffic lights — only when the drone is in the right spot does it take action.
The shift from Episode 1 to Episode 2 wasn't about learning a lot of new commands. It was about learning to ask a question in code.
The Moment Everything Clicked
In the video, the moment that surprised me was how little code it took. Adding comparison operators — a few if statements with == — turned a drone that planted one crop in one row into a drone managing a full rotating farm.
That's the thing about learning to code with a game: you see the result immediately. The drone either farms the grid or it doesn't. No waiting. No guessing. The code tells you if it worked.
Quick Reference: Types of Python Operators
For anyone who Googled "Python operators" expecting maths symbols, here's the full picture:
| Type | Examples | What they do |
|---|---|---|
| Arithmetic | +, -, *, / |
Calculate a number |
| Comparison | ==, <, >, != |
Return True or False |
| Assignment | =, += |
Store a value |
In the farm game, we mainly use comparison operators — because the drone isn't doing maths, it's making decisions.
Watch Episode 2
Everything above is in the video — with the actual game running, the drone making mistakes, and the 3×3 farm coming to life in real time.
▶️ Watch: I Taught an AI to Farm Ep 2
New to the series? Start with Episode 1 — the drone goes from doing absolutely nothing to harvesting its first crop, using just a loop and a plant command.
Cognito Coding — Learn To Code By Building Real Things. No fluff. No theory dumps. Just real builds.