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:

  • Lists are collections of data
  • can use them to store unlimited amounts of data
  • synatx for lists:
    • list = ["item0", "item1", "item2"]
    • print(list)
  • index starts with 0
  • you can use the methods in the table below to make changes to your lists.

Fill out the empty boxes:

Pseudocode Operation Python Syntax Description
x ← aList[i] aList[i] Accesses the element of aList at index i
x ← aList[i] aList(i) = x Assigns the element of aList at index i
to a variable 'x'
aList[i] ← aList[j] aList(i) = x Assigns the value of a variable 'x' to
the element of a List at index i
aList[i] ← aList[j] aList.insert(i, value) Assigns value of aList[j] to aList[i]
APPEND(aList, 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:

  • loops are good for applying a certain alogrithm or function to an entire list of similar things.
  • for loops: mostly useful for applying a function to everything in a list one-by-one
  • recursive loops: when a function, module or an entity keeps making calls to itself repeatedly, thus forming an almost never-ending loop
  • while loops: used to repeat a specific block of code an unknown number of times, until a condition is met

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.

questions = 5
correct = 0

print("For this test you will be asked " + str(questions) + " questions.")

Q1 = input("What is the index number for the first item in a list?:")
print("Here are your choices: 1. 1, 2. 10, 3. 100, 4. 0")
if Q1 == "1":
    print("GREAT JOB!")
    correct += 1
else:
    print(Q1 + " is incorrect!")

Q2 = input("Which brackets do we use for lists?:")
print("Here are your choices: 1. (), 2. [], 3. {}, 4. no brackets")
if Q2 == "2":
    print("GREAT JOB!")
    correct += 1
else:
    print(Q2 + " is incorrect!")

Q3 = input("What would the index number of grapes be if it were in a list? apples, grapes,kiwi, banana:")
print("Here are your choices: 1. 5, 2. 1, 3. 100, 4. 0")
if Q3 == "2":
    print("GREAT JOB!")
    correct += 1
else:
    print(Q3 + " is incorrect!")

Q4 = input("Which method can we use to add an item to a list?:")
print("Here are your choices: 1. pop, 2. remove, 3. del, 4. append")
if Q4 == "4":
    print("GREAT JOB!")
    correct += 1
else:
    print(Q4 + " is incorrect!")

Q5 = input("What are lists?:")
print("Here are your choices: 1. idk, 2. are used to store multiple items in a single variable, 3. are used to store data values in key:value pairs, 4. grocery list?")
if Q5 == "2":
    print("GREAT JOB!")
    correct += 1
else:
    print(Q5 + " is incorrect!")


print( " you scored " + str(correct) +"/" + str(questions))
For this test you will be asked 5 questions.
Here are your choices: 1. 1, 2. 10, 3. 100, 4. 0
GREAT JOB!
Here are your choices: 1. (), 2. [], 3. {}, 4. no brackets
GREAT JOB!
Here are your choices: 1. 5, 2. 1, 3. 100, 4. 0
GREAT JOB!
Here are your choices: 1. pop, 2. remove, 3. del, 4. append
GREAT JOB!
Here are your choices: 1. idk, 2. are used to store multiple items in a single variable, 3. are used to store data values in key:value pairs, 4. grocery list?
GREAT JOB!
 you scored 5/5

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[4])


# Now, assign the fourth item in the list to a variable, x and then print the variable

x = grocery_list[4]
print(x)

# Add these two items at the end of the list : umbrellas and artichokes

grocery_list.append("umbrellas")
grocery_list.append("artichokes")

print(grocery_list)
# Insert the item eggs as the third item of the list 
grocery_list.insert(2,"eggs")
print(grocery_list)

# Remove milk from the list 

grocery_list.remove("milk")
print(grocery_list)
# Assign the element at the end of the list to index 2. Print index 2 to check
grocery_list[2] = "artichokes"
print(grocery_list)
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']
cucumbers
cucumbers
['apples', 'milk', 'oranges', 'carrots', 'cucumbers', 'umbrellas', 'artichokes']
['apples', 'milk', 'eggs', 'oranges', 'carrots', 'cucumbers', 'umbrellas', 'artichokes']
['apples', 'eggs', 'oranges', 'carrots', 'cucumbers', 'umbrellas', 'artichokes']
['apples', 'eggs', 'artichokes', 'carrots', 'cucumbers', 'umbrellas', 'artichokes']
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"
]

for x in binarylist:
    z = int(x,2) # if you do it without the two it will not convert
    if z < 100:
        print(z)

    
73
55