← Python Coding Track

Module 03FoundationWeeks 1–2

Loops

01

Learning outcomes

  • Automate repetitive tasks using loops
02

Core concepts

for loopswhile loopsbreakcontinue
03

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.

04

Guided labs

Lab part 1 — sum a list of numbers

  1. 1.Create loops.py
  2. 2.Create a list of numbers
  3. 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. 1.Set a secret_number variable
  2. 2.Use a while loop that keeps asking until the guess is right
  3. 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.

05

Knowledge Check

Question 1 of 6

What does range(1, 6) actually generate?

06

What you read

  • Why loops save repetitive typing
  • Difference between for (known iterations) and while (condition-based)
  • How to safely exit a loop
07

What you understand

Key takeaways

  • Loops process collections of data efficiently
  • Infinite loops are a real risk without proper exit conditions
08

Hands-on lab

Lab tasks

  1. 1.Loop through a list of numbers and calculate a sum
  2. 2.Build a simple number-guessing game using while
09

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.

10

Mark progress