Topic 3.1: Variables & Assignments | AP CSP Big Idea 3 | APCSExamPrep.com

AP CSP Course Big Idea 3 3.1 Variables and Assignments
3.1
Big Idea 3 • Algorithms & Programming

Variables and Assignments

🕐 ~40 min FREE 📖 6 MCQ questions 🎮 Variable Tracer game 💻 Live Python editor AAP-1.A • AAP-1.B

After this lesson, you will be able to:

  • Explain that a variable is a named abstraction that holds a value
  • Assign values in Python with = and in AP pseudocode with the left arrow
  • Predict that a new assignment replaces the previous value of a variable
  • Update a variable using its own current value, knowing the right side is evaluated first
  • Trace a sequence of assignments with a variable table to find the final output
  • Swap two variables correctly using a temporary variable
📈 Exam weight: Variables and assignment are the foundation of every tracing and code-reasoning question in Big Idea 3. Expect items that hinge on assignment replacing a value, on self-updates like x = x + 1, and on the difference between the value stored and the name printed.
💡 Think about this first

Every program needs to remember things: a score, a total, a name. It does that with variables. A variable is just a labeled box that holds one value at a time, and assignment is how you put a value in the box. The catch is that putting a new value in replaces the old one, and updates like adding to a running total depend on reading the old value first. Master that and the rest of Big Idea 3 falls into place.

What a Variable Is

A variable is a named location that holds a value. The name is an abstraction: instead of repeating a raw value everywhere, you refer to it by a memorable name and let the program remember the actual value for you. When the CED says a variable is "an abstraction inside a program that can hold a value," this is exactly what it means (AAP-1.A).

You put a value into a variable with an assignment statement. In Python you use the single equals sign =. In AP pseudocode you use the left arrow <-. Both mean the same thing: take the value on the right, and store it in the variable named on the left (AAP-1.B).

color = "blue"
count = 3
print(count)
color <- "blue"
count <- 3
DISPLAY(count)

After these two assignments, color holds the text "blue" and count holds the number 3. The statement print(count) shows the value stored in count, which is 3. It does not show the letters "count"; the variable name is just a label for the value inside.

🎯 What the exam expects

Read the assignment direction right to left: the value on the right goes into the name on the left. Python writes x = 5; the AP reference sheet writes x <- 5. Both create or update the variable x so it holds 5. The = here is assignment, not a claim that two things are equal.

Assignment Replaces the Old Value

A variable holds exactly one value at a time. When you assign a new value, the previous value is replaced and is gone. There is no memory of what used to be there; only the most recent assignment survives.

x = 10
x = 25
print(x)
x <- 10
x <- 25
DISPLAY(x)

The first line stores 10 in x. The second line stores 25 in x, overwriting the 10. By the time print(x) runs, the only value x holds is 25, so 25 is displayed. The 10 was thrown away the instant the second assignment happened.

✍ Mini Exercise 1 • Predict the output
Assignment replaces the old value. What does this print?
x = 10
x = 25
print(x)

Updating a Variable Using Itself

One of the most useful patterns is updating a variable based on its own current value, like count = count + 1. At first this looks impossible, as if the variable is being defined in terms of itself. The trick is the order of operations: the right side is fully evaluated first, using the current (old) value, and only then is the result stored back into the variable.

count = 5
count = count + 1
print(count)
count <- 5
count <- count + 1
DISPLAY(count)

Here count starts at 5. On the update line, Python first computes the right side count + 1, which uses the current value 5 to get 6. Then it stores that 6 back into count, replacing the 5. So the final value is 6. The variable on the left is not touched until the right side is finished.

⚠ The self-update trap

Students sometimes think x = x + 1 is a contradiction because "x cannot equal x plus one." Remember that = is not an equality claim. It is an instruction: compute x + 1 with the old value, then make that the new value of x. This is exactly how running totals and counters work.

✍ Mini Exercise 2 • Old value first
The right side is evaluated first using the OLD value. What does this print?
x = 4
x = x + 3
print(x)

Tracing Sequential Assignments

When several assignments run one after another, trace them with a variable table: one column per variable, one row per line, and you rewrite a cell only when that line changes it. Always evaluate the right side using the values in the current row before you write the new value.

total = 0
total = total + 10
total = total + 5
print(total)
total <- 0
total <- total + 10
total <- total + 5
DISPLAY(total)

Trace it line by line: total starts at 0. The next line computes 0 + 10 and stores 10. The next computes 10 + 5 and stores 15. So print(total) shows 15.

Line executed total before total after
total = 0 none 0
total = total + 10 0 10
total = total + 5 10 15
✍ Mini Exercise 3 • Trace and fill in
Trace each line with the current value, then type the number this prints.
t = 3
t = t + 4
t = t * 2
print(t)
prints:

