Unit 3.1 & 3.2 Hacks
drink = "water"
volume = 20
print("There are",volume,"liters of", drink)
Unit 3.1.2 Hacks
- In your own words, briefly explain by writing down what an assignment operator is
- An assignment operator is an equal sign that assigns a value to a variable
- 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?
- It would display 22
-
Bonus: multiple ways to define a variable
var age = 1;
const b = 3
Unit 3.2.1 Hacks
Questions
- What is a list? A list is a sequence of elements with each element being a variable
- What is an element An element is one of the things in a list
- What is an easy way to reference the elements in a list or string? An easy way to reference elements in a list or string is by using index
- What is an example of a string? A string is a series of characters, for example a person's name
Create an index of your favorite foods Tips: Index starts at 1, Strings are ordered sequences of characters
Extra work: Try to create an index that lists your favorite food and print the element at index 3. More work: Create a list of your favorite foods and create an index to access them.
marks = ["food1"]
favFoods = ["pizza","steak","ribs","hamburger","fried chicken"]
print(favFoods[3])
print(favFoods[0])
print(favFoods[4])
print(favFoods[-1])
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)]
print("Original numbers:",numlist)
print("Adding:",add)
# The following is the code that adds the inputted addend to the other numbers. It is hidden from the user.
for i in range(len(numlist)):
numlist[i-1] += int(add)
print("New numbers:",numlist)
Unit 3.2.3 Hacks
On a single markdown file:
-
Insert a screenshot of your score on the python quiz
-
Insert a screenshot of your simplifying of the food list
-
Why are using lists better for a program, rather than writing out each line of code?
- Using lists are better for a program because it reduces redundancy and makes the program more organized and simpler. Also the code is easier to read and faster to write.
- Make your own list the “long and slow way” then manage the complexity of the list
- Long and Slow Way
fruit1 = "apple"
fruit2 = "banana"
fruit3 = "orange"
fruit4 = "mango"
fruit5 = "kiwi"
fruit6 = "lemon"
fruit7 = "lime"
print(fruit1, fruit2, fruit3, fruit4, fruit5, fruit6, fruit7)
Better Way
fruits = ["apple", "banana", "orange", "mango", "kiwi", "lemon", "lime"]
print(fruits)