Lesson 4 3.8-3.10 Notes and Hacks
Joselyn Anda
3.8 Iteration
Key Vocabulary
- Iteration - Repetition of a Process
- For Loop - repeats a function for a set number of times; I is the number of times repeated
- While Loop - (repeat until), 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.
i = 10
for (i > 0; i--) {
println(i);
}
for (var i = 1; i <= 10; i++) {
for (var j = 1; j <= 10; j++) {
println(i * j);
}
}
var numRabbits = 2;
var numYears = 0;
while (numRabbits <= 100) {
numRabbits += (numRabbits/2 * 30);
numYears++;
}
3.10 Lists
Key Vocabulary
- 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
fruits = ["apple", "banana", "kiwi", "pomegranate"]
print(f"Fruits before append: {fruits}")
fruits.append("dragonfruit") # ADDS TO THE END OF THE LIST
print(f"Fruits after append: {fruits}")
food = ["apple", "banana", "kiwi", "pomegranate"]
vegetables = ["carrot", "cucumber", "eggplant"]
print(f"Fruits before extend: {fruits}")
fruits.extend(vegetables) # adds the vegetable list to the end of the food list
print(f"Fruits after extend: {fruits}")
fruits = ["apple", "banana", "kiwi", "pomegranate"]
print(f"Fruits before insert: {fruits}")
fruits.insert(1, "dragonfruit")
print(f"Fruits after insert: {fruits}")
fruits = ["apple", "banana", "kiwi", "pomegranate"]
print(f"Fruits before pop: {fruits}")
fruits.pop()
print(f"Fruits after pop (no parameter): {fruits}")
fruits.pop(0)
print(f"Fruits after pop (specifying index 0): {fruits}")
fruits = ["apple", "banana", "kiwi", "pomegranate"]
print(f"Fruits before remove: {fruits}")
fruits.remove("apple")
print(f"Fruits after remove (removing apple): {fruits}")
fruits.remove("kiwi")
print(f"Fruits after remove (removing kiwi): {fruits}")
TwoDArray = [[1, 0, 1, 0, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0, 1, 1], [1, 1, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 1, 1, 1, 1]]
print(TwoDArray[0]) # print first sublist
print(TwoDArray[1]) # print second sublist
print(TwoDArray[2]) # print third sublist
# These 1s and 0s could represent anything. AMAZING DATA ABSTRACTION!
You can iterate over these using multiple loops. This is useful for algorithms
bin_0 = 0
bin_1 = 0
for array in TwoDArray:
for bin in array:
if bin == 0:
bin_0 += 1
elif bin == 1:
bin_1 += 1
print(f"Num 0s: {bin_0}")
print(f"Num 1s: {bin_1}")
arr = [9,1,5,6,3,7,2,8]
print(f"array before sort {arr}")
def insertion_sort(arr):
for index in range(1,len(arr)): # repeats through length of the array
value = arr[index]
i = index - 1
while i >= 0:
if value < arr[i]:
arr[i+1] = arr[i] # shift number in slot i to the right
arr[i] = value # shift value left into slot i
i = i - 1
else:
break
IS = insertion_sort(arr)
print(f"array after sort {arr}")
Homework Hacks
Exercises
- I learned that there is a function to actually reverse the whole list, listname.reverse
- I also learned that you cannot print a function(list), it would not print the result and instead say None.
- I learned that you have to iterate through a list using a for loop to swap the places of the elements in a list, and that there is no special code like .reverse to do it.
Multiple Choice Quiz
- I keep mixing up a while loop for iterating for a set amount of time instead of until a condition is met.
- I thought that "until a user inputs 'quit'" counted as some sort of condition to be met, so I just assumed it would be a while loop but it is a for loop.
- There are three nested lists inside the big list. I again forgot that while loops are iterating over a certain interval until a condition is met. There are no conditions to be met in this list and loop, so they would be for loops.