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

What Is a Variable in Coding? (And Why Your Code Can't Remember Without One)

pythonvariablesbeginnerslearn-to-codecoding-concepts

Every piece of code that does anything useful needs to remember something.

A game needs to remember your score. A farm simulation needs to remember how many crops have been harvested. A weather app needs to remember what city you searched for. Without a way to store and retrieve information, code can only run from top to bottom and forget everything it encountered.

That's what a variable is: a named storage slot your code can read and update as it runs.

The Basic Idea

In Python, creating a variable looks like this:

score = 0

You've just told Python: create a storage slot called score, and put the value 0 in it.

Later in your code, you can read from it:

print(score)  # outputs: 0

Or update it:

score = score + 10
print(score)  # outputs: 10

The name on the left is the label. The value on the right is what's stored. That's the whole concept.

Why the Name Matters

You can call a variable almost anything. But the name you choose tells the next person reading your code — usually future-you — what's being stored.

Compare these two:

x = 0
crops_harvested = 0

Both do the same thing. One tells you nothing. The other tells you exactly what's being tracked.

Good variable names are one of the easiest wins in readable code. Pick a name that explains what the value represents, not just that it exists.

Variables Can Hold More Than Numbers

Numbers are the obvious example, but variables can store any type of value:

player_name = "Zero"           # text (a string)
is_game_over = False           # True or False (a boolean)
score = 1450                   # a whole number (an integer)
speed = 3.5                    # a decimal (a float)
inventory = ["wood", "stone"]  # a list of values

Your code treats each of these differently, which is why the type of value matters — but the core idea is the same: a name pointing to a stored value.

Variables Change — That's the Point

The word "variable" comes from "varies" — these values are expected to change as your program runs.

In a game, the score starts at 0 and updates every time the player earns points. In a farm simulation, the crop count increases with each harvest. In a login form, the username starts as an empty string and gets filled in.

If a value never changes, you might be looking at a constant rather than a variable — but that's a topic for another day. For now: most values in a real program need to change, and variables are how you manage that.

A Real Example From The Farmer Was Replaced

In Episode 4 of the Farmer series, the drone hit a problem: it needed to know whether a pumpkin crop was ready to harvest — which required tracking state across multiple loop iterations.

Without a variable, the drone would check the pumpkin on every loop and immediately forget what it found. With a variable:

pumpkin_ready = False

if get_entity_type() == Entity.PUMPKIN:
    pumpkin_ready = True

Now the drone can check pumpkin_ready later in the same loop without re-checking the tile. It remembered.

That's the real power of variables: they let your code build up knowledge as it runs, instead of starting from zero every step.

The Short Version

  • A variable is a named storage slot.
  • You create it with a name and a value: score = 0
  • You read it by using the name: print(score)
  • You update it by reassigning: score = score + 10
  • Good names make your code readable. Bad names make it confusing.
  • Variables change — that's what they're for.

The OCCOB Short for Variables — 60 seconds, one concept, no jargon.