Pilot · L02 · 50 minutes
Variables, expressions and text
Predict and trace short programs that assign, copy and update variables, evaluate arithmetic using precedence and MOD, and apply a supplied string operation exactly as defined.
Topics: 3.1 Variables and Assignments · 3.3 Mathematical Expressions · 3.4 Strings
Goals and vocabulary
By the end you can:
- Trace a sequence of assignments and state each variable’s value after every line.
- Explain why copying a value does not link two variables, and why an update uses the old value.
- Distinguish an assignment from a comparison in AP pseudocode and Python.
- Evaluate an arithmetic expression with precedence, parentheses and MOD.
- Apply a supplied string procedure exactly as specified.
- Variable
- A named abstraction that holds one value at a time.
- Assignment
- An instruction that evaluates the right-hand side and stores the result in the variable on the left: a ← expression.
- Expression
- A combination of values, variables and operators that evaluates to a single value.
- Precedence
- The order in which operators are applied when parentheses do not say otherwise.
- MOD
- The remainder after whole-number division: 17 MOD 5 is 2.
- String
- An ordered sequence of characters, such as "Chess".
- Concatenation
- Joining two strings end to end into a new string.
Assignment replaces a value
A variable is a named place that holds one value at a time. The assignment statement a ← expression works in two steps: first the expression on the right is evaluated using the values variables hold right now, then the result is stored in the variable on the left, replacing whatever was there.
places ← 12places ← 9DISPLAY(places)This displays 9. The first value is not kept anywhere: the second assignment overwrote it. Python writes the same idea with an equals sign, places = 9, which is why it is easy to misread assignment as a statement that two things are equal.
Copies are not links; updates use the old value
y ← x copies the value x holds at that moment into y. After that, the two variables are independent. Changing x later does not change y.
An update such as score ← score + 1 uses the old value of score on the right, then stores the new value back into score. It is not an equation (which would be false for every number); it is an instruction.
| Written | Meaning | Result |
|---|---|---|
| score ← score + 1 | Assignment: store one more than the old score in score. | Changes score. Produces no true/false value. |
| score = score + 1 | Comparison in AP pseudocode: is score equal to score + 1? | Always false. Changes nothing. |
| score == score + 1 | The same comparison written in Python. | Always False. Changes nothing. |
Worked example: tracing state line by line
Worked example
Predict the output before you read the trace. A trace table has one column per variable and one row per line, and records the value each variable holds after that line runs.
a ← 3b ← aa ← a + 4b ← b * 2DISPLAY(a)DISPLAY(b)| Line | a | b | What happened |
|---|---|---|---|
| 1 | 3 | — | a gets 3. b does not exist yet. |
| 2 | 3 | 3 | b gets a copy of a’s value, 3. |
| 3 | 7 | 3 | Right side uses the old a: 3 + 4 = 7. b is unchanged because it holds its own copy. |
| 4 | 7 | 6 | Right side uses the old b: 3 × 2 = 6. |
| 5–6 | 7 | 6 | Displays 7, then 6. |
The common wrong prediction is “7 then 14”, which assumes b followed a when a changed. The trace shows why it does not.
a = 3b = aa = a + 4b = b * 2print(a)print(b)Arithmetic expressions and precedence
AP pseudocode supports +, −, *, / and MOD. a MOD b is the remainder when a is divided by b, so 17 MOD 5 is 2. Multiplication, division and MOD are evaluated before addition and subtraction; operators with the same precedence are evaluated left to right. Parentheses override this.
| Expression | Steps | Value |
|---|---|---|
| 4 + 6 * 2 | 6 * 2 = 12, then 4 + 12 | 16 |
| (4 + 6) * 2 | Parentheses first: 10, then 10 * 2 | 20 |
| 20 - 9 MOD 4 | 9 MOD 4 = 1, then 20 - 1 | 19 |
| 12 / 4 * 3 | Same precedence, left to right: 3, then 3 * 3 | 9 |
Text values and supplied operations
A string is an ordered sequence of characters, written in quotation marks: "Chess". Exam questions and specifications often define the string procedures you may use. Read the definition and apply it exactly — do not assume extra spaces or punctuation.
concat(s1, s2) returns a new string: the characters of s1 followed immediately by the characters of s2.club ← "Chess"label ← concat(club, "Club")DISPLAY(label)This displays ChessClub with no space, because the definition adds nothing between the strings. To get "Chess Club" you would write concat(club, " Club") or concat(concat(club, " "), "Club").
Numbers and text are different kinds of value. "5" is a one-character string; 5 is a number. In Python, input() always gives a string, so int(input()) is needed before doing arithmetic — you will use this in Lab P01.
Explore each concept
Choose a concept, change its inputs and follow the explanation. Each example models the stated rule or scenario; completing it is practice rather than evidence of mastery.
- Names versus values
- Number, string, Boolean and list
- Copy then update
Change it · step through · explain
Change an input, predict what will happen, then advance one step. Read why the state changes, reset and experiment. This fixed teaching model does not save progress or award XP.
Ready · Make a prediction
- Value typenumberWaiting
- Original number (0–99)7Waiting
Ready to explore: Value types and independent copies
Why: Change an input, predict the first effect, then advance one step.
Try this: Change one input or choose another listed case. Predict the effect, reset and compare the states and governing rule.
Step through and experiment
Change it · step through · explain
Change an input, predict what will happen, then advance one step. Read why the state changes, reset and experiment. This fixed teaching model does not save progress or award XP.
Ready · Make a prediction
- Input 14Waiting
- Input 29Waiting
- Input 35Waiting
Ready to experiment
Why: Predict the first change, then advance one step.
Try this: Change one input, predict the difference, then reset and compare.
Step through and experiment
Change it · step through · explain
Change an input, predict what will happen, then advance one step. Read why the state changes, reset and experiment. This fixed teaching model does not save progress or award XP.
Ready · Make a prediction
- Input 1PLANETWaiting
- Input 22Waiting
- Input 33Waiting
Ready to experiment
Why: Predict the first change, then advance one step.
Try this: Try count 0, a count beyond the end, and start one past the last character.
Common misconceptions
Assignment states that two things are equal
The belief: x ← x + 1 is impossible because no number equals itself plus one.
What is actually true: Assignment is an instruction, not an equation: evaluate the right side with the current value, then store it.
Check yourself: Read the line aloud as “x gets…”. What is x after it runs if x was 4?
Copied variables stay linked
The belief: After y ← x, changing x also changes y.
What is actually true: y receives a copy of the value at that moment. The two variables are independent afterwards.
Check yourself: In your trace table, did you change a column for a variable that was not on the left of the arrow?
Expressions are always worked left to right
The belief: 2 + 3 * 4 is 20.
What is actually true: * , / and MOD come before + and −, so 2 + 3 * 4 is 14. Only operators with equal precedence go left to right.
Check yourself: Which operator in the expression is applied first, and why?
Guided practice
Guided: predict, then trace
With a partner, write a prediction for each fragment before tracing it. Then complete a trace table and compare it with your prediction.
n ← 10n ← n - 4n ← n * 2DISPLAY(n)p ← 5q ← pp ← 1DISPLAY(q)total ← 7 + 8 MOD 3 * 2DISPLAY(total)- Write your prediction for each fragment on its own line before tracing.
- Trace each fragment in a table with one row per line.
- Circle any prediction that was wrong and name the misconception that caused it.
- Translate Fragment B into Python and say which symbol replaces ←.
Independent practice
Independent: trace an unseen fragment
Trace the program below without running it, then answer the questions.
concat(s1, s2) returns s1 followed immediately by s2.k ← 4m ← k + 3k ← m MOD 4word ← concat("Room ", "B")DISPLAY(k)DISPLAY(m)DISPLAY(word)- Complete a trace table for k, m and word.
- State the three values displayed, in order.
- Explain why m does not change when the instruction k ← m MOD 4 runs.
- Change only the number after MOD in the instruction k ← m MOD 4 so that k ends with the value 0. Show your working.
Exit check
- Trace x ← 6, y ← x, x ← y + x and state the final values of x and y.
- Evaluate 15 - 6 / 2 MOD 4 and show the order of the steps.
Check yourself
Practice only: answers are checked in your browser and nothing is saved or sent to your teacher. Each explanation links back to the section it practises.
Lesson practice
Your practice so far
Check an answer to see its rationale.
Apply it somewhere new
Your practice so far
Check an answer to see its rationale.