Article
Variables and assignment
A game shows your score in the corner of the screen. When you win a coin, the score goes up. For the program to do this, it must remember the score somewhere and change it later. The place where a program remembers something is called a variable.
A variable is a labelled box
A variable is a name that stands for one stored value. Think of a box with a label on the front. The label is the variable's name. Whatever is inside the box is its value.
| Name (the label) | Value (inside the box) |
|---|---|
score | 10 |
lives | 3 |
To put a value into a variable we use an assignment statement. In AP pseudocode it uses an arrow. In Python it uses an equals sign:
score ← 10 DISPLAY(score)
Read score ← 10 as "score gets 10". The arrow points to the variable that receives the value. The name is always on the left and the value is always on the right.
The second statement shows 10, not the word score. Without quote marks, DISPLAY(score) shows what is inside the box. With quote marks, DISPLAY("score") would show the word.
Try it: change 10 to a different number and run again.
Check your understanding
What does this program show?
coins ← 7 DISPLAY(coins)
A variable holds only the latest value
A box holds one thing. If you put something new in, the old thing is gone. The same is true for a variable:
score ← 10 score ← 25 DISPLAY(score)
The program shows 25. A trace table shows what happened after each statement:
| After this statement | score |
|---|---|
score ← 10 | 10 |
score ← 25 | 25 |
One box, two assignments
score ← 10
the box named score holds 10
next statementscore ← 25
the box now holds 25 and the 10 is gone
next statementDISPLAY(score)
shows 25, the latest value
What can a variable hold?
A variable can hold different kinds of data. Each kind is a data type.
| Data type | What it holds | Examples |
|---|---|---|
| Number | whole numbers and decimals | 7, 0, 2.5 |
| String | text, inside quote marks | "Mia", "hello" |
| Boolean | only true or false | true, false (Python: True, False) |
A variable can also hold a list, which you will meet later in this unit. You do not have to say the type in advance. The value you assign decides it:
age ← 15 name ← "Mia" isStudent ← true
You can choose any name you like, but a good name says what the variable is for. score is better than s. In Python, a name can have letters, digits and underscores, it cannot start with a digit, and it cannot have spaces. Capital letters matter: score and Score are two different variables.
Copying with a ← b
What happens when the right-hand side is another variable? The computer takes the value that variable holds right now, and puts a copy of it in the variable on the left:
a ← 5 b ← a a ← 9 DISPLAY(b)
| After this statement | a | b |
|---|---|---|
a ← 5 | 5 | (nothing yet) |
b ← a | 5 | 5 |
a ← 9 | 9 | 5 |
The program shows 5. Statement 2 copied the 5 into b. When a later changed to 9, b did not change. The two boxes are separate. b ← a does not mean "b is always the same as a". It means "copy a's value into b, just this once".
Check your understanding
What does this program show?
a ← 2 b ← a a ← 6 DISPLAY(b)
Worked example
Trace this program and say what it shows.
Line 1: lives ← 3 Line 2: coins ← 10 Line 3: lives ← coins Line 4: coins ← 1 Line 5: DISPLAY(lives)
| After line | lives | coins |
|---|---|---|
| 1 | 3 | (nothing yet) |
| 2 | 3 | 10 |
| 3 | 10 | 10 |
| 4 | 10 | 1 |
Line 3 copies the value 10 into lives, so the old 3 is gone. Line 4 changes only coins. Line 5 shows 10.
Common mistakes
- Reading
←as "equals".x ← 5is a command to store a value, not a statement of fact. - Writing it backwards.
5 ← xis not valid. The variable goes on the left. - Thinking a variable remembers its old values. It holds only the most recent one.
- Thinking
b ← alinks the two variables. It copies the value once. Later changes toado not reachb. - Putting quote marks around a variable name.
DISPLAY("score")shows the word score.DISPLAY(score)shows its value.
On the exam
Many questions give a few assignments and ask for the value of a variable at the end, or what DISPLAY shows. Trace it from the top: after each line, write down what every variable holds. Watch for a variable that is assigned twice, and for a ← b statements that copy a value. Remember that in AP pseudocode = compares two values and ← stores one.
Key words
Variable: a name that stands for one stored value.
Value: the data inside a variable.
Assignment: a statement that stores a value in a variable (← in AP pseudocode, = in Python).
Data type: the kind of value, such as number, string or Boolean.
Boolean: a value that is either true or false.
String: text inside quote marks.