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:

  • A list is a container to store many datatypes, such as strings, integersa, and booleans
  • A list is defined with square brackets and is useful, rather than keeping a lot of variables
  • There are many functions that you cna use on lists, such as append, insert, and remove
  • lists are acessed by index.
    • the first index is 0, second is one, and so on
    • negative indexes acess elements from the back.

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) | Replaces the element at index i with value | | 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 acess eleemnts of a list in order, one can use iteration
  • With a for or while loop, you can acess the elements in an array or the indexes of an array in order
  • This makes printing data and data manipulation easier
  • saying "for i in list:" iterates through every element in list
  • "for i in range(len(list))" iterates through every index in list

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.

import random
questions = [(0,"How are elements from arrays acessed?"),(1,"Which data types can be stored in arrays"),(2,"What does the function .append() do?"),(3,"What does negative indexing do?"), (4,"what number does indexing start at?"),
(5,"what kind of loop is most used in iteration?")]
options = ["A. [] \n B. () \n C. {} \n D. you cannot acess individual elements",
            "A. int \n B. String \n C. boolean \n D. all of the above",
            "A. remove an element \n B. add an element to the end \n C. add an element to the start \n D. append does not work with a list",
            "A. the same thing as regular indexing \n B. gives elements from a sorted list \n C. index from the back \n D. give a syntax error",
            "A. 0 \n B. 1 \n C. 2 \n D. -1",
            "A. while \n B. for \n C. if \n D. recursive"]
answers = ["A","D","B","C","A","B"]
def questionloop():
    score = 0
    random.shuffle(questions)
    for i in questions:
        answer = input(i[1]+ " " + options[i[0]])
        if answercheck(answer, i[0]) == True:
            print("correct")
            score += 1
        else:
            print("wrong")
    return score
    #make an iterative function to ask the questions
    #this can be any loop you want as long as it works!

def answercheck(answer, index):
    if answer == answers[index]:
        return True
    return False
    #make a function to check if the answer was correct or not
print("Your score is " + str(questionloop()) + "/6. Good Job!")
wrong
wrong
wrong
correct
wrong
correct
Your score is 2/6. 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

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']
binarylist = [
    "01001001", "10101010", "10010110", "00110111", "11101100", "11010001", "10000001"
]

def binary_convert(binarylist):
    decimallist = []
    for i in binarylist:
        number = 0
        power = 7
        for j in i:
            if j == "1":
                number += 2**power
            power -= 1
        decimallist.append(number)
    for i in decimallist:
        if i <= 100:
            print(i)
    print(decimallist)
    #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
binary_convert(binarylist)
73
55
[73, 170, 150, 55, 236, 209, 129]