Topic 3.1: Variables & Assignments | AP CSP Big Idea 3 | APCSExamPrep.com
Variables & Assignments
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
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)
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.
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)
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.
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:
- Draw a table — one column per variable
- Process each line in order, top to bottom
- For each assignment: evaluate the right side using current values, then update the variable
- 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)
result hold? Type your answer.a = 6 b = 4 a = a - b result = a * b
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)
# 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 |
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 →
Get a free AP CSP question every day
Join 3,000+ students. Daily practice, study tips, and exam strategies.
result after this Python code executes?x = 8y = 3x = x - yresult = x * y
a <- 10b <- 20temp <- aa <- bb <- tempWhat are the final values of a and b?
count = 1count = count + 1count = count + 1count = count * countprint(count)
x having the value 10?total, adds 7 to it, multiplies by 2, then prints it. Expected output: 44
a = 100 and b = 200. Print a then b after swapping. Expected: 200 then 100
Frequently Asked Questions
🔗 Continue studying
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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]