Topic 3.1: Variables & Assignments | AP CSP Big Idea 3 | APCSExamPrep.com
Variables and Assignments
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
x = x + 1, and on the difference between the value stored and the name printed.
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.
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.
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.
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.
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 |
t = 3 t = t + 4 t = t * 2 print(t)
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
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 |
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:
In your written response, name the variable, say what it represents, and describe how assignment changes it:
- "The variable
scorestores the running total of points the user has earned so far." - "Each assignment updates
scoreusing its own current value, so the total grows as the program runs." - "Because
scoreholds 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 →
Get a free AP CSP question every day
Join 3,000+ students. Daily practice, study tips, and exam strategies.
p and q. What does it actually print?k. Which single line does that correctly?== tests equality; it does not store a value. It would not assign 10 to k.= is the assignment operator: it takes the value 10 on the right and stores it in k on the left.10 = k tries to assign into a number, which is not valid.=: is not a Python operator. Python uses a single = for assignment.n hold 10 after it runs?- I. n = 4 ; n = n + 6
- II. n = 10 ; n = n
- III. n = 20 ; n = n - 5
score, assign it the value 42, then print it. Target output: 42
x already holds 10. Reassign x to 25, then print it. Remember assignment replaces the old value. Target output: 25
count holds 7. Increase it by 1 using the pattern count = count + 1, then print it. Target output: 8
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
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
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
balance and apply the transactions in order:- add
deposit1 - subtract
withdrawal - add
deposit2
a, b, and c, store each of these in its own variable:- their sum
- their product
- the product minus the sum
Frequently Asked Questions
= 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.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.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.x holds 25, then print(x) displays 25, not the letter x.🔗 Continue studying
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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]