Lists and Iteration Homework
Try to complete this to show understanding! Copy this into your notebook so you can also take notes as we lecture. Make a copy of this notebook to your own repository to take notes, do the class challenges, and to access the homework.
Overview and Notes: 3.10 - Lists
- Make sure you complete the challenge in the challenges section while we present the lesson!
Add your OWN Notes for 3.10 here:
- Lists can be used to store data
- Indexes count an organize data in a list
- Iteration is repeating a function multiple times
Fill out the empty boxes:
Pseudocode Operation | Python Syntax | Description |
---|---|---|
alist[i] | aList[i] | Accesses the element of aList at index i |
x ← aList[i] | x = alist[i] | Assigns the element of aList at index i to a variable 'x' |
aList[i] <- x | aList(i) = x | Assigns the value of a variable 'x' to the element of a List at index i |
aList[i] ← aList[j] | aList[i] = aList[j] | Assigns value of aList[j] to aList[i] |
INSERT(aList,i,value) | aList.insert(i, value) | value is placed at index i in aList. Any element at an index greater than i will shift one position to the right. |
APPEND(aList, value) | aList.append(value) | Replaces the element at index i with value |
REMOVE(aList,i) | aList.pop(i) OR aList.remove(value) |
Removes item at index i and any values at indices greater than i shift to the left. Length of aList decreased by 1. |
Homework Assignment
Instead of us making a quiz for you to take, we would like YOU to make a quiz about the material we reviewed.
We would like you to input questions into a list, and use some sort of iterative system to print the questions, detect an input, and determine if you answered correctly. There should be at least five questions, each with at least three possible answers.
You may use the template below as a framework for this assignment.
def quiz():
points = 0
questions = ["How do you remove an element from a list", "How do you add to a list", "How do you insert an element into a list", "How do you call an index to a list","How do you have a variable equal to a part of the list?"]
answers = ["list.remove", "list.append", "list.insert", "list[i]", "x = list[i]"]
num = 0
while num <= 4:
answer = input(questions[num])
if answer == answers[num]:
print("Correct")
points = points + 1
else:
print("Incorrect")
num += 1
print("You got:", (points/5)*100, "%")
quiz()
#make a function to check if the answer was correct or not
Hacks
Here are some ideas of things you can do to make your program even cooler. Doing these will raise your grade if done correctly.
- Add more than five questions with more than three answer choices
- Randomize the order in which questions/answers are output
- At the end, display the user's score and determine whether or not they passed
Challenges
Important! You don't have to complete these challenges completely perfectly, but you will be marked down if you don't show evidence of at least having tried these challenges in the time we gave during the lesson.
3.10 Challenge
Follow the instructions in the code comments.
grocery_list = ['apples', 'milk', 'oranges', 'carrots', 'cucumbers']
# Print the fourth item in the list
print(grocery_list[3])
# Now, assign the fourth item in the list to a variable, x and then print the variable
x = grocery_list[3]
print(x)
# Add these two items at the end of the list : umbrellas and artichokes
grocery_list.append("umbrellas")
grocery_list.append("artichokes")
# Insert the item eggs as the third item of the list
grocery_list.insert(3, "eggs")
# Remove milk from the list
grocery_list.remove('milk')
# Assign the element at the end of the list to index 2. Print index 2 to check
grocery_list.insert(2,grocery_list[-1])
print(grocery_list[2])
# Print the entire list, does it match ours ?
print(grocery_list)
# Expected output
# carrots
# carrots
# artichokes
# ['apples', 'eggs', 'artichokes', 'carrots', 'cucumbers', 'umbrellas', 'artichokes']
3.8 Challenge
Create a loop that converts 8-bit binary values from the provided list into decimal numbers. Then, after the value is determined, remove all the values greater than 100 from the list using a list-related function you've been taught before. Print the new list when done.
Once you've done this with one of the types of loops discussed in this lesson, create a function that does the same thing with a different type of loop.
Struggle: I struggled with challenge 3.8. I was very confused on how to convert the binary to decimal because I didn't know how to move through the array and multiply the numbers by the power they needed to be multiplied by. I was able to convert one of the binary numbers too decimal however I was unable to do the rest. The end of the code shows how I checked if the output was greater than 100 and how it would be removed if it was.
binarylist = [
"01001001", "10101010", "10010110", "00110111", "11101100", "11010001", "10000001"
]
decimallist = []
for binary in binarylist:
print(binary)
def binary_convert(binarylist):
decimal = 0
for digit in binary:
decimal = decimal*2 + int(digit)
print("The decimal value is:", decimal)
decimallist.append(decimal)
binary_convert(binarylist)
for i in decimallist:
if i > 100:
decimallist.remove(i)
print(decimallist)
#use this function to convert every binary value in binarylist to decimal
#afterward, get rid of the values that are greater than 100 in decimal
#when done, print the results