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:

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) 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.

Overview and Notes: 3.8 - Iteration

Add your OWN Notes for 3.8 here:

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.

import random
import os

questions = [
    "What is the college board syntax for accessing an element of aList at index i?",
    "What is the college board syntax for assigning an element of aList to variable 'x'?",
    "What is the college board syntax for assigning the value of a variable 'x' to the element of aList at index i?",
    "What is the college board syntax for assigning the value of aList[j] to aList [i]?",
    "What is the college board syntax for placing a value at index i in aList?",
    "What is the college board syntax for adding a value to the end of aList?",
    "What is the college board syntax for removing a value from aList at index i?",
    "What are the two different types of loops?",
    "Recursive loops involve [   ?   ]."
]
answers = [
    "aList[i]",
    "x <- aList[i]",
    "aList[i] <- x",
    "aList[i] <- aList[j]",
    "INSERT(aList, i, value)",
    "APPEND(aList, value)",
    "REMOVE(aList, i)",
    "For and While",
    "incrementing a variable until it reaches a certain break point"
]

def questionloop():
    index=0
    totalCorrect = 0
    while index < len(questions):
        numbers=random.sample(range(0, 9), 4)
        question = questions[index]
        print(question)
        answer_place=random.randint(0,3)
        if answer_place == 0:
            print(f"""
A: {answers[index]}
B: {answers[numbers[1]]}
C: {answers[numbers[2]]}
D: {answers[numbers[3]]}
""")
            answer = input()
            if answer.lower() == "a":
                print("correct!")
                totalCorrect += 1
            else:
                print("wrong")
            os.system("clear")
        elif answer_place == 1:
            print(f"""
A: {answers[numbers[0]]}
B: {answers[index]}
C: {answers[numbers[2]]}
D: {answers[numbers[3]]}
""")
            answer = input()
            if answer.lower() == "b":
                print("correct!")
                totalCorrect += 1
            else:
                print("wrong")
            os.system("clear")
        elif answer_place == 2:
            print(f"""
A: {answers[numbers[0]]}
B: {answers[numbers[1]]}
C: {answers[index]}
D: {answers[numbers[3]]}
""")
            answer = input()
            if answer.lower() == "c":
                print("correct!")
                totalCorrect += 1
            else:
                print("wrong")
            os.system("clear")
        elif answer_place == 3:
            print(f"""
A: {answers[numbers[0]]}
B: {answers[numbers[1]]}
C: {answers[numbers[2]]}
D: {answers[index]}
""")
            answer = input()
            if answer.lower() == "d":
                print("correct!")
                totalCorrect += 1
            else:
                print("wrong")
            os.system("clear")
        index += 1
    return totalCorrect

total=questionloop()
print(f"You got: {total}/{len(questions)}")
What is the college board syntax for accessing an element of aList at index i?

A: aList[i]
B: INSERT(aList, i, value)
C: aList[i] <- x
D: For and While

correct!
What is the college board syntax for assigning an element of aList to variable 'x'?

A: x <- aList[i]
B: INSERT(aList, i, value)
C: For and While
D: incrementing a variable until it reaches a certain break point

correct!
What is the college board syntax for assigning the value of a variable 'x' to the element of aList at index i?

A: aList[i] <- x
B: incrementing a variable until it reaches a certain break point
C: x <- aList[i]
D: REMOVE(aList, i)

correct!
What is the college board syntax for assigning the value of aList[j] to aList [i]?

A: aList[i] <- aList[j]
B: INSERT(aList, i, value)
C: APPEND(aList, value)
D: x <- aList[i]

correct!
What is the college board syntax for placing a value at index i in aList?

A: For and While
B: aList[i] <- aList[j]
C: INSERT(aList, i, value)
D: INSERT(aList, i, value)

correct!
What is the college board syntax for adding a value to the end of aList?

A: aList[i] <- x
B: INSERT(aList, i, value)
C: APPEND(aList, value)
D: aList[i] <- aList[j]

wrong
What is the college board syntax for removing a value from aList at index i?

A: For and While
B: aList[i]
C: REMOVE(aList, i)
D: incrementing a variable until it reaches a certain break point

wrong
What are the two different types of loops?

A: incrementing a variable until it reaches a certain break point
B: aList[i] <- aList[j]
C: For and While
D: APPEND(aList, value)

wrong
Recursive loops involve [   ?   ].

A: incrementing a variable until it reaches a certain break point
B: APPEND(aList, value)
C: aList[i] <- aList[j]
D: incrementing a variable until it reaches a certain break point

wrong
You got: 5/9

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")

# Assign the element at the end of the list to index 2. Print index 2 to check
grocery_list[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']
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"
]

def binary_convert(binary):
    power = 0
    decimal = 0
    while power < 8:
        position = 7-power
        if binary[position] == "1":
            decimal += 2**power
        power += 1
    return decimal

#when done, print the results
decimallist = []
for binary in binarylist:
    decimal = binary_convert(binary)
    if decimal <= 100:
        decimallist.append(decimal)

print(decimallist)
[73, 55]