Lesson 3.1-3.2 - Variables, Assignments, and Data Abstractions
Notes, questions, and hacks
Notes
- A variable is an abstraction that holds values
- The data in these values can be Booleans, numbers, lists, and strings
- Strings are series of characters
- Integers are number values
- Booleans are true/false statements
- When choosing variables, it is important to assign the variables name to something that correlates with what the function of the variable is supposed to do
- An assignment operator stores a value inside a variable
- Lists are sequences of elements with each element being a variable
- The elements within the list can be accessed by index
- Indexes can efficiently print an item from the list
- Data abstraction can be created with lists
- Lists bundle together multiple elements and/or variables under one name are not defined with specified lengths
- Use square brackets “[]” to store the values of a certain variable, then you can simply print the variable to output your desired list
- Lists manage the complexity of a program by creating a more efficient and coherent code segment
name = "Annika" # this is a string variable
age = 15 # this is an integer variable
city = "San Diego"
print("Hi, my name is " + name + f". I am {age}" + ", and I live in " + city)
# use f'...{}' to integrate an integer variable into the string
#2 Questions
- In your own words, briefly explain by writing down what an assignment operator is
- An assignment operator is a symbol used to store a value inside a variable, such as an equal sign (=).
- In Collegeboard pseudocode, what symbol is used to assign values to variables?
- An arrow symbol (<--).
- A variable, x, is initially given a value of 15. Later on, the value for x is changed to 22. If you print x, would the command display 15 or 22?
- 22, because it comes after and therefore replaces it.
#3 Questions
- What is a list?
- A list is a sequence of elements, each of which is a variable.
- What is an element?
- An element is a single fragment of the code.
- What is an easy way to reference the elements in a list or string?
- An index can return the element of a list or string by the position it is at.
- What is an example of a string?
- A series of characters, i.e., a name, a phone number, or a phrase.
#4 List index
favFoods = ['udon', 'penne rosa', 'spam musubi', 'avocado toast', 'brownies']
# this is a list of elements, all of which are string variables
print(favFoods[3]) # this indexes and prints the third item of my list
print(favFoods[-3]) # this indexes and prints the third from the end of my list
num1 = input("Input a number. ")
num2 = input("Input a number. ")
num3 = input("Input a number. ")
add = input("How much would you like to add? ")
# Add code in the space below
numlist = [int(num1), int(num2), int(num3)]
# The following is the code that adds the inputted addend to the other numbers. It is hidden from the user.
for i in numlist:
numlist[i-1] += int(add)
print(list(numlist))
food1 = "pizza"
food2 = "hot dog"
food3 = "sushi"
food4 = "strawberry"
food5 = "sandwich"
print(food1, food2, food3, food4, food5)
# simplifying the list
food = ['pizza', 'hot dog', 'sushi', 'strawberry', 'sandwich']
print(food)
#8 Question
Why are using lists better for a program, rather than writing out each line of code?
- Writing out each line of code for each variable is tedious and impractical. Using a list manages this complexity, creating a more efficient and coherent code segment. It also reduces the amount of variables used, simplifying the code and making it easier to work on.
#9 Managing a complex list
color1 = 'red'
color2 = 'orange'
color3 = 'yellow'
color4 = 'green'
color5 = 'blue'
color6 = 'purple'
print(color1, color2, color3, color4, color5, color6)
# this is the "long and slow way" to write out a list, with separate variables for each item
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple']
print(colors)
# I have managed the complexity by placing the items all in one list