Hacks

Exercise 1

list  = [1, 2, 3, 4, 5]
newlist = [] # Defining new list
interval = len(list) - 1 # Another variable to store highest index

# for loop, goes through all items
for index in range(1, len(list) + 1):
    i = index - 1
    newlist.append(list[interval - i]) # appends all numbers backwards

print(newlist)
# Using commands to reverse
list.reverse()
print(list)
[5, 4, 3, 2, 1]
[5, 4, 3, 2, 1]

Exercise 2

list = [9, 8, 4, 3, 5, 2, 6, 7, 1, 0]

def bubble_sort(numbers): # function
    has_swapped = True # check if the list has been swapped
    while(has_swapped):
        has_swapped = False
        for i in range(len(numbers) - 1): # checks every value
            if numbers[i] > numbers[i+1]: # checks if the current value is larger than the next one
                holder = numbers[i] # temporary var
                numbers[i] = numbers[i+1] # transferring values
                numbers[i+1] = holder
                has_swapped = True # List has been swapped
    return numbers

print(bubble_sort(list))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Reflection

From these exercises, I learned a lot about list manipulation. I was introduced to some code segments which allowed me to manipulate a list. I also used algorithms to manipulate lists in certain ways. It is important to check the values of indices for certain programs. Indices are very important in lists because they have the value of where each data point is located.

Quiz

I scored an 8/10 on the quiz. I got questions 7 and 9 wrong.

Question 7

Link to Image: https://drive.google.com/file/d/1v6sMC8NobA_R4UbF3aJBKvRwTDdfT_xb/view?usp=sharing

  • Question: What is Base 0 Indexing?
  • Actual Answer: Seguence demarcated with an Index starting from 0

I originally answered a) Binary, thinking that this would refer to the 0s and 1s. This was a silly mistake, and I forgot that Binary conversions are done with base 2. When a list has an index of 0 for its first element, this would be Base 0 Indexing, since the first index is at 0.

Question 9

Link to Image: https://drive.google.com/file/d/1zmxeXww2YxTBzhzHWRuU_J0pQXQYPBSj/view?usp=sharing

  • Question: I want to iterate over a list until the user inputs 'quit'. What loop would I use?
  • Actual Answer: FOR loops

My first answer was while loops because I thought it would make sense for the command to run while a certain condition is true, which would be if the user has not quit. FOR loops would work because it could include for every action, which could include opening an app, lowering the brightness, or shutting down the machine and quitting.

Reflection

I thought I did decently well on the quiz. It will be important to pay attention to what the question is asking for to prevent silly mistakes. I will also need to remember to think through problems, and go through each answer choice to find the best one.

Important Notes

  • Iteration can repeat something multiple times, either based on a condition or a set count
  • Lists methods Mutate, meaning they actively change the list, but they don't return anything. This means that return a None-type, which you cannot manipulate
  • Lists are collections of data, are iterable
  • Lists start at index 0
  • Iterate through lists with for or while loops

Vocab

  • 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