Week Six
This week we learn about loops — a way to repeat code multiple times without writing the same lines over and over.
Presentation
Why Loops?
Sometimes you want your program to do the same thing many times. Instead of copying and pasting code, you use a loop.
# Without a loop — repetitive!
print("Hello!")
print("Hello!")
print("Hello!")
# With a loop — much cleaner!
for i in range(3):
print("Hello!")For Loops
A for loop repeats code for each item in a sequence.
Looping Over a List
colors = ["red", "blue", "green"]
for color in colors:
print(color)Output:
red
blue
greenUsing range()
range(n) creates a sequence of numbers from 0 to n - 1.
for i in range(5):
print(i)Output:
0
1
2
3
4Looping with an Index
You can use range() with len() to loop through a list by index:
fruits = ["apple", "banana", "cherry"]
for i in range(len(fruits)):
print(f"{i}: {fruits[i]}")Output:
0: apple
1: banana
2: cherryWhile Loops
A while loop keeps running as long as a condition is True.
count = 0
while count < 3:
print(f"Count is {count}")
count = count + 1Output:
Count is 0
Count is 1
Count is 2Make sure the condition eventually becomes False, or the loop will run forever!
Looping and Lists Together
Loops are especially useful with lists:
scores = [85, 92, 78]
total = 0
for score in scores:
total = total + score
print(f"Total: {total}")
print(f"Average: {total / len(scores)}")Activity
Try writing a loop that prints your name 5 times, then one that prints every item in a list of your favorite foods!
loops.py
foods = ["pizza", "sushi", "tacos"]
for food in foods:
print(f"I love {food}!")Last updated on