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. They are used to store unlimited amount of data and can be called using loops and functions. It is a good way to organize infomation and use it in code at the same time.
  • Indexes are the position a thing is set in the list. Most code languages start at 0.
  • The indexes can be added together using '+' to get a combined value.
  • Operations of lists are listed in the table below. The commands are used to get infomation or change the list.

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. This allows code to repeat a function a set amount of times. Used by every single coding language using a type of loop.
  • Conditional Iteration is like while loops(loops while something is set to something else).
  • Iteration in Lists and Dictionares will use for loops.
  • Types of Loops include . . .
    • For Loops: For Loops makes it so that a function or multiple functions with loop for a set amount of things or numbers. It is good for checking something in a list because it does the function one by one.
    • Recursive Loops: Recursive Loops are functions that loop other functions until something else happens. It's like "do this until that". This can work like a for loop but is used for a lot more other functions.
    • While Loops: While Loops are very similar to recursive loops. It uses boolean and operations and works as "do this until that".

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 = ["What is the starting index value used in most coding languages?", "What is used for repetition of a function?", "What are iteration most often done with?", "What do list collect and store?", "What locates items that are inside of a list?"
    #questions go here (remember to make them strings!)
]
correct_answers = ["0", "iteration", "loops", "data", "index"
]

def questionloop(questions_input):
    #make an iterative function to ask the questions
    #this can be any loop you want as long as it works!
    i = 0
    ans_inputs = []
    while i < len(questions_input):
        ans_inputs.append(input(questions_input[i]))
        i += 1
    return ans_inputs

def answercheck(q, ans_chk, input_ans):
        #make a function to check if the answer was correct or not
    i = 0
    while i < len(ans_chk):
        print("The question was:" + " " + q[i] + " You answered " + input_ans[i])
        if ans_chk[i] == input_ans[i]:
            print("Correct")
        else:
            print("Incorrect! It was " + ans_chk[i])
        i += 1
    return

answers = questionloop(questions)
answercheck(questions, correct_answers,answers)
The question was: What is the starting index value used in most coding languages? You answered 0
Correct
The question was: What is used for repetition of a function? You answered iteration
Correct
The question was: What are iteration most often done with? You answered loops
Correct
The question was: What do list collect and store? You answered data
Correct
The question was: What locates items that are inside of a list? You answered index
Correct

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', 'eggs', 'oranges', 'carrots', 'cucumbers']
print(grocery_list[4])
x = grocery_list[4]
print(x)
grocery_list.append("umbrellas")
grocery_list.append("artichokes")
grocery_list[3] = grocery_list[7]
print(grocery_list[3])
grocery_list.remove('milk')
print(grocery_list)

# Print the entire list, does it match ours ? 

# 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):
    #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
    #decimallist.remove('milk')
    i = 0
    num = []
    while i < len(binary):
        if (int(binary[i], 2) <= 100):
            num.append(int(binary[i], 2))
        i += 1
    return num

decimallist = binary_convert(binarylist)

#when done, print the results
print(decimallist)

def binary_convert2(binary):
    #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
    #decimallist.remove('milk')
    num = []
    for x in range(0, len(binary)):
        if (int(binary[x], 2) <= 100):
            num.append(int(binary[x], 2))
    return num

decimallist2 = binary_convert2(binarylist)

#when done, print the results
print(decimallist2)
[73, 55]
[73, 55]

Struggle

I was struggling with Challenge 3.8. For some reason that is unknown to me, the output would contain values that didn't seen possible based on the restrictions of the code. I was unable to find out why. However, the code in challenge 3.8 does produce the same results but instead of starting with the whole list and removing the values over 100, the code starts the list with nothing and adds the values less than 100. Instead of the .remove function, the code used .append which for this list I think is better because most of the values are over 100.