Score of 1/1

Vocab:

  • Lists: collections of data
  • Indexes: Most code languages count starting with zero, and this is also the case for list indexes
  • Iteration: repetition

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:

  • Items in lists are located with indexes, which, in most code languages, start with 0 (though the pseudocode used by CollegeBoard usually starts at 1!).
  • Lists are incredibly helpful because you can use them to store unlimited amounts of data, and using loops and functions that locate list data using indexes, the information can be procedurally used in a way that suits a certain purpose.

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:

  • Iteration is the repetition of a function. Allowing a function to repeat on its own based on various conditions can vastly optimize a program.
  • Iteration is most often done with loops, typically in combination with lists and/or dictionaries.
  • Conditional iteration continues repeating until a condition is met
  • Nested loops or repeated loops are also useful

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

print("Welcome to the quiz! For your answers, please enter in 'A', 'B', or 'C'! Thanks!")

def question(prompt, answer):
    msg = input("Question: " + prompt)
    if msg == answer:
        print(msg + " is correct!")
        return(1)
    else:
        print(msg + " is incorrect!")
        return(0)

# Append to List a Dictionary of key/values related to a person and cars
quiz = {
    "Which of the following python codes accesses the element from a list at index i? (A) aList[i] (B) aList(i) (C) aList[[i]]": "A",
    "What are lists? (A) my grocery list (B) a useful feature in only the Python language (C) collections of data": "C",
    "What indexes do lists start at in programming languages? (A) -1 (B) 0 (C) 1": "B",
    "What indexes do lists in Collegeboard start at? (A) -1 (B) 0 (C) 1": "C",
    "What is iteration? (A) When the teacher talks about something twice (B) repetition (C) a for loop": "B",
    "When is a good time to use a recursive loop? (A) to limit the starting point (B) to be fancy (C) if you want a while loop": "A"
}

numqs = 6
correct = 0

for item in quiz:
    num = question(item, quiz[item])
    correct = correct + num

print("You scored" + str(correct) +"/" + str(numqs))
print("Your percentage is " + str(correct/numqs*100) + "%! Good work!")
if (correct >= 5):
    print("You passed!")
else:
    print("You failed!")
Welcome to the quiz! For your answers, please enter in 'A', 'B', or 'C'! Thanks!
A is correct!
C is correct!
B is correct!
C is correct!
B is correct!
A is correct!
You scored6/6
Your percentage is 100.0%! Good work!
You passed!

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
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 the entire list, does it match ours ? 
print(grocery_list)

# Expected output
# carrots
# carrots
# artichokes
# ['apples', 'eggs', 'artichokes', 'carrots', 'cucumbers', 'umbrellas', 'artichokes']
carrots
['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):
    return(int(binary,2))

for i in range(0,7):
     binarylist[i] = binary_convert(binarylist[i])

finalList = []

for i in range(0,7):
    if (binarylist[i] <= 100):
        finalList.append(binarylist[i])

print(finalList)
[73, 55]