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 that can store lots of information
  • You can manipulate lists to be changed automatically
  • Lists have indexs that represent the location of an element in their respective list. Normally start on 0 in most coding languages.

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 placed in index i in aList. Any element with an index value greater than i will shift one position to the right.
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:

  • To iterate is to repeat. In coding, it is used to describe repeating sections of code based on a set of programs.
  • Make sure to not have a dead loop that infinitely repeats.
  • Automating code is possible using various types of loops.
  • We can make loops inside loops.

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.

qCorrect: 0
qTotal: 5
questions = [
    ("Q1: what is the python syntax for accessing an element of a list?", 
     "A) aList[i]", 
     "B) x <-- aList[3]", 
     "C) idk",
     "A"),
    ("Q2: what is the psuedocode syntax for assigning a value of one index in a list to another?", 
     "A) aList.insert(i, value)", 
     "B) WHAT IS THIS", 
     "C) aList[i] = aList[j]",
     "C"),
    ("Q3: what is the syntax for appending something into a string?", 
     "A) aList.append(value)", 
     "B) REMOVE(aList, i)", 
     "C) help",
     "A"),
     
    ("Q4: what does it mean to append something?", 
     "A) To join something together", 
     "B) To seperate something", 
     "C) what is python?",
     "A"),
     
    ("Q5: what is a recursive loop?",
     "A) a loop that is for something",
     "B) a function",
     "C) loops that involve incrementing a variable until it reaches a certain break point.",
     "C")
]

def printQuestion(tuple):
    print(tuple[0])
    print(tuple[1])
    print(tuple[2])
    print(tuple[3])
qCorrect = 0
qTotal = 5

def questionloop():  
  qCorrect = 0
  qTotal = 5
  for i in range(len(questions)):
      print(printQuestion(questions[i]))
      qAnswer = input("insert answer here...")
      if qAnswer == questions[i][4]:
        print("correct!")
        qCorrect += 1
      else: 
        print("L you got it wrong")
  return qCorrect
      
    
qCorrect = questionloop()
print("you got" + " " + str(qCorrect) + " " + "/" + " " + str(qTotal) + " " +  "right!")
Q1: what is the python syntax for accessing an element of a list?
A) aList[i]
B) x <-- aList[3]
C) idk
None
correct!
Q2: what is the psuedocode syntax for assigning a value of one index in a list to another?
A) aList.insert(i, value)
B) WHAT IS THIS
C) aList[i] = aList[j]
None
correct!
Q3: what is the syntax for appending something into a string?
A) aList.append(value)
B) REMOVE(aList, i)
C) help
None
correct!
Q4: what does it mean to append something?
A) To join something together
B) To seperate something
C) what is python?
None
L you got it wrong
Q5: what is a recursive loop?
A) a loop that is for something
B) a function
C) loops that involve incrementing a variable until it reaches a certain break point.
None
L you got it wrong
you got 3 / 5right!

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"]
declist = []

# how would I reverse engineer binary bit-by-bit? I don't understand an ounce as to what to do.

def binary_convert(binarylist):
    
    print(int(binarylist[0])) # i guess i could try and reverse the remainder stuff from dec -> bin but i don't know how i could go about it
                               # the issue lies with the fact that this is a list with strings.
binary_convert(binarylist)
1001001