Variables: Labeled Boxes for Information
A variable is a name you give to a piece of information so you can store it and use it later.
Now we meet our first real building block — and it's a friendly one. It's called a variable.
Think of a labeled box
Imagine a box with a label on the front. You can put something inside the box, write a name on the label, and later find your thing again just by reading the label.
A variable is exactly that: a labeled box that stores a piece of information so you can use it later. The label is the variable's name. What's inside is its value.
Here's how it might look in pseudo-code:
name = "Sam"
age = 30Read that as plain English:
- "Make a box labeled
name, and put the wordSaminside it." - "Make a box labeled
age, and put the number30inside it."
The equals sign means 'put into'
That = sign is important, and it's a little sneaky. In programming, = usually does not mean "is equal to" like in math. It means "put the value on the right into the box on the left."
So age = 30 means "put 30 into the box named age." We call this assigning a value.
Why variables are so useful
Once something is in a labeled box, you can use the label instead of repeating the value:
name = "Sam"
show "Hello, " + name
show "Nice to meet you, " + nameThis would display:
Hello, Sam
Nice to meet you, SamIf you later changed name to "Alex", both messages would update automatically. You store the information in one place and reuse it everywhere. That's the whole point.
Variables can change
The name "variable" comes from "vary" — the value can change over time. You can put a new value in the same box:
score = 0
score = 10
score = 25After those three lines, the box labeled score holds 25. Each new assignment replaces what was there before — like emptying the box and putting in something new.
Tip: Give your variables clear, descriptive names. age tells you exactly what's inside. A name like x leaves you guessing. Future-you will be grateful for good labels.
Quick check
Q1 Which everyday object is the best comparison for a variable?
Q2 In most programming, what does score = 10 actually do?
Want to save your progress? It's free.
Create a free account