Variables & Simple Programs
A variable is a named container that stores information a program can use and change.
Reading is good — doing is better. Practice Variables & simple programs as an interactive lesson.
Try the lessonDefinition
In programming, a variable is like a labeled box where you can put a piece of information — a number, a word, or true/false — and give that box a name so your program can find it later. A simple program is a set of step-by-step instructions that uses variables to store, change, and display information.
Remember the rule
Name it → Store it → Use it: first give your variable a clear name, then assign a value, then use the variable in your program logic.
Key words
- Variable
- A named storage spot in a program that holds a value, like a box with a label on it.
- Value
- The actual information stored inside a variable, such as the number 5 or the word 'hello'.
- Assignment
- When you put a value into a variable using an equals sign, like score = 10.
- Data type
- The kind of value a variable holds — for example, a whole number (integer), a decimal, or text (string).
- String
- A data type made of letters, words, or symbols written inside quotes, like 'Hello, World!'.
- Integer
- A whole number with no decimal point, like 7 or 42, often stored in a variable.
- Output
- Information the program shows to the user, usually with a print or display command.
- Program
- A list of instructions written in a coding language that a computer follows in order.
Worked examples
You want to store a player's score of 10 in a variable called score. How do you write that?
→ score = 10 · The variable name is on the left, the value is on the right of the equals sign.
Now the player earns 5 more points. How do you update the score variable?
→ score = score + 5 (score is now 15) · The program reads the old value of score, adds 5, and stores the new total back in score.
You want to store a player's name. How do you save the name 'Alex' in a variable called playerName?
→ playerName = 'Alex' · Words and letters are strings and go inside quotes; numbers do not need quotes.
Write a simple program that stores two numbers, adds them, and prints the result.
→ a = 8 b = 3 total = a + b print(total) — the screen shows 11 · Each line does one job: store, store, calculate, display.
A program has age = 12. What does print(age + 1) show on the screen?
→ 13 · The program uses the value inside age (12) and adds 1 before printing.
What happens if you type pint(score) instead of print(score)?
→ The program shows an error and stops because pint is not a real command. · Computers follow instructions exactly — a single typo can break the whole program.
Common mistakes
- Writing the value on the left and the variable name on the right — it must be variableName = value, not value = variableName.
- Forgetting quotes around strings, like writing name = Alex instead of name = 'Alex', which makes the program think Alex is another variable.
- Using spaces in a variable name — write playerScore, not player score, because spaces confuse the program.
- Thinking the equals sign means 'is the same as' like in math — in programming it means 'put this value into this variable'.
- Not updating the variable when the value changes, so the program keeps using the old value instead of the new one.
FAQs
Can two variables have the same name?
No. Each variable must have a unique name in the same program. If you reuse a name, the new value replaces the old one and the original information is gone.
Does it matter if I use capital letters in a variable name?
Yes! Most coding languages are case-sensitive, so score, Score, and SCORE are three different variables. Pick one style and stick with it.
Can a variable hold different types of values at different times?
In some languages like Python, yes — you can store a number in age and later store a word there. In other languages you must declare a type and stick to it. For beginners, it is safest to keep the same type in one variable.
Why use variables instead of just typing the numbers directly?
Because variables let you change one value and have the whole program update automatically. If your game speed is stored in a variable, you change it in one place instead of hunting through every line.
What is a good variable name?
A name that describes what the variable holds, like totalScore or userName. Avoid single letters like x unless you are doing simple math, and avoid vague names like stuff or thing.
How is a simple program different from just one line of code?
A simple program has multiple lines that work together — storing values in variables, doing calculations, and showing results — so the computer solves a complete task, not just one tiny step.
Want the whole picture for your child?
Every K–6 subject, an AI tutor that teaches step by step, unlimited practice, and a reward world.
Start a 3-day free trial