Overview and Notes: 3.10 - Lists

  • Make sure you complete the challenge in the challenges section while we present the lesson!

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 a List 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 and 3.10

  • lists are collections of data
  • indexes: position

    • most languages start at zero (but cb starts at 1)
    • used when calling a certain value from a list
  • operations that can be done on lists

    • append values
    • assign a value from a list to a variable
    • insert a value at a specific index in the list
    • remove values from lists
  • loops!

    • for loops: useful for applying a function to everything in a list one-by-one
      • for x in mylist:
    • recursive loops: good when you want to limit the starting point
    • while loops: don't require a function that is then called again within the original function until a condition is met. perform all the way through
      • while i> 0:

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.

print("For this quiz on lists and iteration, please answer using lowercase letters")

def questionandanswer(prompt, answer):   # defines question_and_answer
    print("Question: " + prompt)      # asks the question
    msg = input()                  # the user's input/answer is taken
    if msg == answer:
        print(msg + " good job!")
    else:
        print("wrong :(")

qa = {
    "What is a list? \n a) a collection of data \n b) an iterating function \n c) a type of loop" : "a",
    "______ adds data to a list and using the _____ you can accesses specific data \n a) a add and remove \n b) append and index \n c) insert and select": "b",
    "The three types of loops are: \n a) for, recursive, while \n b) recursive, while, on event \n c) while, after, for" : "b",
    "Dictionarys use ___ brackets in python \n a) square \n b) curly \n c) parenthetical" : "a",
    "For loops are good when you want to: \n a) control number of iterations \n b) use strings instead of integers \n c) apply the same function to an entire list" : "c",
}
    
correct = 0
for item in qa:
    num = questionandanswer(item, qa[item])
For this quiz on lists and iteration, please answer using lowercase letters
Question: What is a list? 
 a) a collection of data 
 b) an iterating function 
 c) a type of loop
a good job!
Question: ______ adds data to a list and using the _____ you can accesses specific data 
 a) a add and remove 
 b) append and index 
 c) insert and select
b good job!
Question: The three types of loops are: 
 a) for, recursive, while 
 b) recursive, while, on event 
 c) while, after, for
wrong :(
Question: Dictionarys use ___ brackets in python 
 a) square 
 b) curly 
 c) parenthetical
a good job!
Question: For loops are good when you want to: 
 a) control number of iterations 
 b) use strings instead of integers 
 c) apply the same function to an entire list
d good job!

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" and "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.insert(2, "artichokes")

# Print the entire list, does it match ours ? 
grocery_list

# Expected output
# carrots
# carrots
# artichokes
# ['apples', 'eggs', 'artichokes', 'carrots', 'cucumbers', 'umbrellas', 'artichokes']
carrots
['apples',
 'eggs',
 'artichokes',
 'oranges',
 'carrots',
 'cucumbers',
 '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"]
decimal_list = []

for x in binarylist:
    decimal = int(x,2) 
    decimal_list.append(decimal)

max_valid = 100 # set max value
for x in range(len(decimal_list)): # for each thing in decimal list
    if decimal_list[x] < max_valid: # if value is smaller than the max
        print(decimal_list[x]) # print it!
73
55