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:

  • i always represents the current index, so an extra index variable is not needed
  • indexes can be used to make lists of numbers

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:

  • For Loops - Loops that apply a certain algorithm or function to an entire list of similar things; iterates until told not to/until conditions are met (infinite)
  • Recursive Loops - Loops that run through the code until reaching a specific break point
  • While Loops - run until the end of the list (sometimes not in some cases)

Sorry if there's not many notes most of it was just the chart :(


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

questions_list = [
    #questions go here (remember to make them strings!)
    "What does a for loop do?",
    "What is a while loop?",
    "What is a recursive loop?",
    "How can you append to a list?",
    "Why would you use an index?",
]

answers = [
    [ "A loop that iterates until the completion conditions are met", "Loops that apply a certain algorithm or function to an entire list of similar things", "Loops that apply a certain algorithm or function to a list until completion conditions are met"], 
     [ "Loops that start with 'while'", "A loop that runs until the end of the list", "A loop that runs until the end of the list most of the time"], 
     [ "A loop that runs until the code reaches a breakpoint", "A loop that runs infinitely until it is told to stop", "A loop that has a limited starting point"], 
     [ "aList.pop(i)", "aList.remove(value)", "aList.append(value)"],
     [ "To make a list of numbers", "To make numbers on a list easier to organize", "To categorize data"], 
]

def questionloop():
    s = ""
    while (s != "q"):
        question = random.choice(questions_list)
        print(question)
        s = input()
        print(s)
        answercheck(s, question)
    #make an iterative function to ask the questions
    #this can be any loop you want as long as it works!

def answercheck(s, question):
    i = questions_list.index(question)
    matchedAnswers = list(filter(lambda item: item == s, answers[i])) # Filters wrong/right, if it's not in the list it's wrong, and that makes it appear as 'empty'
    # (Tests, basically shows the len and if the len is >1, the answer is correct) print(matchedAnswers , str(len(matchedAnswers)))
    if (len(matchedAnswers)) >= 1: # if the length was marked as 'none' (0) in the previous line, then it's measured (using len) and marked as incorrect
        print("Correct.")
    else:
        print("Incorrect.")
    #make a function to check if the answer was correct or not

questionloop()
What is a recursive loop?
A loop that runs until the code reaches a breakpoint
Correct.
What is a while loop?
incorrect answer 
Incorrect.
What does a for loop do?
Loops that apply a certain algorithm or function to an entire list of similar things
Correct.
How can you append to a list?
q
Incorrect.

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
fourth = grocery_list[3]
print(fourth)
# Now, assign the fourth item in the list to a variable, x and then print the variable
x = fourth
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
index_2 = grocery_list[6]
print (index_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', 'oranges', '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(bin):
    dec, i = 0, 0 # starts at 0
    for c in bin: # c = character, bin = numbers in binary list
        ci = int(c) # convert string to int
        dec = dec + ci * pow(2,i) # index times power of base 2 = 1 or 0
        i += 1 # adds/increases position in the index
    return(dec)

decList = []
for bin in binarylist:
    dec = binary_convert(bin)
    decList.append(dec)
print ("Binary to decimal conversion of the Binary List:", decList)

filteredList = list(filter(lambda item: item < 100, decList)) # filters list so that ONLY items under 100 appear
print("All values less than 100:", filteredList)


#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
Binary to decimal conversion of the Binary List: [146, 85, 105, 236, 55, 139, 129]
All values less than 100: [85, 55]