Lists and Iteration Exercises:

  1. print in reversed order using lists and iteration
list = [1, 2, 3, 4, 5]
reversedList = []

for value in list:
    reversedList = [value] + reversedList

print(reversedList)
[5, 4, 3, 2, 1]

Faster Way to do it without iteration:

list = [1, 2, 3, 4, 5]
list.reverse()
print(list)
[5, 4, 3, 2, 1]
  1. Sort array using bubble sort
list = [9, 8, 4, 3, 5, 2, 6, 7, 1, 0]
def 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

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

Faster Way to do it without algorithm:

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

Learning Points

  • While there may be functions that quickly allow for reversing or sorting, using algorithms or loops may be better for the long run, because once you start getting to more complex problems, algorithms allow for the most efficient and best way to solve a problem
  • how to use the sort() and reverse() function
  • how to use the range() function
  • The Python index() method helps you find the index position of an element or an item in a string of characters or a list of items.
  • Indexing/List Index: position of an element in a list, starting from 0.

Multiple Choice Quiz Results

SCORE: 9/10

  • A is wrong because it is not always for a set amount of time, this would be the condition for a for loop. A while loop can repeat forever if the condition is always true. Since you are testing if the condition is true, this would be a comparison to another conditional, so c is the correct answer. I thought this would be the wrong answer at first because I didn't think it was a comparison, but after doing some extra research using this resource, it made more sense: RESOURCE

Notes:

  • Iteration: the repitition of a process.
  • For Loop: repeats a function for a set number of times; I is the number of times repeated. With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.
  • While Loop: the while loop is used to repeat a section of code an unknown number of times until a specific condition is met. A while loop will execute a set of statements as long as a condition is true.
  • Initialization: whatever sets the counter variable to a starting value.
  • 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: append adds an element to the end, removes an index or selected index, and pop removes the last item of an index.
  • Elements: an item in a list.
  • Nesting: having one data type or function inside another data type or function.
  • Array - alternate name for a list.
  • A MASSIVE NOTE: 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