All Subjects



AP Comp Sci P



Big Idea 3: Algorithms and Programming

Variables and Assignments

3 min read•november 16, 2020


Minna Chow

AP Computer Science Principles ⌨️

Bookmarked 2.3k • 69 resources
See Units

A variable is a placeholder in your program for a value, just like in math. Variables are usually represented by letters or words.

Variables in Pseudocode and Python

You can assign values to variables and also change these values through the assignment operator.
The College Board Pseudocode uses the arrow ←.
If you want to change the value the variable holds, you just have to reassign it.
In Python, the = symbol is used to assign values to variables.
a = expression
For all programming languages, the value that a variable has is the most recent one it's been assigned. Take this example (from the College Board's CED):
number = 7 changed_number = number number = 17 print (changed_number)
In this case, the print statement would return 7 because the variable changed_number was assigned to the value of the variable number when number still equaled 7.
It can be difficult to keep track of what values your variables have, especially if you're changing them around a lot. Using extra print statements and hand tracing (from Big Idea 1) can help reduce this uncertainty.

Data Types

Data types are different categories of data that your computer can represent. Examples of data types are integers, strings, lists and booleans.
Numerical data is represented in several different ways. The numerical types in Python are known as int, float, (and complex.) You'll mainly be working with integers in AP CSP. Integers can only be positive or negative. You might come across float, or floating point numbers, as well: floating point numbers allow for decimal values to be represented.
int_example = 5 float_example = 5.52
You can think of strings as lists of characters. In Python, strings are represented by quotation marks. You'll usually see them in the format of letters or words. However, strings can also be numerical; anything between quotation marks is considered a string.
"Hello world!" "Fiveable" "123456789" "y + x"
A list is an ordered sequence of elements. They're also known as arrays. They allow you to treat multiple items as a single value, and can contain both words and numbers.
list_example = [Fish, fish, fish] num_list_example = [2, 4, 6, 8]
Finally, the Boolean data type only represents two values: true or false.
Boolean_value = True
The data type your variable is categorized under will determine what you can do to it. For example, you can't do math on strings, even if the string has a number in it.
Here's an example in Python:
y = 25 x = 5 print (y / x) #prints a value of 5 y = "25" x = "5" print (y / x) #returns an error/doesn't work
Because of this, some values are better represented as one type of data than another.
Each variable can only hold one data value at a time, but that value itself could have multiple values within it. For example, you can store multiple values in a list, and then you can assign that list to the variable.
When naming your variables, it's important to use meaningful, descriptive names. Variables with clear names are like good documentation (mentioned in Big Idea 1); they help make your code easy to understand.
This isn't as vital in shorter programs, where you'll often see variables represented as just letters, but it's good practice for when you have to write longer programs with more lines to keep track of.
It's important to note that when naming variables, spelling and capitalization matter.
number = 7 #Is a different variable from Number = 7 #and beware of typos!

Was this guide helpful?

Join us on Discord
Thousands of students are studying with us for the AP Computer Science Principles exam.
join now
Hours Logo
Studying with Hours = the ultimate focus mode
Start a free study session
🔍 Are you ready for college apps?
Take this quiz and find out!
Start Quiz
Browse Study Guides By Unit
🕹Big Idea 1: Creative Development
⚙️Big Idea 2: Data
📱Big Idea 3: Algorithms and Programming
🖥Big Idea 4: Computer Systems and Networks
⌨️Big Idea 5: Impact of Computing
📝Exam Prep
Join us on Discord
Thousands of students are studying with us for the AP Computer Science Principles exam.
join now
💪🏽 Are you ready for the AP CSP exam?
Take this quiz for a progress check on what you’ve learned this year and get a personalized study plan to grab that 5!