Preparation

Create a file called loops_lab.py and put all of your code for today inside this file.

Exercise 1

Consider the following loop.

count = 0
for i in range(10):
    if i % 3 == 0:
        count = count + 1

print("The final count is:", count)

a. Predict what the value of count is when it is printed.

b. Verify your prediction by copying and pasting the code into your loops_lap.py file and running it via the terminal.

c. Add the following line of code inside the if statement immediately after the count increment and rerun the code.

print("Currently, i =", i, "and count =", count)

d. Try changing the input passed to range to several other numbers and make sure it does what you expect.

Exercise 2

Consider the following loop.

integers = [5, 9, -17, 2, 4, 4, 8, 3, -1]
count_even = 0
count_odd = 0

for num in integers:
    if num % 2 == 0:
        count_even = count_even + 1
    else:
        count_odd = count_odd + 1

print("The final even / odd counts are:", count_even, "/", count_odd)

a. Predict what the values of count_even and count_odd are when they are printed.

b. Verify your prediction by copying and pasting the code into your loops_lap.py file and running it via the terminal. (Be sure to comment out other code that may be in the file.)

c. Add the following line of code inside the for loop but after the if else statement. (It should be horizontally aligned with else:.)

print("Currently, num =", num, ", count_even =", count_even, ", and count_odd =", count_odd)

d. Try changing the numbers in integers to see how the execution of the loop changes.

Exercise 3

Consider the following loop.

old_list = [10, "the", -7, 17.5, "quick", "brown", 19, "fox"]

new_list = []
for element in old_list:
    if type(element) == str:
        new_list.append(element)

print("The final values of new_list are:", new_list)

a. Predict what the value of new_list is when it is printed.

b. Verify your prediction by copying and pasting the code into your loops_lap.py file and running it via the terminal. (Be sure to comment out other code that may be in the file.)

c. Add the following line of code inside the for loop but after the if statement. (It should be horizontally aligned with if:.)

print("Currently, element =", element, " and new_list =", new_list)

d. Try changing the elements of old_list to see how the execution of the loop changes.

Exercise 4

Rewrite the code from exercise 3 so that it is a function named extract_strings that takes a list as a parameter and returns a copy of the same list that contains only the strings of the original list.

Exercise 5

Consider the following loop.

old_list = [10, "the", -7, 17.5, "quick", "brown", 19, "fox"]

new_list = []
index = len(old_list) - 1
while index >= 0:
    new_list.append(old_list[index])
    index = index - 1

print("The final values of new_list are:", new_list)

a. Predict what the values of new_list are when they are printed.

b. Verify your prediction by copying and pasting the code into your loops_lap.py file and running it via the terminal. (Be sure to comment out other code that may be in the file.)

c. Add the following line of code inside the while loop at the very beginning before the element is appended to the new list.

print("Currently, index =", index, ", old_list[index] =", old_list[index], ", and new_list =", new_list)

d. Try changing the elements of old_list to see how the execution of the loop changes.

Exercise 6

Consider the following loop.

old_list = ["hello", "world", "foo", "bar", "baz"]

new_list = []
index = 0
while index < len(old_list):
    new_list.append(old_list[index])
    index = index + 2

print("The final values of new_list are:", new_list)

a. Predict what the values of new_list are when they are printed.

b. Verify your prediction by copying and pasting the code into your loops_lap.py file and running it via the terminal. (Be sure to comment out other code that may be in the file.)

c. Add the following line of code inside the while loop at the very beginning before the element is appended to the new list.

print("Currently, index =", index, ", old_list[index] =", old_list[index], ", and new_list =", new_list)

d. Try changing the elements of old_list to see how the execution of the loop changes.

e. Notice that new_list always consists of the evenly indexed elements of old_list. Modify the code so that new_list consists of the elements in the odd indices instead.

Exercise 7

Rewrite the code from exercise 6 so that it is a function named extract_odd_elements that takes a list as a parameter and returns a copy of the same list that contains only the strings of the original list.

Exercise 8

a. The range(stop, start) function is pretty helpful. Try executing the following in the REPL to make sure you understand what sequence it is returning.

>>> list(range(0, 5))
>>> list(range(3, 10))
>>> list(range(-7, 4))

b. How is the range function implemented? Let’s try writing it ourselves! Write a function called my_range that takes two integers start and stop as parameters and returns a list that contains the integers starting at start and counting up but not including the integer at stop. You should write your function without using the range function. Below are a few example executions of the function.

>>> my_range(0, 5)
[0, 1, 2, 3, 4]
>>> my_range(3, 10)
[3, 4, 5, 6, 7, 8, 9]
>>> my_range(-7, 4)
[-7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3]