Unit 3 Sections 8 and 10 Work
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: used to collect and store data unlimited amounts of data
- can use loops and funcions that locate lists; use indexes and procedurally use it
- syntax: list = ["item0", "item1", "item2"]
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] | aList[i] = x | Assigns the element of aList at index i to a variable 'x' |
INSERT(aList, i, value) | 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) | value is added as an element to the end of aList and length of aList is increased by 1 |
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.
questions = [
#questions go here (remember to make them strings!)
"1. In the for loop, for i in range(9), what is final value of i?",
"2. What does the list method pop do?",
"3. What does the and operator do?",
"4. When is the base used when converting binary to decimal?",
"5. What are loops that involve incrementing a variable till a breaking point?"
]
answerOptions = [ #Options to choose from for each question above
("A. 10", "B. 9", "C. 8"),
("A. Add an element to the list", "B. Replace an element in the list", "C. Remove an element from the list"),
("A. Adds two values", "B. Returns True if both values are same", "C. Returns False if both values are same"),
("A. 2", "B. 10", "C. 8"),
("A. while", "B. recursive", "C. Binary")
]
# the real answers
answers = ["C", "C", "B", "A", "B"]
def questionloop():
score = 0
for i in range(len(questions) - 1):
print(questions[i]) #print the question
print(answerOptions[i]) #print the possible options
ans = input('Choose answer from the options: ') #get input from user
if(answercheck(i, ans) == True): #pass the index of the question and the user's answer to our function
print("Correct answer!")
score += 1 #increment the score if answer is correct
else:
print("Wrong answer!")
print("Your score: ", end="")
print(score) #print the final score
#check the answer by comparing it with the answer stored in answers list
def answercheck(index, answer):
if (answers[index] == answer):
return True
else:
return False
#start our questionnaire
questionloop()
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(2, "eggs")
# Remove milk from the list
grocery_list.remove("milk") # or can use .pop
# Assign the element at the end of the list to index 2. Print index 2 to check
grocery_list[2] = "artichokes"
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.
binarylist = [
"01001001", "10101010", "10010110", "00110111", "11101100", "11010001", "10000001"
]
print ("Original List: ", end="")
print (binarylist)
#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
def binary_convert(binary):
binLen = 8 # length of each 8-bit string is fixed to 8
dec = 0 # used to store our converted decimal value
# we want to extract the bits from right to left
for i in range(binLen): # replace with "while i < binLen" if you want to use while loop
dec += int(binary[binLen-i-1]) * 2**i #convert binary digit to decimal and add to our variable
#uncomment if using the while loop version
# i += 1
return dec #return the final converted value
# Make a new list while iterating thru the original list
# This new list will store only those values that are less than or equal 100
# In the end, replace the original list with this new list
newBinaryList = []
for bin in binarylist:
if (binary_convert(bin)) <= 100:
newBinaryList.append(bin)
binarylist = newBinaryList #replace original list
print("Final List: ", end="")
print(binarylist)
#when done, print the results