For Loops

These loops are good for applying a certain alogrithm or function to an entire list of similar things. However, how many times the function iterates is not as controllable.

We already talked a bit about them in the previous iteration lesson, so here's a review.

list = ["cheese", 328, 3.1415926, True]
# This for loop show what the type of everything in the list is
for thing in list:
    print(type(thing))
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>

For loops are mostly useful for applying a function to everything in a list one-by-one. It's technically possible to make for loops act the same as other loops, but it often takes a lot more work-arounds.

numlist = [3, 6, 93, 247, 33338, 32, 211]
newlist = []
for number in numlist:
    #this loop generates a new list with the numlist values divided by 2
    newlist.append(number / 2)
print(newlist)

#it is more difficult, for example, to detect the index of the list objects
newerlist = []
index = 0 #with this loop, this new variable is necessary
for number in numlist: #this only applies the division to even indexes
    if index % 2 == 0:
        newerlist.append(number / 2)
    else:
        newerlist.append(number)
    index += 1
print(newerlist)
[1.5, 3.0, 46.5, 123.5, 16669.0, 16.0, 105.5]
[1.5, 6, 46.5, 247, 16669.0, 32, 105.5]

Recursive Loops

Recursive loops can be created in multiple different ways, and involve incrementing a variable until it reaches a certain break point. This can be a good type of loop to use when you want to limit the starting point, for example.

list = ["Benjamin", "Franklin", "Lightbulbs", "Kites", "Scams"]
def recursive_loop(i):
    if i < len(list):
        print(list[i])
        recursive_loop(i+1)
    else:
        return
# This will print the whole list
recursive_loop(0)
print("---")
# This will print only the last three things
print("Things associated with Benjamin Franklin:")
recursive_loop(2)
Benjamin
Franklin
Lightbulbs
Kites
Scams
---
Things associated with Benjamin Franklin:
Lightbulbs
Kites
Scams

Unlike the for loop from before, because i always represents the current index of the list in question, we don't need to include the extra index variable.

numlist = [3, 6, 93, 247, 33338, 32, 211]
newerlist = []
def recursive_num(i):
    if i < len(numlist):
        if i % 2 == 0:
            newerlist.append(numlist[i] / 2)
        else:
            newerlist.append(numlist[i])
        recursive_num(i + 1)
    else:
        print(newerlist)
#even though we were able to omit that index variable,
#some would argue this is more complicated
recursive_num(0)
[1.5, 6, 46.5, 247, 16669.0, 32, 105.5]

While Loops

While loops are very similar to recursive loops, but with a different syntax.

While loops don't require a function that is then called again within the original function until a condition is met. While functions perform all the way through, and if the condition is still satisfied afterward, the while loop will repeat again.

list = ["Sample", "Text", "For", "Learning"]
def while_loop():
    i = 0
    while i < len(list):
        print(list[i])
        i += 1
# This will print the whole list
while_loop()
Sample
Text
For
Learning

And here's a while loop version of the function from before.

numlist = [3, 6, 93, 247, 33338, 32, 211]
newerlist = []
def while_num():
    i = 0
    while i < len(numlist):
        if i % 2 == 0:
            newerlist.append(numlist[i] / 2)
        else:
            newerlist.append(numlist[i])
        i += 1
    print(newerlist)
#this version has a bit less clutter than recursive with one less if/else
while_num()
[1.5, 6, 46.5, 247, 16669.0, 32, 105.5]

Challenge

This isn't the homework, but giving it a solid attempt and showing it on your review ticket will definitely boost your grade! You'll have time to work on it during the lesson.

Create a loop that converts 8-bit binary values from the provided list into decimal numbers. Then, after the value is determined, remove all the values greater than 100 from the list using a list-related function you've been taught before. Print the new list when done.

Once you've done this with one of the types of loops discussed in this lesson, create a function that does the same thing with a different type of loop.

binarylist = [
    "01001001", "10101010", "10010110", "00110111", "11101100", "11010001", "10000001"
]

def binary_convert(binary):
    pass
    #use this function to convert every binary value in binarylist to decimal
    #afterward, get rid of the values that are greater than 100 in decimal

#when done, print the results

At the end, reflect on which type of loop was the easiest/most efficient to use and consider why.