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

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

Variables & Assignments

🕐 ~30 min FREE 📖 4 MCQ questions 🎮 Variable Tracer game 💻 Python code editor AAP-1.A • AAP-1.B

After this lesson, you will be able to:

  • Create and assign variables in Python using the = operator
  • Trace variable values step by step through sequential code
  • Update a variable using its own current value (x = x + 1)
  • Swap two variables correctly using a temporary variable
  • Read AP exam pseudocode and map it to Python equivalents
📈 Exam weight: Variable tracing is the single most tested skill in Big Idea 3. Expect 4–6 MCQs that show code and ask what a variable's value is. Master this first — everything else in BI3 builds on it.
💡 Think about this first

Every app you've ever used is keeping track of things. Your score. Your location. Your username. The number of unread messages. Without variables, a program runs once and immediately forgets everything. Variables are how programs remember — they're the fundamental building block of every Create Task you'll write and every tracing question you'll see on the exam.

What Is a Variable?

A variable is a named storage location that holds a value. Think of it as a labeled box: the label is the variable name, and whatever is inside the box is the current value. You can check what's in the box, replace it with something new, or use the value to compute something.

In Python, you create a variable and give it a value using the assignment operator — a single equals sign (=):

score = 0
name  = "Alice"
level = 5
print(score)
score <- 0
name  <- "Alice"
level <- 5
DISPLAY(score)
🎯 Python vs. Pseudocode

The AP exam shows pseudocode using <- (or a left arrow) for assignment and DISPLAY() for output. Python uses = and print(). Same concept, different notation. You write Python for the Create Task — you read pseudocode on the exam MCQs. This lesson teaches both.

✍ Mini Exercise 1 — Predict the output
What does this Python code print?
x = 10
y = 3
print(x)

Updating Variables

Once a variable has a value, you can change it. Each new assignment completely replaces the old value. The previous value is gone — only the new one is stored.

score = 0
score = 10
score = 25
print(score)  # 25
score <- 0
score <- 10
score <- 25
DISPLAY(score)

Updating using the current value

One of the most important patterns — and most tested on the AP exam — is updating a variable based on its current value:

a = 3
a = a + 1  # 3+1=4
print(a)     # 4
a <- 3
a <- a + 1
DISPLAY(a)
⚠ Most common exam mistake

Students see a = a + 1 and think “that's impossible — a can't equal itself plus one!” But = in Python is not mathematical equality. It's an instruction: evaluate the right side, then store the result. The right side (a + 1) always uses the OLD value of a. This counter pattern appears in almost every loop problem on the exam.

✍ Mini Exercise 2 — Predict the output
What does this print?
x = 5
x = x * 2
x = x + 3
print(x)

How to Trace Code

Tracing is the skill of manually following code line by line and tracking what each variable holds at each step. It is the core skill tested in Big Idea 3 MCQs. Here is the method:

  1. Draw a table — one column per variable
  2. Process each line in order, top to bottom
  3. For each assignment: evaluate the right side using current values, then update the variable
  4. Fill in the table after each change
x = 4   # x=4
y = 7   # y=7
x = x + y  # x=11
y = x * 2  # y=22
print(y)    # 22
x <- 4
y <- 7
x <- x + y
y <- x * 2
DISPLAY(y)
✍ Mini Exercise 3 — Fill in the blank
After this code runs, what value does result hold? Type your answer.
a = 6
b = 4
a = a - b
result = a * b
result =

Swapping Two Variables

A classic exam problem: swap the values of two variables. The naive approach destroys one value — you need a temporary variable to hold one value while you overwrite it:

# WRONG: loses original x
x = 10  # x=10
y = 20  # y=20
x = y      # x=20, 10 lost
y = x      # y=20 (wrong!)

# CORRECT: use temp
x    = 10
y    = 20
temp = x
x    = y
y    = temp
print(x, y)  # 20 10
// CORRECT swap:
x    <- 10
y    <- 20
temp <- x
x    <- y
y    <- temp
DISPLAY(x)
DISPLAY(y)
✍ Mini Exercise 4 — Spot the error
This swap code has a bug. Which line should be first to fix it?
# a = 100, b = 200 before this runs:
a = b
temp = a
b = temp

Key Vocabulary

Term Definition Python example
Variable A named storage location that holds a value in a program score = 0
Assignment (=) Stores the value on the right into the variable on the left; replaces any previous value x = 10
Pseudocode <- AP exam assignment operator — same concept as Python's = x <- 10
Tracing Manually following code line by line to track what each variable holds Done on paper or in your head
Incrementing Increasing a variable's value, typically by 1 count = count + 1
Temp variable A variable used temporarily to hold a value while swapping temp = a
📋 Create Task connection

Every Create Task program uses variables. The CPT rubric asks you to identify a variable and explain how it manages complexity. The answer that earns the point is specific: not just “x stores a number” but “this variable tracks the running total across iterations so the program accumulates values without losing previous results.” Understanding variables deeply — not just that they exist — is what earns that point. See the Create Task module →

