3.1-3.2 Hacks
lists, index, variables
simplified food list:
lists are better because it makes your code sorter and more efficient
foods = ['pizza, hotdog, sushi, strawberry, sandwich']
print(foods)
color1 = "red"
color2 = "orange"
color3 = "yellow"
color4 = "green"
color5 = "blue"
color6 = "indigo"
color7 = "violet"
print(color1, color2, color3, color4, color5, color6, color7)
color = ['red, orange, yellow, green, blue, indigo, violet']
print(color)
num1=input("Input first number. ")
num2=input("Input second number. ")
num3=input("Input third number. ")
add=input("How much would you like to add to each number? ")
# Add code in the space below
num1 = int(num1)
num2 = int(num2)
num3 = int(num3)
numlist = [num1 , num2, 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(numlist)
- Create a list with indices
- Index a part of the list that you created.
- Try to index from the end ### index dog from front and end
petsList = ["cat", "dog", "fish", "bird"]
print(petsList)
print(petsList[1])
print(petsList[-3])
What is a list?
- A list of values or variables that can be assigned all at once
What is an element
- the things in a list
What is an easy way to reference the elements in a list or string?
- print(name of list)
What is an example of a string?
- hello
Create an index of your favorite foods
favFood = ["sushi", "pasta", "strawberries", "tiramisu", "potato"]
print(favFood)
in1 = input("Input number")
print(favFood[int(in1)-1])
In your own words, briefly explain by writing down what an assignment operator is
- an operator that assigns values to variables
In Collegeboard pseudocode, what symbol is used to assign values to variables?
- -> an arrow
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?
- if you print x it would display 22 because code is read from top to bottom and meaning if the variable is reassigned later on in the code, the reassigned value will be the one that is displayed
print("name:")
name = input("what's your name?")
print("my name is", name)
print("age:")
age = input("how old are you?")
print("I am", age, "years old")
print("phone number:")
phoneNumber = input("what's your phone number")
print("my phone number is", phoneNumber)