What’s a Variable? – the Magic Boxes of Computer Science
Introduction
Imagine you have a Labelled Box that can hold anything you want—a number, a word, or even a secret code. In computer programming, that box is called a Variable. Variables let computers Store, Change, and Use information, just like you might keep a snack in your lunchbox and swap it for a different treat later. Let’s explore how these magic boxes work!
1. Naming Your Box – The Variable’s Name
Every variable needs a name so the computer knows which box you’re talking about.
-
Rules:
- Start with a letter (a‑z) or an underscore
_. - No spaces; use Camelcase (
myScore) or Snake_case (my_score). - Avoid words the computer already uses (called keywords).
- Start with a letter (a‑z) or an underscore
-
Why It Matters:
If you name two boxes the same, the computer gets confused and might use the wrong value—just like mixing up two identical backpacks!
Example
age = 9 # The box named "age" holds the number 9
favoriteColor = "blue" # The box "favoriteColor" holds the word blue
2. Putting Things Inside – Assigning Values
Assigning means putting a value into a variable. Think of it as putting a toy into a toy chest.
- Cause → Effect:
- Cause: You write
score = 0. - Effect: The computer creates a box called
scoreand puts the number0inside.
- Cause: You write
You can later Update the value: score = score + 5 (the box now holds 5).
Mini Experiment 1 – “counting Coins”
- Grab a piece of paper and draw three boxes labeled Coins, Bonus, and Total.
- Write
coins = 3. - Write
bonus = 2. - Add them:
total = coins + bonus. - What number is inside Total? (Answer: 5)
3. Changing over Time – Variables Are Not Fixed
Unlike a real box that stays the same size, a variable’s value can Change whenever the program tells it to.
-
Example: A video game character’s health might start at
100. When the character gets hit, you dohealth = health - 20. The health variable now holds80. -
Cause → Effect:
- Cause: The character is hit.
- Effect: The
healthvariable’s value decreases, which might later cause the game to end if health reaches0.
Mini Experiment 2 – “growing Plant”
- Create a variable
height = 5(centimeters). - Each day the plant grows 2 cm:
height = height + 2. - After 3 days, what is
height? (Answer: 11 cm)
4. Did You Know? 🤔
- The word Variable comes from the Latin variabilis, meaning “changeable.”
- Early computers didn’t have fancy names; programmers used simple letters like
A,B, andCas variables. - In many programming languages, you can store Different Kinds of data in variables: numbers (Integers, Floats), text (Strings), and even true/false values (Booleans).
Mini Quiz & Experiment
| Question | Your Answer |
|---|---|
| 1️⃣ What must a variable name start with? | |
2️⃣ If score = 10 and you run score = score + 7, what is the new score? | |
| 3️⃣ True or False: A variable can hold both a number And a word at the same time. |
| 4️⃣ Write a tiny program (in any language) that creates a variable called cookies with value 4, then adds
| 4️⃣ Write a tiny program (in any language) that creates a variable called cookies with value 4, then adds 3 more cookies and prints the total number of cookies. | |
Example Answer (Python):
cookies = 4
cookies = cookies + 3
print(cookies) # 7
Keep experimenting with your own numbers! 🎉
You’ve mastered the magic of variables—keep coding!