Variables and Assignments

David Foster
5 min read
Listen to this study note
Study Guide Overview
This study guide covers variables and data types in programming. It explains what variables are, including assignment and reassignment, and how to track their values. It also describes different data types such as integers, floats, strings, lists, and booleans.
#AP Computer Science Principles: Variables and Data Types - Your Last-Minute Guide π
Hey! Let's get you ready to ace this exam. We're going to break down variables and data types so everything is crystal clear. Think of this as your cheat sheet for tonight!
#Variables: The Heart of Your Program
#What is a Variable?
- A variable is like a container that holds a value. It's a named storage location in the computer's memory. Think of it like a box with a label on it. π¦
- You use variables to store and manipulate data in your programs.
- They are represented by names (usually letters or words).
#Assignment and Reassignment
- Assignment: Giving a variable a value for the first time. In pseudocode, we use
β
. In Python, we use=
. - Reassignment: Changing the value of a variable. The variable will always hold the most recent value assigned to it.
# Example in Python
number = 7 # Assignment
changed_number = number # changed_number is now 7
number = 17 # Reassignment
print (changed_number) # Output: 7
The key is that changed_number
was assigned the value of number
before number
was changed to 17.
#Keeping Track of Variable Values
- Use extra
print
statements to check values during execution. - Hand tracing can help reduce uncertainty (remember Big Idea 1!).
#Data Types: What Kind of Data Is It?
#What are Data Types?
- Data types are categories that classify the type of value a variable can hold. ποΈ
- Common data types include integers, floats, strings, lists (arrays), and booleans.
- The data type determines what operations you can perform on the data.
#Numerical Data
- Integers (
int
): Whole numbers (positive or negative). Example:5
,-10
,0
. - Floats (
float
): Numbers with decimal points. Example:3.14
,-2.5
,0.0
.
python
int_example = 5
float_example = 5.52
#Strings
- Strings (
str
): Sequences of characters, enclosed in quotation marks. - Can be letters, words, numbers, or symbols. Example:
"Hello"
- Use single or double quotes.
python
string_example = "Hello, World!"
#Booleans
- Booleans (
bool
): Represents truth values:True
orFalse
. - Used for conditional logic and decision-making.
python
bool_example = True
#Lists (Arrays)
- Lists (
list
): Ordered collections of items. Can contain elements of different data types. - Enclosed in square brackets
[]
.
python
list_example = [1, 2, "three", 4.0]
#Key Concepts
- Variables are named storage locations.
- Data types classify the kind of values variables can hold.
- Common data types:
int
,float
,str
,bool
,list
.
Practice Question
#Practice Questions
-
What is the correct way to assign the value 10 to a variable named
count
in Python? a) count = 10 b) 10 = count c) count β 10 d) set count to 10 -
Which data type would you use to store the value
3.14
? a) int b) str c) float d) bool -
What is the value of
x
after the following code is executed?
python
x = 5
x = x + 2
a) 5
b) 2
c) 7
d) Error
4. Which of the following is a valid string? a) 123 b) True c) "Hello" d) [1, 2, 3]
- What is the purpose of a boolean data type? a) To store numbers. b) To store text. c) To store truth values. d) To store lists.
#Short Answer Questions
- Explain the difference between assignment and reassignment of a variable.
- Describe the different data types and their uses in programming.
#Memory Aid
Remember the acronym V-I-S-B-L:
- Variables: Named storage locations.
- Int: Whole numbers.
- String: Text in quotes.
- Bool: True or False.
- List: Ordered collection.
#
Final Tip
- Go through the practice questions again. You got this!
Explore more resources

How are we doing?
Give us your feedback and let us know how we can improve