Review: Loops

Module 1: Python Fundamentals

Review of Session 1.3: Loops

Exercise

Without running the code, what is going to be the output of this cell?

for a in [1, 2, 3]:
  print(a)
Exercise

Without running the code, what is going to be the output of this cell?

for b in range(1, 3):
  print(b)
Exercise

Without running the code, what is going to be the output of this cell?

for c in range(1, 4, 2):
  print(c)
print("Next!")
Exercise

Without running the code, what is going to be the output of this cell?

for d in ["apple", "banana", "chocolate"]:
  print(c)
  print("Next!")
Exercise

Without running the code, what is going to be the output of this cell?

# Count the number of steps in the loop
n = 0

for d in ["apple", "banana", "chocolate"]:
  # Increase step count
  n = n + 1

  if n != 1:
    print(d)

  print("Next!")
Exercise

How would you implement a for loop to simplify this piece of code? And a while loop?

# Compute the sum of all positive numbers lower than 10
result = 1
result = result + 1
result = result + 2
result = result + 3
result = result + 4
result = result + 5
result = result + 6
result = result + 7
result = result + 8
result = result + 9
print(result)
Exercise

How would you implement a for loop to simplify this piece of code?

# The multiplication table of 2 and 4
print("Multiplication table of 2")
print("2 x 1 = 2")
print("2 x 2 = 4")
print("2 x 3 = 6")
print("...")  # We keep going until 10
print("2 x 10 = 20")
print("Multiplication table of 4")
print("4 x 1 = 4")
print("4 x 2 = 8")
print("4 x 3 = 12")
print("...")  # We keep going until 10
print("4 x 10 = 40")
Exercise

How would you implement a for loop to simplify this piece of code? And a while loop?

# Simplify this code with a for loop
result = 1
result = result * 2
result = result * 3
result = result * 4
result = result * 5
print(result)
Exercise

How would you implement a for loop to simplify this piece of code? And a while loop?

# Simplify this code with a for loop
list_words = ["Hello", "my", "name", "is", "Daniel"]

sentence = list_words[0]
sentence = sentence + " " + list_words[1]
sentence = sentence + " " + list_words[2]
sentence = sentence + " " + list_words[3]
sentence = sentence + " " + list_words[4]

print(sentence)
Exercise

Look at the following piece of code and explain why it fails.

# Exercise 3.3
# Write a Python program that finds and displays all the multiples of a given
# number within a specified range. The user inputs the number and the range,
# and the program lists the multiples. The output should be a string with all
# the terms separated by commas.

number = 7
limit = 50
# Expected output: "7, 14, 21, 28, 35, 42, 49"

# Initialize the output string
msg = ""

# Loop through all numbers up to the limit
for x in range(1, limit + 1):
  # If x is divisible by our number, add it to the output
  if x % number == 0:
    msg = msg + x + ", "

print(msg)
Exercise

Look at the following piece of code and explain why it fails.

# Exercise 3.5
# Write a Python program that generates the Fibonacci sequence up to a specified
# number of terms. The input should be the number of terms we want to see in
# the sequence. The output should be a string with all the terms separated by
# commas.

num = 5  # Expected output: "0, 1, 1, 2, 3"

# Initialize the first Fibonacci term
fib_1 = 0

# Initialize the second Fibonacci term
fib_2 = 1

# Initialize the number of terms computed
nterms = 2

# Initialize the string of terms
msg = "0, 1"

while nterms < num:
  # Compute the next Fibonacci term
  fib_next = fib_1 + fib_2
  # Add the term to the msg
  msg = msg + ",  " + fib_next
  # Increment the number of terms
  nterms = +1

print(msg)