Important Vocab/Notes

  • For Loop - FOR LOOP repeats a function for a set number of times; I is the number of times repeated
  • While Loop - The while loop is used to repeat a section of code an unknown number of times until a specific condition is met
  • Initialization - What sets the counter variable to a starting value. For example (var i = 0) represents an initial value of 0.
  • Condition - Allows the computer to know whether or not to keep repeating the loop.
  • Increment/decrement - Modifies the counter variable after each repetition.
  • Append, remove, pop - Various methods, append adds an element to the end, remove removes at an index, and pop removes the last item.
  • Iterative statements are also called loops, and they repeat themselves over and over until the condition for stopping is met
list = [1, 2, 3, 4, 5] # Print this list in reverse order

list.reverse()

print('Reversed List:', list)
Reversed List: [5, 4, 3, 2, 1]
list = [9, 8, 4, 3, 5, 2, 6, 7, 1, 0] # Sort this   

list.sort()

print('Sorted List:', list)
Sorted List: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list = [9, 8, 4, 3, 5, 2, 6, 7, 1, 0]
print(f"listay before sort: {list}")
def insertion_sort(list):
    for index in range(1,len(list)): # repeats through length of the listay
        value = list[index]
        i = index - 1
        while i >= 0:
            if value < list[i]:
                list[i+1] = list[i] # shift number in slot i to the right
                list[i] = value # shift value left into slot i
                i = i - 1
            else:
                break

IS = insertion_sort(list)
print(f"listay after sort: {list}")
listay before sort: [9, 8, 4, 3, 5, 2, 6, 7, 1, 0]
listay after sort: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

I got question 8 wrong (image wont load)

  • I selected a because I thought a certain about of time met until a conditon is met
  • I am not sure why I selecetd b because it was not very close to the answer and did not make much sense
  • I selected c after because I realized it was saying the code repeats until a conditon is met, which is a While Loop

I got question 9 wrong (image wont load)

  • I selected a While Loop because I thought the the code would repeat until a condition is met (the user quits)
  • I selected a For Loop because the code is going to repeat a certain number of times until the user quits