Vocabulary

  • Iteration: refers to the repetition of processes in an instruction
  • for loop :The command is repeated a certain number of times
  • while loop: the instruction is repeated until the requirement is reached
  • Initialization: starting value of variable.
  • Lists: Sets in iterable data
  • Nested lists: several similar arrays of data grouped together

Notes

  1. For loop repeats a function a certain number of times
  2. The while loop is used to repeat a piece of code a certain number of times until the condition is met.
  3. The while loop is very similar to the if condition, except that while is executed continuously until it is no longer true, while if is executed only once.
  4. List index starts at 0 in the list
  5. append adds an element at the end, remove removes it at the index, and pop removes the last item.

Exercise#1

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

list = [1, 2, 3, 4, 5] # Print this in reverse order

for number in range(5, 0, -1):
    print(number)
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] # Sort this array using bubble sort

def BubbleSort(list) :
    length = len(list)
    for i in range(length):
        for a in range(0, length-i-1):
          if list[a] > list[a+1]:
            tem = list [a + 1]
            list [a + 1] = list [a]
            list [a] = tem
 
if __name__ == "__main__":
  
  BubbleSort(list)
  print ("Sorted Array:")
  for i in range(len(list)):
    print("%d" % list[i], end=" ")
Sorted Array:
0 1 2 3 4 5 6 7 8 9 

A while loop loops over an interval until a condition is met, a for loop iterates over an iterable object and lasts a certain amount of time.

Use a for loop to iterate through the list until the user inputs quit