Swapping Two Variables

Suppose a holds 1 and b holds 9, and you want them to trade values. The natural first attempt, a = b then b = a, does not work. The moment you run a = b, the old value of a is overwritten and lost, so the second line just copies the value back. Both variables end up the same.

The fix is a temporary variable. Save the first value in temp before you overwrite it, then restore it into the second variable:

a = 1
b = 9
temp = a
a = b
b = temp
print(a, b)
a <- 1
b <- 9
temp <- a
a <- b
b <- temp
DISPLAY(a)
DISPLAY(b)

Now temp remembers the original 1 while a is overwritten with 9. Then b is set to temp, which is 1. The result is a real swap: a is 9 and b is 1. Compare that with the broken version that skips the temp holder:

# WRONG: no temp, one value is lost
a = 1
b = 9
a = b
b = a
print(a, b)
# RIGHT: hold the first value in temp
temp <- a
a <- b
b <- temp
⚠ Lost value

Any time you assign into a variable, whatever it held is gone. If you still need that old value later, copy it somewhere first. Swapping is the classic case: without a temp variable, the first assignment destroys the value the second assignment needed.

Key Vocabulary

Term Definition Example
Variable A named abstraction that holds a value the program can use and change count
Value The data currently stored in a variable the 3 in count
Assignment Storing a value into a variable, replacing any previous value x = 5
= (Python) The assignment operator; takes the right side and stores it on the left x = 5
<- (pseudocode) The AP assignment arrow; same meaning as Python = x <- 5
Self-update Assigning a variable a value computed from its own current value x = x + 1
Temporary variable A holder that saves a value so it is not lost during a swap temp
📋 Create Performance Task • Variables that manage complexity

Variables are the first tool your Create Task program uses to store and manage data. A well chosen variable, updated across several steps, is often what lets your program "produce a result" that depends on the input rather than a fixed constant. A running total is the classic example.

A point-earning example

Imagine a program that adds up points a user earns during a session:

score <- 0 score <- score + 10 score <- score + 5 DISPLAY(score)

In your written response, name the variable, say what it represents, and describe how assignment changes it:

  • "The variable score stores the running total of points the user has earned so far."
  • "Each assignment updates score using its own current value, so the total grows as the program runs."
  • "Because score holds a value that changes with the input, the program produces a result that is not fixed in advance."

The trap to avoid

Do not overwrite a value you still need. If you assign into a variable before saving its old contents, that data is gone, which is the exact bug behind a failed swap. When you update a total, use score = score + n so the old value is read before the new one replaces it. See the full Create Task module →

📈
MCQ Practice
6 questions • Exam difficulty and above • Predict before you peek
Question 1 of 6Trace
Predict first: what is the last value stored in x before it prints?
What does this code segment display?
x = 7 x = 2 x = x + 4 print(x)
Correct. x becomes 7, then is replaced by 2. The update computes 2 + 4 using the current value 2, giving 6.
Incorrect. 2 is the value before the last line runs. That line computes 2 + 4 and replaces x with 6.
Incorrect. 11 would be 7 + 4, but 7 was already overwritten by 2 before the update line ran.
Incorrect. 13 is 7 + 2 + 4, but the assignments replace values rather than adding them together.
Question 2 of 6Spot the bug
This segment is meant to swap the values of p and q. What does it actually print?
p = 3 q = 8 p = q q = p print(p, q)
Correct. p = q makes p 8 and destroys the original 3. Then q = p copies 8 back, so both are 8. A temp variable is needed.
Incorrect. That would be a successful swap, but the original 3 is lost on the line p = q before it can be moved to q.
Incorrect. 3 8 is the unchanged starting state, but p is overwritten to 8 on the third line.
Incorrect. q is never assigned 3 here. After p = q both hold 8, so q = p also stores 8.
Question 3 of 6Spot the bug
A student wants to store the value 10 in a variable named k. Which single line does that correctly?
Incorrect. Double equals == tests equality; it does not store a value. It would not assign 10 to k.
Correct. A single = is the assignment operator: it takes the value 10 on the right and stores it in k on the left.
Incorrect. The variable being assigned must be on the left. 10 = k tries to assign into a number, which is not valid.
Incorrect. =: is not a Python operator. Python uses a single = for assignment.
Question 4 of 6I, II, III
For which of the following segments does n hold 10 after it runs?
  • I. n = 4 ; n = n + 6
  • II. n = 10 ; n = n
  • III. n = 20 ; n = n - 5
