Fiveable

⌨️AP Computer Science Principles Unit 3 Review

QR code for AP Computer Science Principles practice questions

3.1 Variables and Assignments

3.1 Variables and Assignments

Written by the Fiveable Content Team • Last updated June 2026
Verified for the 2027 exam
Verified for the 2027 examWritten by the Fiveable Content Team • Last updated June 2026
⌨️AP Computer Science Principles
Unit & Topic Study Guides

AP Computer Science Principles Exam

Pep mascot

TLDR

A variable is a named spot in your program that holds one value at a time, and the assignment operator stores a value into it. On the AP Computer Science Principles exam, the value in a variable is always the most recent one assigned, and the official pseudocode uses the left-arrow for assignment. Get comfortable tracing assignments line by line so you can predict what a variable holds at any point.

Why This Matters for the AP Computer Science Principles Exam

Variables and assignments are the foundation for almost every program you read or write in AP Computer Science Principles. On the multiple-choice section, you will trace short code segments and determine what value a variable holds after a series of assignments. That same skill carries into your Create performance task program, where you choose meaningful variable names and store values so your code is readable and easy to explain in your written responses.

The exam uses a single set of pseudocode on the reference sheet, so you need to recognize the assignment operator no matter what programming language your class used. Knowing how values are copied during assignment helps you avoid the most common tracing mistakes.

Key Takeaways

  • A variable is an abstraction that holds one value at a time, but that one value can be a collection like a list.
  • The assignment operator changes what value a variable represents. The exam pseudocode uses a ← expression, which evaluates the expression first, then stores a copy of the result in a.
  • A variable always holds the most recent value assigned to it. Reassigning overwrites the old value.
  • When you assign one variable to another, you copy the current value. Changing the original later does not change the copy.
  • Common data types include numbers, Booleans, lists, and strings. Some values fit one type better than another.
  • Meaningful, descriptive variable names make code readable and easier to explain.

How a Variable Holds a Value

A variable is a named placeholder in your program that can hold a value. Each variable has storage that represents one value at a time, but that single value can be a collection, such as a list, that contains many values inside it.

You store a value with the assignment operator. The AP exam pseudocode uses the left-arrow :

</>Code
a ← expression

This evaluates expression first, then assigns a copy of the result to the variable a. To change what a variable holds, just assign it again.

Many programming languages use = for assignment instead of . For example, in Python:

</>Code
a = expression

The exam reference sheet only uses , so practice reading that notation even if your class used a different language.

The Most-Recent-Value Rule

A variable always holds the most recent value assigned to it. This is the rule that trips up the most students on tracing questions, so trace carefully.

Here is the classic example using exam pseudocode:

</>Code
number ← 7
changedNumber ← number
number ← 17
DISPLAY(changedNumber)

This displays 7. When changedNumber ← number runs, number still equals 7, so a copy of 7 is stored in changedNumber. Changing number to 17 afterward does not affect changedNumber, because the assignment copied the value, not a live link to number.

In Python the same idea looks like this:

</>Code
number = 7
changed_number = number
number = 17
print(changed_number)

The output is still 7 for the same reason.

When you trace, write the current value of each variable next to each line. Update only the variable on the left side of the assignment. Hand tracing like this is the fastest way to answer "what does this display" questions correctly.

Data Types

Data types are categories of data your program can represent. The types you should know for AP Computer Science Principles are numbers, Booleans, lists, and strings.

  • Numbers include whole numbers and decimal values, depending on the language. You will mostly work with integers, but some values need decimals.
  • Strings are ordered sequences of characters, usually written between quotation marks. Anything in quotation marks counts as a string, even digits.
  • Lists are ordered sequences of elements. A list lets you treat many related items as a single value, and it can hold numbers, strings, or both. Lists are also called arrays in some languages.
  • Booleans represent only two values: true or false.
</>Code
intExample ← 5
floatExample ← 5.52
stringExample ← "Fiveable"
numberString ← "123456789"
listExample ← ["Fish", "fish", "fish"]
numList ← [2, 4, 6, 8]
booleanValue ← true

The type of a value affects what you can do with it. For example, a string that looks like a number is still a string, so arithmetic on it does not behave like arithmetic on actual numbers. That is why some values are better represented as one type than another. A phone number you never do math on might be a string, while a quantity you add or multiply should be a number.

