6m left·0%
Reading Time: 6 min
Last Updated: June 25, 2026
Main Ideas: 4
Reading Time: 6 min
Last Updated: June 25, 2026
Main Ideas: 4

Topic 3.1 Notes – Variables and Assignments

Verified for 2027 AP® Computer Science Principles Exam
Read aloud
Variables and assignments are how programs remember information. A variable gives a name to a value so the program can store it, update it, and use it later. This topic is about what variables actually are, what kinds of values they can hold, and how assignment changes those values step by step.

1. What a Variable Is

A variable is an abstraction inside a program that represents a value. Think of it as a labeled storage space in memory.

  • Each variable has one value at a time.
  • That value could be:
    • A single number
    • A string of text
    • A Boolean
    • A whole list (which itself contains many values)

So when we say “one value,” that includes collections. A variable can hold one list, and that list can hold many items.

This is how programs solve general problems. Instead of hard-coding “5 students,” you store the number in a variable. Then the same algorithm works for 5, 50, or 5,000.

Key Properties

  • A variable must be assigned a value before it’s used.
  • The value stored is always the most recent assignment.
  • Variable names are:
    • Case-sensitive → total and Total are different.
    • Spelling-sensitive → count and coutn are different variables.

Naming Variables Well

Meaningful names make code readable:

  • totalScore is clearer than t
  • userAge is clearer than x

On the Create Task, clear variable names make your algorithm easier to explain in writing. Readers should understand what your variable represents without guessing.

2. Data Types Variables Can Represent

A data type is a category of value. The type determines what operations are allowed.

Numbers

Used for math.

  • In AP CSP, mostly integers (positive or negative whole numbers).
  • You can use arithmetic: +, -, *, /

Example:

points ← 12
bonus ← 3
total ← points + bonus

Strings

A string is text inside quotation marks.

  • "Hello"
  • "2026" (this is text, not a number)

You cannot do math with strings. "5" + "3" does not behave like 5 + 3 in CSP pseudocode.

Strings are used for:

  • Names
  • Messages
  • User input
  • IDs like phone numbers (since you don’t calculate with them)

Booleans

A Boolean has only two values:

  • true
  • false

Used in:

  • Conditionals
  • Comparisons
  • Decision-making

Example:

passed ← true

Lists

A list is an ordered sequence of elements.

  • Stored as a single value in one variable.
  • Can contain numbers, strings, Booleans, or a mix.

Example:

scores ← [80, 90, 75]

Even though there are three numbers, scores holds one list. The variable points to one list value, and that list contains multiple elements.

One variable storing one list value

Choosing the Right Type

Match the type to how you’ll use the data:

  • Doing calculations → number
  • Storing text → string
  • Yes/no state → Boolean
  • Group of related items → list

This shows up in multiple-choice questions where the “wrong” answer uses the wrong type, like storing a zip code as a number and then trying to preserve leading zeros.

3. Assignment and the Assignment Operator

Assignment is how variables get values.

In AP CSP pseudocode, the assignment operator is:

a ← expression

Here’s what happens:

  1. The right side is evaluated first.
  2. A copy of that result is stored in the variable on the left.
  3. The old value is replaced.

This is not math equality.
It means “store this value in this variable.”

Reassignment

Variables can change:

level ← 1
level ← 2

After this, level is 2. The first value is gone.

Programs constantly update variables as they run.

4. Tracing Variable Values After Assignments

You must be able to determine a variable’s value after several lines of code.

Example:

x ← 4
y ← x
x ← x + 3
DISPLAY(y)

Trace it:

  • x starts as 4
  • y gets a copy of 4
  • x becomes 7
  • DISPLAY(y) shows 4

y does not change when x changes. It stored a copy.

One way to stay organized is to use a small trace table as you move through each line:

Step-by-step trace table for variable updates

On quizzes and the AP exam, they love stacking assignments like this. Go line by line. Update values immediately. Use the most recent assignment only.

Key Takeaways

A variable stores one value at a time, even if that value is a list.
The right side of ← is evaluated before the value is stored.
Assignment makes a copy of the value at that moment.
The current value of a variable is always its most recent assignment.
Data type determines what operations are allowed on a value.
Case and spelling differences create completely different variables.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse this website.

Notes

1 credit used · 5/5 remaining