Unit 2 · Programming 1 · Lesson 2.2

Variables and Assignment

A program that can’t remember anything is just a printout. Today you give the machine a memory — and meet the notation the AP exam writes it in.

Big Idea 3 — Algorithms & ProgrammingDual-pane starts here

Before this lesson — read on Runestone

The book’s version of today. Read it after you try the box model below — the picture makes the words land. Enrolled in our Runestone course? Open it from there so your progress counts.

1

Where We Are

People→ Bits→ Programs→ Internet→ Security→ Big Data→ Impact

Deeper into Programs. In 2.1 the machine could only speak (print). Give it a place to store values and it can start to reason — hold a score, remember a name, do math on something it was told a moment ago.

2

The Hook

a box with a label. Picture a box with a sticky note on the front. The note says score. Inside is a number. You can read what’s inside any time, and you can throw the old contents out and put something new in — the label never changes, the contents do.

That is a variable, and it is the whole idea. Here is the part worth bracing for now: the exam does not write it the way Python does. Python writes score = 0. The AP Pseudocode writes score ← 0. Same box, same note, same act — two spellings. From today, you’ll see both, every time.

3

The Idea

An assignment statement puts a value into a variable. Read it right to left: work out the value on the right, then drop it into the box named on the left. The = here does not mean “is equal to” the way it does in math — it means “gets.” score = 0 is read “score gets zero.” The exam’s arrow, ←, actually shows that direction, which is why it’s a nicer picture of what’s happening.

New rule, and it’s the one that trips everyone: a variable can appear on both sides of an assignment. When it does, the right side is worked out first, using the old value, and only then is the box overwritten. Watch:

Python — you write this

score = 0
score = score + 10
score = score + 5
print(score)   # 15

AP Pseudocode — you read this

score ← 0
score ← score + 10
score ← score + 5
DISPLAY(score)   // 15

Hover or tap any line to light up its twin in the other language. They are the same program, line for line.

Line 2 reads “score gets score-plus-ten.” Python first computes the right side with the current value (0 + 10 = 10), then stores 10 back in score. The old value is gone. This is how a running total works, and you’ll use it in every project from here to May.

Three words for the exam. The box is a variable. The = (or ←) is the assignment operator. What ends up inside is the value. The exam mostly shows the arrow, so train your eye: x ← 7 is just x = 7 wearing the exam’s uniform.
4

Try It — The Swap Problem

Here is the classic. You have two variables, a and b, and you want them to trade values. The obvious way fails, and understanding why is a guaranteed exam question. Step through it and watch the boxes.

Press Start to load the program.

The fix is a third box — a temp variable that holds one value safely while the other is overwritten. Same idea, both languages:

Python

temp = a
a = b
b = temp

AP Pseudocode

temp ← a
a ← b
b ← temp
5

Vocabulary

variable Programs
A named location in memory that holds a value, which can be read and changed while the program runs. The “labeled box.”
assignment operator Programs
The symbol that stores a value in a variable: = in Python, ← in AP Pseudocode. It means “gets,” not “is equal to.”
value Programs
The data currently stored in a variable — a number, a piece of text, and so on. Assigning a new value discards the old one.
6

Check

Five questions, some in AP Pseudocode on purpose — you read both now. Pick an answer for instant feedback.

0 of 5 answered
7

Impact Check

One notation, one exam, everyone

Why does the AP exam use its own pseudocode with a ← instead of just picking Python? Because a national exam can’t assume every classroom teaches the same language. Some schools teach Java, some JavaScript, some Python, some a block language. A shared, language-neutral notation is an abstraction — the same idea you named in 1.7 — that lets one exam be fair across very different classrooms.

It costs you something real: a second spelling to learn. It buys something real too: a student who moves schools, or learns a different language next year, still shares one vocabulary with everyone else. That is the recurring shape of these decisions — a cost and a benefit, landing on different people — and you’ll keep meeting it all year.

8

Connections

Came from 2.1 — a program was a list of actions. Now it has memory between actions, which is what makes it more than a fancy printout.
Uses 1.7 — the AP’s language-neutral pseudocode is an abstraction: hide the specific language, keep the idea.
Next in 2.3 — the right side of an assignment gets interesting. Expressions, arithmetic, and the exam’s favorite operator, MOD.
Returns on the exam — trace questions live on assignment sequences and the swap. When a variable is on both sides, compute the right side with the old value first, every time.
← 2.1 Your First Real Program