Incorrect. I ends at 10, but II also ends at 10 because n = n leaves the value unchanged.
Correct. I gives 4 + 6 = 10. II leaves n at 10 since n = n stores the same value. III gives 20 - 5 = 15, not 10.
Incorrect. III leaves n at 15, not 10, so it does not belong. I does end at 10.
Incorrect. III computes 20 - 5 = 15, so it is not 10. Only I and II reach 10.
Question 5 of 6NOT question
The two lines below run in order. Which statement about the result is NOT true?
x = 5 x = x + 1
Incorrect. This IS true. x + 1 uses the old 5 to get 6, which becomes the new value of x.
Incorrect. This IS true. Assignment replaces the previous value, so the 5 is gone.
Incorrect. This IS true. Python computes x + 1 first, then stores the result back into x.
Correct. This is the false statement. The second line replaced 5 with 6, so x no longer holds 5.
Question 6 of 6Trace
Predict first: does t copy the value of s, or stay linked to s?
What does this code segment display?
s = 12 t = s s = 3 print(t)
Correct. t = s copies the current value 12 into t. Changing s afterward does not affect t, so t is still 12.
Incorrect. 3 is the later value of s. t captured 12 at the moment of assignment and does not track later changes to s.
Incorrect. print shows the value stored in t, not the letter s. Variable names are not printed as text here.
Incorrect. print(t) displays the value inside t, which is 12, not the name t.
🎮 Lesson Game
Variable Tracer
Trace each sequence of assignments and predict the value that is displayed. 8 rounds.
0
Correct
1/8
Round
0
Streak 🔥
🐛 Python • what value is stored?
0/8
correct answers
💻 Live Python Code Editor
Practice Problems
Real Python runs right here in your browser. The first time you press Run, the Python engine loads (a few seconds); after that it is instant. Problems build from guided to Create-Task level. Use Hint if you are stuck, and check your output against the target.
Hints used: 0 • Solutions viewed: 0
Level 1 • Guided
Problem 1 of 8 • create and print
Create a variable named score, assign it the value 42, then print it. Target output: 42
Level 2 • Guided
Problem 2 of 8 • replace the old value
x already holds 10. Reassign x to 25, then print it. Remember assignment replaces the old value. Target output: 25
Level 3 • Self-update
Problem 3 of 8 • add one to a counter
count holds 7. Increase it by 1 using the pattern count = count + 1, then print it. Target output: 8
Level 4 • Running total
Problem 4 of 8 • several sequential updates
Start total at 0. Add 10, then add 5, then add 20, each on its own line using total = total + .... Print the final total. Target output: 35
Level 5 • Swap
Problem 5 of 8 • use a temp variable
a is 1 and b is 2. Swap their values using a temporary variable, then print them with print(a, b). Target output: 2 1
Level 6 • Create Task style
Problem 6 of 8 • compute from given values
A cart has item_price, quantity, and a coupon discount. Compute the total as price times quantity minus the coupon, store it in a variable, and print it. Target output: 19
Level 7 • Challenge
Problem 7 of 8 • you write the whole program
Open ended. Write the entire program yourself. Start from balance and apply the transactions in order:
  • add deposit1
  • subtract withdrawal
  • add deposit2
Update the balance each step and print only the final balance. Only the four inputs are given. Target output: 145
Level 8 • Challenge
Problem 8 of 8 • multi-step, several variables
Open ended. Write the entire program yourself. Using a, b, and c, store each of these in its own variable:
  • their sum
  • their product
  • the product minus the sum
Print the sum, then the product, then the difference, each on its own line. Only the three inputs are given. Target output: 12 then 48 then 36

Frequently Asked Questions

A variable is a named abstraction inside a program that holds a value. The name lets you refer to the value and change it later. Creating a variable and giving it a value is done with an assignment statement.
In Python = is the assignment operator. It takes the value on the right and stores it in the variable on the left. It is not a claim that two things are equal, which is why x = x + 1 is valid. AP pseudocode writes the same idea as x <- x + 1.
Because the right side is evaluated first using the current value of x, and only then is the result stored back into x. If x was 5, the program computes 5 + 1 = 6 and makes 6 the new value. The variable is not updated until the right side is finished.
When you assign into a variable, its old value is immediately replaced and lost. If you write a = b first, the original value of a is gone before you can move it to b. A temporary variable saves that value so the swap can finish correctly.
It shows the value currently stored in the variable, not the letters of the name. If x holds 25, then print(x) displays 25, not the letter x.
📦
AP CSP Teacher SuperpackSlides, lesson plans, unit tests for all 5 Big Ideas, $249
Get the Superpack →
🏫
For teachers

The Superpack includes an editable Topic 3.1 slide deck with animated variable-table traces, an assignment and self-update problem bank, a print-ready swapping worksheet, and a unit quiz. View what's included →

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]