Remember that a single variable holds one value at a time, but that value can itself contain multiple values. Storing a list in a variable is the standard example: the variable holds one list, and the list holds many elements.

Naming Variables Clearly

Use meaningful, descriptive names for your variables. Clear names act like built-in documentation and make your code easier to read and explain, which matters for both tracing and for the written responses about your own program.

In very short programs you will often see single letters, and that is fine. In longer programs with many variables, descriptive names save you from confusion.

Spelling and capitalization matter. These are treated as two different variables:

</>Code
number ← 7
Number ← 7

Watch out for typos, because a misspelled variable name is a common and frustrating bug.

How to Use This on the AP Computer Science Principles Exam

Code Tracing

  • Read every line in order and write the current value of each variable to the side.
  • On each assignment line, update only the variable on the left of .
  • For a chain like b ← a, copy the value a has right now. Later changes to a do not update b.
  • For "what does this display" questions, find the value of the displayed variable at the exact line the display runs.

Create Performance Task

  • Choose descriptive variable names so your code is readable and easy to describe in your written responses.
  • Pick a data type that fits the value, such as a list when you need to store multiple related items together.

Common Trap

  • Assuming b ← a creates a permanent link between a and b. It does not; it copies the current value.

Common Misconceptions

  • "A variable can hold many values at once." A variable holds one value at a time. That one value can be a list or other collection that contains multiple values inside it.
  • "Assigning one variable to another links them." Assignment copies the current value. Changing the source later does not change the variable that received the copy.
  • "The number stored is whatever it started as." The value is always the most recent one assigned, so reassignment overwrites the old value.
  • "A string of digits is a number." Text in quotation marks is a string, even if it looks like a number, and it does not behave like a numeric value in arithmetic.
  • "Capitalization in names does not matter." number and Number are different variables, and typos in names cause bugs.

Vocabulary

The following words are mentioned explicitly in the College Board Course and Exam Description for this topic.

Term

Definition

assignment operator

A symbol or operator that allows a program to assign a value to a variable, changing what the variable represents.

Boolean

A data type that represents one of two values: true or false.

collection

A data structure that contains multiple values grouped together and can be represented by a single variable.

data storage

The memory location within a program where a variable's value is stored.

data type

A classification that specifies what kind of value a variable can hold, such as numbers, Booleans, lists, or strings.

expression

A combination of a value, variable, operator, or procedure call that can be evaluated to produce a single value.

list

An ordered sequence of elements, where each element is assigned a unique index for reference.

number

A data type that represents numeric values used in calculations and comparisons.

string

An ordered sequence of characters.

variable

A named container in a program that stores a value which can be changed through assignment.

Frequently Asked Questions

What is AP CSP 3.1 about?

AP Computer Science Principles 3.1 is about variables and assignments. You learn how variables represent values, how assignment changes the value stored in a variable, and how to trace the most recent value assigned.

What is a variable in AP Computer Science Principles?

A variable is an abstraction that stores one value at a time. That value can be a number, Boolean, string, list, or another collection, but the variable itself represents the current stored value.

What does the left-arrow operator mean in AP CSP?

The left-arrow operator, written as `←`, is assignment on the AP CSP reference sheet. In `a ← expression`, the expression is evaluated first, then a copy of the result is stored in `a`.

What happens when one variable is assigned to another?

When one variable is assigned to another, the current value is copied. If `b ← a` runs and `a` later changes, `b` keeps the value that was copied at the moment of assignment.

What data types should I know for AP CSP variables?

The main AP CSP data types are numbers, Booleans, strings, and lists. Some values fit one type better than another, such as using a string for text and a number for values you need to calculate with.

How should I trace variables on the AP CSP exam?

Read each line in order, update only the variable on the left side of the assignment, and record the most recent value. For display questions, use the value the variable has at the exact line where it is displayed.

Pep mascot
Upgrade your Fiveable account to print any study guide

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Click below to go to billing portal → update your plan → choose Yearly→ and select "Fiveable Share Plan". Only pay the difference

Plan is open to all students, teachers, parents, etc
Pep mascot
Upgrade your Fiveable account to export vocabulary

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Plan is open to all students, teachers, parents, etc
report an error
description

screenshots help us find and fix the issue faster (optional)

add screenshot