📈
MCQ Practice
4 questions • AP exam difficulty • Instant feedback
Question 1 of 4
What is the value of result after this Python code executes?

x = 8
y = 3
x = x - y
result = x * y
Incorrect. Trace each line: x=8, y=3, x = 8-3 = 5, result = 5*3 = 15. Not 8.
Incorrect. 24 = 8*3 uses the original x. But x is updated to 5 first. result = 5*3 = 15.
Correct. x=8, y=3. x = 8-3 = 5 (x is now 5). result = 5*3 = 15. Always use updated values.
Incorrect. 5 is the updated x, but result = x*y = 5*3 = 15, not 5.
Question 2 of 4
The AP exam shows this pseudocode:

a <- 10
b <- 20
temp <- a
a <- b
b <- temp

What are the final values of a and b?
Incorrect. temp saves 10, a becomes 20, b gets temp (10). a=20, b=10.
Incorrect. temp saves a's original value (10) so b correctly gets 10, not 20.
Correct. temp=10, a=20, b=temp=10. The temp variable saves a's original value before it gets overwritten. Successful swap.
Incorrect. Nothing swapped? Trace: temp=10, a becomes 20, b becomes temp (10). The values definitely change.
Question 3 of 4
What is displayed after this Python code runs?

count = 1
count = count + 1
count = count + 1
count = count * count
print(count)
Incorrect. count: 1 → 2 → 3 → 3*3 = 9. Not 4.
Incorrect. 6 would be 2*3, but count is 3 when the multiplication runs: 3*3 = 9.
Correct. count=1, then +1=2, then +1=3, then 3*3=9. Each line uses the current value.
Incorrect. 16=4*4. count is 3, not 4, when the multiplication happens.
Question 4 of 4 — NOT question
Which of the following code segments does NOT result in x having the value 10?
Incorrect. x = 10 directly assigns 10. This DOES result in x=10. The question asks which does NOT.
Incorrect. x=7 then x=7+3=10. This DOES result in x=10.
Incorrect. x=20, then x=20//2=10 (integer division). This DOES result in x=10.
Correct. x=5, y=2, x=5+2=7. This gives x=7, NOT 10. This is the one that does NOT produce 10.
🎮 Lesson Game
Variable Tracer
Trace each highlighted line. Pick the correct variable value. 8 questions.
0
Correct
1/8
Question
0
Streak 🔥
🐛 Python — trace the highlighted line:
0/8
correct traces
💻 Python Code Editor
Practice Problems
Write Python code and run it. Judge0 execution coming soon — paste into Replit or Python shell to test now.
Problem 1 of 3
Write Python code that: stores 15 in total, adds 7 to it, multiplies by 2, then prints it. Expected output: 44
Explanation: total = 15, then total = total + 7 (gives 22), then total = total * 2 (gives 44), then print(total)
Problem 2 of 3
Write Python code to swap a = 100 and b = 200. Print a then b after swapping. Expected: 200 then 100
Explanation: You need: temp = a, then a = b, then b = temp. The temp variable saves a's original value (100) before a gets overwritten.
Problem 3 of 3 — Spot & fix
This code should print 30 but prints the wrong value. Find and fix the bug.
Explanation: Trace: x=5, y=6, x=12. print(y) shows 6. The bug is two things: the last line prints y instead of x, AND x = y*2 = 6*2 = 12 not 30. To get 30: y = x (not x+1), then x = y*5 (or similar). Or fix the last line to print(x) after adjusting the formula.

Frequently Asked Questions

In math, = asserts that two things are equal. In Python (and most programming languages), = is an assignment operator — it stores the value on the right into the variable on the left. The right side is always evaluated first. So x = x + 1 is perfectly valid: evaluate x+1 using the current x, then store the result back. The AP exam pseudocode uses <- to make this clearer, but it means the same thing.
You need a temp variable whenever you need to remember a value before overwriting it. The classic case is swapping: if you write a = b, a's original value is gone. Save it first: temp = a, then a = b, then b = temp. Any time an assignment would destroy a value you still need, save it in a temporary variable first.
They mean exactly the same thing — assignment. Python uses = because it's faster to type. AP pseudocode uses <- (or a left arrow) to make it visually clear that information flows from right to left (into the variable). On the AP exam you'll see pseudocode. In your Create Task you write Python (or JavaScript). Both work the same way.
The CPT rubric asks you to identify a variable and explain how it manages complexity. The answer that earns the point is specific about what the variable does: not “x stores a number” but “this variable accumulates the running total so the program can track the sum across multiple iterations without losing previous values.” Shallow = no point. Deep = point.
📦
AP CSP Teacher SuperpackSlides, lesson plans, unit tests for all 5 Big Ideas — $249
Get the Superpack →
🏫
For teachers

The Superpack includes an editable slide deck for Topic 3.1 with animated variable traces, a pseudocode reference sheet, Python code-along activities, and a unit test. 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]