Fiveable
Fiveable

or

Log in

Find what you need to study


Light

Find what you need to study

3.1 Variables and Assignments

3 min readdecember 22, 2022

Minna Chow

Minna Chow

Milo Chang

Milo Chang

Minna Chow

Minna Chow

Milo Chang

Milo Chang

A 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 ←.

https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-c0SscSxFs0nI.png?alt=media&token=ad1d0cc4-24c4-4402-aa5e-8ec1b24a2dca

If you want to change the value the 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 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 changed_number was assigned to the value of the 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

are different categories of data that your computer can represent. Examples of are integers, strings, , and .

Numerical data is represented in several different ways. The numerical types in Python are known as , , (and complex). You'll mainly be working with integers in AP CSP. Integers can only be positive or negative. You might come across , 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 of characters. In Python, strings are represented by . You'll usually see them in the format of letters or words. However, strings can also be numerical; anything between is considered a string.

"Hello world!"
"Fiveable"
"123456789"
"y + x"

A list is an ordered sequence of elements. They're also known as . 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 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 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 .

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 

Key Terms to Review (11)

Arrays

: Arrays are a data structure that stores a fixed-size sequence of elements of the same type. They allow for efficient storage and retrieval of multiple values using a single variable.

Arrow Symbol

: The arrow symbol, also known as the assignment operator, is used in programming to assign a value to a variable. It indicates that the value on the right side of the arrow should be stored in the variable on the left side.

Boolean value

: A Boolean value is a data type that can only have two possible values: true or false. It is often used in programming to make decisions and control the flow of a program.

Booleans

: Booleans represent one of two possible values - True or False. They are often used for logical operations and decision-making in programming.

Complex Numbers

: Complex numbers are mathematical entities that consist of both real and imaginary parts. They are often used in scientific and engineering calculations.

Data Types

: Data types refer to the different categories or classifications of data that can be stored and manipulated in a computer program. Each data type has specific characteristics and operations associated with it.

Float

: A float is a data type in programming that represents decimal numbers. It can store both whole and fractional values.

int

: Int (short for integer) is a data type that represents whole numbers without any decimal points. It includes both positive and negative numbers, as well as zero.

Lists

: Lists are ordered collections of items in computer programming. They allow you to store multiple values under one variable name and access them using their position or index.

Quotation Marks

: Quotation marks are punctuation marks used to indicate direct speech or quotations within a text. They enclose words or phrases spoken by someone else or taken from another source.

Variable

: A variable is a named storage location in computer memory that holds a value which can change during program execution. It allows programmers to store and manipulate different types of data throughout their code.

3.1 Variables and Assignments

3 min readdecember 22, 2022

Minna Chow

Minna Chow

Milo Chang

Milo Chang

Minna Chow

Minna Chow

Milo Chang

Milo Chang

A 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 ←.

https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-c0SscSxFs0nI.png?alt=media&token=ad1d0cc4-24c4-4402-aa5e-8ec1b24a2dca

If you want to change the value the 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 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 changed_number was assigned to the value of the 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

are different categories of data that your computer can represent. Examples of are integers, strings, , and .

Numerical data is represented in several different ways. The numerical types in Python are known as , , (and complex). You'll mainly be working with integers in AP CSP. Integers can only be positive or negative. You might come across , 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 of characters. In Python, strings are represented by . You'll usually see them in the format of letters or words. However, strings can also be numerical; anything between is considered a string.

"Hello world!"
"Fiveable"
"123456789"
"y + x"

A list is an ordered sequence of elements. They're also known as . 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 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 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 .

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 

Key Terms to Review (11)

Arrays

: Arrays are a data structure that stores a fixed-size sequence of elements of the same type. They allow for efficient storage and retrieval of multiple values using a single variable.

Arrow Symbol

: The arrow symbol, also known as the assignment operator, is used in programming to assign a value to a variable. It indicates that the value on the right side of the arrow should be stored in the variable on the left side.

Boolean value

: A Boolean value is a data type that can only have two possible values: true or false. It is often used in programming to make decisions and control the flow of a program.

Booleans

: Booleans represent one of two possible values - True or False. They are often used for logical operations and decision-making in programming.

Complex Numbers

: Complex numbers are mathematical entities that consist of both real and imaginary parts. They are often used in scientific and engineering calculations.

Data Types

: Data types refer to the different categories or classifications of data that can be stored and manipulated in a computer program. Each data type has specific characteristics and operations associated with it.

Float

: A float is a data type in programming that represents decimal numbers. It can store both whole and fractional values.

int

: Int (short for integer) is a data type that represents whole numbers without any decimal points. It includes both positive and negative numbers, as well as zero.

Lists

: Lists are ordered collections of items in computer programming. They allow you to store multiple values under one variable name and access them using their position or index.

Quotation Marks

: Quotation marks are punctuation marks used to indicate direct speech or quotations within a text. They enclose words or phrases spoken by someone else or taken from another source.

Variable

: A variable is a named storage location in computer memory that holds a value which can change during program execution. It allows programmers to store and manipulate different types of data throughout their code.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.