Learning outcomes
- Automate repetitive tasks using loops
Core concepts
Lessons
Module goal
Learner understands how to automate repetition instead of writing the same code over and over, and can safely control when a loop starts and stops.
Lesson 1 of 5 · lecture
Why loops exist
Step 1 of 2
Printing numbers 1 to 100 without loops means 100 separate print() lines — repetitive, error-prone, and impossible to scale to 10,000. Loops let you say “repeat this action” instead of writing it out every time.
Why this is powerful
Computers are extremely good at repeating tasks precisely and quickly. Loops are how you tell them what to repeat and for how long.
Guided labs
Lab part 1 — sum a list of numbers
- 1.Create loops.py
- 2.Create a list of numbers
- 3.Use a for loop to add them together and print the total
numbers = [4, 8, 15, 16, 23, 42]
total = 0
for number in numbers:
total += number
print(f"The total is: {total}")Lab part 2 — number guessing game
- 1.Set a secret_number variable
- 2.Use a while loop that keeps asking until the guess is right
- 3.Give “too high” / “too low” feedback on each wrong guess
secret_number = 7
guess = None
while guess != secret_number:
guess = int(input("Guess the number (1-10): "))
if guess < secret_number:
print("Too low, try again.")
elif guess > secret_number:
print("Too high, try again.")
else:
print("Correct! You guessed it.")Teaching point
guess = None before the loop is necessary — the while condition checks guess before the user has entered anything, so it must start as something not equal to secret_number.
Success criteria
Learner tests the guessing game with multiple wrong guesses before getting it right, confirming the loop keeps running until the condition is met.
Knowledge Check
Question 1 of 6
What does range(1, 6) actually generate?
What you read
- Why loops save repetitive typing
- Difference between for (known iterations) and while (condition-based)
- How to safely exit a loop
What you understand
Key takeaways
- — Loops process collections of data efficiently
- — Infinite loops are a real risk without proper exit conditions
Hands-on lab
Lab tasks
- 1.Loop through a list of numbers and calculate a sum
- 2.Build a simple number-guessing game using while
Knowledge check
- Trace loop output
- Identify infinite loop risks
Write your answers in your own notes before moving on — explaining a concept in your own words is the fastest way to find the gaps.