FreeLesson 1 of 35 min read
Variables: how a program remembers values
Learn what a variable is, how assignment works, and what really happens when you change a variable's value.
Every useful program needs to remember things: a player's score, a username, the total of a shopping basket. Without memory, a program could only do one fixed calculation and then forget it. Variables are how a program remembers. Understanding them clearly makes loops and functions much easier later in this course.
A name attached to a value
A variable is a name that refers to a value stored in the computer's memory. You create one by writing a name, an equals sign, and a value. This is called assignment. We will use Python in this course because it reads almost like English.
score = 10
player = "Amira"
price = 4.5
After these three lines run, the program remembers three things. The name score refers to the number 10, player refers to the text "Amira", and price refers to 4.5. Whenever you later write score, the computer looks up the name and uses its current value.
Changing a value
Assigning to a name that already exists does not create a second copy. It replaces the old value. The old value is forgotten. This is the key idea: a variable holds one value at a time, and that value is always the most recent one assigned.
Example: tracing a variable line by line
Line 1: score = 10 -> score is 10
Line 2: score = 25 -> score is 25 (10 is gone)
Line 3: score = 3 -> score is 3
After line 3, asking for score gives 3.
The right side runs first
Programs often update a variable using its own old value. To read these lines, follow one rule: the computer works out the right side of the equals sign completely, and only then stores the result in the name on the left.
Example: score = score + 5
Before the line: score is 10
Step 1: read the right side, score + 5
Step 2: look up score, which is 10, so 10 + 5 = 15
Step 3: store 15 in score
After the line: score is 15
Copying from one variable to another
When you write b = a with a number or text, b gets the value a has at that moment. If a changes later, b keeps the value it was given. Think of it as copying a number onto a new sticky note, not tying two notes together.
a = 5
b = a
a = 9
# a is now 9, but b is still 5
(Later, when you meet lists, you will see that some values can be shared between names. For numbers and text, the sticky-note picture works well.)
Habits that keep variables easy to follow:
- Choose names that describe the value, such as total_cost rather than tc.
- Use lowercase words joined by underscores, which is the usual Python style.
- Give a variable a value before you use it, or the program will stop with an error.
- When confused, write a small table of each variable's value after every line.
What to do next time
Whenever you read code that uses variables, trace it. Go line by line, evaluate the right side first, and write down the new value. This simple habit will carry you through the next lesson, where a loop changes the same variable many times in a row.
Key takeaways
- A variable is a name that refers to one value at a time.
- Assigning again replaces the old value, which is then forgotten.
- In an assignment, the right side is worked out first and then stored on the left.
Check your understanding
Written with AI help. Spotted a mistake? Tell us.