Unit 1

For Loops

Code Tracing

[9.0] Why would you use a for loop over a while loop?

[9.1] How many times will "Hi!" be printed?

for i in range(9):
    print("Hi!")

[9.2] How many iterations are performed by the following loop?

for i in range(10, 20):
    # Some code here

[9.3] What will the output of the following code be?

for i in range(2, 8, 2):
    print(i * 3)

[9.4] What pattern is printed by the following code?

for i in range(5, 0, -1):
    print('*' * i)

[9.5] Trace the code below. What will it print once the code is done executing?

for num in range(7, 24, 2):
    print(num)

[9.6] Trace the code below. What will it print once the code is done executing?


for num in range(7, 13):
    if num % 2 == 0:
        print("even")
    else:
        print("odd")

[9.7] Trace the code below. What will it print once the code is done executing?

for num in range(15, 35):
    if num % 3 == 0:
        print("multiple of 3")
    elif num % 5 == 0:
        print("multiple of 5")
    else:
        print("not multiple of 3 or 5")

[9.8] What will the code below print once the code is done executing?

n = 3
k = 4

for i in range(2 * n + 1):
    j = k - i
    while j > 0:
        print(j)
        j -= 2
    print()

[9.9] What will the value of 'total' be after the following code executes? What about the value of num?

total = 0
for num in range(3, 15):
    total += num

[9.10] What will be the value of 'result' after the following code executes?

result = 1
for num in range(10, 0, -1):
    result *= num

[9.11] What would the values of i be for the following for loops?
a. for i in range(2)
b. for i in range(9, 3, -1)
c. for i in range(3, 18, 3)

Code Writing

[9.12] Convert this for loop to a while loop.


for outer in range(1, 7):
    for inner in range(1, outer + 2, 2):
        print(inner)

[9.13] Write a for loop that prints out the integers 2 through 10, each on a new line.

[9.14] Display every other number from -10 to -1 using a for loop.

[9.15] Write a function that takes a parameter n and counts all the multiples of 5 between 1 to n, and returns this count. Use a for loop.

[9.16] Write a for loop that goes through numbers 1 through 20, printing "Fizz" for multiples of 3, "Buzz" for multiples of 5, "FizzBuzz" for multiples of both 3 and 5, and otherwise printing the number itself.

[9.17] Write a for loop that prints every other odd number between 1 and 50. For example, it should print 1, 5, 9, 13, 15… and so on.

[9.18] Convert the following while loop into an equivalent for loop:

count = 0
while count < 5:
    print("Count:", count)
    count += 1

[9.19] Write a for loop that will calculate and print the sum of the squares of numbers from 1 to 5.

[9.20] Write a program to print the following star pattern using for loops.


* 
* * 
* * * 
* * * * 
* * * * * 
* * * * 
* * * 
* * 
*

[9.21] Convert the following while loop to a for loop. Then, what will the value of product be after the for-loop code executes?


product = 1
outer_num = 2
while outer_num < 6:
    inner_num = 1
    while inner_num < 6:
        product = outer_num * inner_num
        inner_num += 1
    outer_num += 1