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:

  1. Lists are a collection of related data and are incredibly useful in storing large amounts of data.
  2. lists have indexes which represents the location of an element in the list. Most programming languages use 0-based indexing, but others could use 1-based indexing. FYI, Collegeboard uses 1-based indexing.

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 appended as an element to the end of the list.
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:

  1. Iteration is repetition: repeating a set of actions based on a set of conditions.
  2. Make sure that you don't have a dead loop, so that the program doesn't keep repeating
  3. We want to automate certain process with loops, this way we reduce unprofessional, repetitive, and duplicate code.
  4. For loops are an example of an iterative loop that could be used to check values within a list, we can establish a variable in a for loop to pass values to the rest of our code,
  5. We can print multi-dimensional lists: petlist = [("Dogs",1),("Cats", 2), ("Fish", 0)] for pet , number in petlist: print(pet + " : " + number)
  6. We can have nested loops inside of a loop.
  7. We can also loop with recursion, if we call a function within itself.

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.

from random import shuffle
questions = [
    ("What is a list?",
    "A) A data structure that could store large amounts of data.", 
    "B) A FIFO structure that outputs the first value that was inputted.", 
    "C) Your Grocery list for shopping on saturday night.", 
    "A"),
    ("How does a while loop work?",
    "A) Recursively executes a function within it's self.", 
    "B) Checks for a certain condition and executes a block of code.", 
    "C) Iterates over each element in a list by assigning each element a variable.", 
    "B"),
    ("What is a list index?",
    "A) A representation of the location of a given list element.", 
    "B) The location of the list in the computer memory", 
    "C) A general term for the elements in a list", 
    "A"),
    ("What list indexing convention is typically used for most programming languages?",
    "A) 1 based indexing", 
    "B) keyword indexing", 
    "C) 0 based indexing", 
    "C"),
    ("What list indexing convention is typically used for collegeboard's lists?",
    "A) pseudo indexing", 
    "B) 1 based indexing", 
    "C) 0 based indexing", 
    "B")
]

correct = 0
total = 5
shuffle(questions)
def printQuestion(tup):
    print(tup[0])
    print(tup[1])
    print(tup[2])
    print(tup[3])

def questionloop():
    correct = 0
    for i in range(len(questions)):
        printQuestion(questions[i])
        ans = input("What is your answer? ")
        if ans == questions[i][4]:
            print("Correct! Nice Job!")
            correct+=1
        else:
            print("Try again next time")
    return correct

correct = questionloop()
print("You got a total of {0} out of {1} problems right!".format(correct, total))
What list indexing convention is typically used for most programming languages?
A) 1 based indexing
B) keyword indexing
C) 0 based indexing
Correct! Nice Job!
How does a while loop work?
A) Recursively executes a function within it's self.
B) Checks for a certain condition and executes a block of code.
C) Iterates over each element in a list by assigning each element a variable.
Correct! Nice Job!
What is a list?
A) A data structure that could store large amounts of data.
B) A FIFO structure that outputs the first value that was inputted.
C) Your Grocery list for shopping on saturday night.
Correct! Nice Job!
What list indexing convention is typically used for collegeboard's lists?
A) pseudo indexing
B) 1 based indexing
C) 0 based indexing
Try again next time
What is a list index?
A) A representation of the location of a given list element.
B) The location of the list in the computer memory
C) A general term for the elements in a list
Try again next time
You got a total of 3 out of 5 problems right!

Extra things I did

  1. I randomized the quiz questions so that the order is different each time
  2. I displayed the user's score out of 5 at the very end.

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):
    N = 7
    res = 0
    for bit in binary:
        if bit == "1":
            res+=2**int(N)
        N-=1
    return res
    #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
binarylist = [binary_convert(i) for i in binarylist if binary_convert(i) < 100]
binarylist
[73, 55]