Notes

Iteration - Repitition of a Process

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.

Indexing / List Index - The position of an element in a list, starting from 0

append, remove, pop - Various methods, append adds an element to the end, remove removes at an index, and pop removes the last item.

Elements [in a list] - An item in a list.

Nesting - Having one data type or function inside another data type or function, such as lists or loops.

Array - Another name for a list, depends on the language

Exercise 1

Task: Reverse a list utilizing features of lists and iteration Hint: Use two parameters with the range function

original_list = [1, 2, 3, 4, 5]
print("List before reverse : ",original_list)
reversed_list = []
for value in original_list:
  reversed_list = [value] + reversed_list
print("List after reverse : ", reversed_list)
List before reverse :  [1, 2, 3, 4, 5]
List after reverse :  [5, 4, 3, 2, 1]

Exercise 2

Task: Similar to insertion sort, this algorithm takes an unsorted array and returns a sorted array Unlike insertion sort where you iterate through the each element and move the smaller elements to the front, this algorithm starts at the beginning and swaps the position of every element in the array Expected Output The sorted array from 1-10

list = [9, 8, 4, 3, 5, 2, 6, 7, 1, 0]
print(f"array before sort {list}")
def insertion_sort(list):
    for index in range(1,len(list)): # repeats through length of the array
        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"array after sort {list}")
array before sort [9, 8, 4, 3, 5, 2, 6, 7, 1, 0]
array after sort [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Multiple Choice Quiz

I got a 9/10 on the quiz.

To the question, "How can we add something to the end of a list?", I chose "Extend", instead of the correct answer, "Append". Appending in Python, refers to adding its arguments as a single element to the end of a list. In other words, the length of the list keeps increasing by one. In extending, you are adding two lists together but in appending, you are just adding one thing to the list.