lists are collections of data- you can use them to store unlimited amounts of data- the data can be procedurally used by using loops and functions that locate list data using indexes

indexes count starting with 0

numbers in lists can be used for math

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:

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 repetition- have a system in place to repeat a function a certain amount of times. copy and pasting a ton of times is very unprofessional.

automate by using loops.

ex: number = 2 def multiplyby8_4times(num): print('Multiplying', num, 'by 8 four times:') i = 0 #i starts at 0 while i < 4: #the function will repeat until i >= 4 num = num * 8 print(num) i += 1 #i increments each time

multiplyby8_4times(number)

can also use for loop

numlist = [1, 2, 3, 4] #more numbers = increase the amount of times it iterates

since numlist is a list of single numbers, it understands 'number' as each number

for number in numlist: prod = number * 2 print(str(number), "times 2 is equal to", str(prod) + ".")

for multiple items in each index

petlist = [("Dogs", 1), ("Cats", 2), ("Fish", 0)]

print("Your pets:") for pet, number in petlist: #in order, the first and then the second print(pet + ": ", number)

BUT it is preferable to use a dictionary

drewpets = [("Drew", ({"dogs": 1, "cats": 1, "fish": 0}))]
ajpets = [("AJ", {"dogs": 1, "cats": 0, "fish": 329})]
johnnypets = [("Johnny", {"dogs": 2, "cats": 0, "fish": 0})]
allpets = [drewpets, ajpets, johnnypets] #a collection of all pet lists

for person in allpets:
    for name, dict in person: #unpacking the name and dictionary
        print(name + "'s pets:")
        for pet, num in dict.items(): #use .items() to go through keys and values
            print(pet.capitalize() + ":", num) #capitalizes first letter
    print("")
Drew's pets:
Dogs: 1
Cats: 1
Fish: 0

AJ's pets:
Dogs: 1
Cats: 0
Fish: 329

Johnny's pets:
Dogs: 2
Cats: 0
Fish: 0

ybkwork = [("Yearbook", ({"ybk8": 3, "ybk14": 5, "ybk17": 2}))]
mathwork = [("AFA", ({"A11": 1, "A9": 1}))]
englishwork = [("Scarlet Letter", ({"Chapter 11": 2, "Socratic Seminar": 3}))]
alltasks = [ybkwork, mathwork, englishwork]

for task in alltasks:
    for name, dict in task: 
        print(name + " to do:")
        for tasks, num in dict.items(): 
            print(tasks.capitalize() + ":", num) 
    print("")
Yearbook to do:
Ybk8: 3
Ybk14: 5
Ybk17: 2

AFA to do:
A11: 1
A9: 1

Scarlet Letter to do:
Chapter 11: 2
Socratic seminar: 3

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 = [
    #questions go here (remember to make them strings!)
]

def questionloop():
    pass
    #make an iterative function to ask the questions
    #this can be any loop you want as long as it works!

def answercheck():
    pass
    #make a function to check if the answer was correct or not
def qwp(prompt):
    print("question:" + prompt)
    msg = input()
    return msg

response = qwp("what is the append syntax for python?")
if response == "aList.append(value)":
    print("that's right!")
else:
    print("boo unfortunately it was aList.append(value)")

response = qwp("what can you use for automation?")
if response == "loops":
    print("that's right!")
else:
    print("boo unfortunately it was loops")

response = qwp("is copy pasting repeatedly professional?")
if response == "no":
    print("that's right!")
else:
    print("boo unfortunately it was no")

response = qwp("what do indexes normally count starting with?")
if response == "0":
    print("that's right!")
else:
    print("boo unfortunately it was 0")

response = qwp("can numbers in lists be used for math?")
if response == "yes":
    print("that's right!")
else:
    print("boo unfortunately it was yes")
question:what is the append syntax for python?
that's right!
question:what can you use for automation?
that's right!
question:is copy pasting repeatedly professional?
that's right!
question:what do indexes normally count starting with?
that's right!
question:can numbers in lists be used for math?
that's right!

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[6]
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):
    pass
    #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
print(binarylist)
['01001001', '10101010', '10010110', '00110111', '11101100', '11010001', '10000001']
def binarytoDec(n):
     
    decimalNum = [0] * n;
 

    i = 0;
    while (n > 0):
 
       
        decimalNum[i] = n + 2;
        n = int(n / 2);
        i += 1;
 
   
    for j in range(i - 1, -1, -1):
        print(decimalNum[j], end = "");
 

n = "01001001", "10101010", "10010110", "00110111", "11101100", "11010001", "10000001";
binarytoDec(n);
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In [14], line 20
     16         print(decimalNum[j], end = "");
     19 n = "01001001", "10101010", "10010110", "00110111", "11101100", "11010001", "10000001";
---> 20 binarytoDec(n)

Cell In [14], line 3, in binarytoDec(n)
      1 def binarytoDec(n):
----> 3     decimalNum = [0] * n;
      6     i = 0;
      7     while (n > 0):

TypeError: can't multiply sequence by non-int of type 'tuple'

I had a lot of trouble with this because I have converted binary to decimal before but never the other way around. I wasn't really sure where to start because I knew I needed to start with 2 for the binary, but I didn't know how to incorporate the whole list of values because I've only converted one value at a time.