ALL MODULESmodule_06.py

Loops — while & for

~60 minTheoryLive CodingChallengeQuiz
theory.md

6. Loops — while & for

6.1Repeating While True (while Loops)

Think of an alarm clock — it keeps ringing WHILE you haven't hit the snooze button.

SHOW:

PYTHON
WHAT HAPPENED HERE
Python repeatedly checked count > 0. Each loop cycle printed the number and subtracted 1 from count until count reached 0.
THE TECHNICAL NAME
Repeating code based on a condition is a while loop. Each execution of the loop block is an iteration.
TAKEAWAY
A while loop repeats as long as its condition stays True.

6.2Repeating Over Sequences (for & range)

Think of stepping down a flight of 5 stairs — you know exactly how many steps you need to take.

SHOW:

PYTHON
WHAT HAPPENED HERE
range(1, 4) generated numbers 1, 2, and 3. The for loop assigned each number to variable i one by one.
THE TECHNICAL NAME
Repeating a known number of times uses a for loop. The range(start, stop) function generates number sequences.
TAKEAWAY
Use for i in range(start, stop) to repeat code a set number of times.

6.3Iterating Through Lists & Strings

Think of a teacher reading student names from an attendance sheet one by one.

SHOW:

PYTHON
WHAT HAPPENED HERE
The for loop automatically pulled each element out of the names list in order and stored it in variable name.
THE TECHNICAL NAME
Accessing every item in a collection one after another is called iterating.
TAKEAWAY
Use for item in collection: to process every element cleanly.

6.4Loop Control (break & continue)

Think of line jumping at an amusement park: continue skips the current person, while break closes the ride completely.

SHOW:

PYTHON
WHAT HAPPENED HERE
When num == 3, continue skipped the print line. When num == 5, break terminated the entire loop immediately.
THE TECHNICAL NAME
break exits a loop early, and continue skips to the next iteration.
TAKEAWAY
Use break to stop a loop and continue to skip an item.
GOTCHA 1

Forgetting to update counter variables inside while loops, creating infinite loops that freeze execution.

GOTCHA 2

Off-by-one errors with range(start, stop) — remembering stop index is exclusive!

GOTCHA 3

Modifying list items while iterating over the exact same list.

WHERE THIS SHOWS UP IN REAL CODE

Games run infinite loops checking player inputs until Game Over, while servers loop over database records to generate reports.

TRY THIS YOURSELF

Write a for loop with range(1, 6) printing squares of each number.

live_coding — Retry Mechanism & Inventory Scanner

Notice how while loop exits as soon as success becomes True.

Python not loaded yet.
demo — Demonstrate while loop counter increments, for range iterations, break, and continue.
for n in range(1, 8):
    if n % 15 == 0: print("FizzBuzz")
    elif n % 3 == 0: print("Fizz")
    elif n % 5 == 0: print("Buzz")
    else: print(n)
n=1
n=2
n=3
n=4
n=5
n=6
n=7

current iteration → Fizz? no

challenge_1

Warm-up 1: Count from 1 to 3 using a while loop.

Steps:

  1. Start i = 1.
  2. Loop while i <= 3, print Count: i, increment i += 1.
Python not loaded yet.
challenge_2

Warm-up 2: Print numbers 0 to 2 using for loop and range.

Steps:

  1. Use for num in range(3):.
  2. Print Num: num.
Python not loaded yet.
challenge_3

Warm-up 3: Iterate over list of items.

Steps:

  1. Given items = ["pen", "paper"].
  2. Loop over items printing Item: pen.
Python not loaded yet.
challenge_4

Apply-it 1: Calculate sum of numbers 1 to 5.

Steps:

  1. Initialize total = 0.
  2. Use for n in range(1, 6): adding n to total.
  3. Print Sum: 15.
Python not loaded yet.
challenge_5

Apply-it 2: Filter even numbers using continue.

Steps:

  1. Loop for x in range(1, 6):.
  2. If x % 2 != 0 (odd), continue.
  3. Print Even: x.
Python not loaded yet.
challenge_6

Apply-it 3: Stop search when target found using break.

Steps:

  1. Given data = [10, 25, 50, 99, 100].
  2. Loop through data; if item == 50, print Found 50! and break.
Python not loaded yet.
challenge_7

Stretch Challenge: Multiplication Table Generator.

Steps:

  1. Print multiplication table for 3 up to 3 x 3.
  2. Output formatted:

3 x 1 = 3 3 x 2 = 6 3 x 3 = 9

Python not loaded yet.
quiz — 9 questions
30s question timers are paused. Start individually or all together.
# question 1

What condition causes a while loop to stop running?

# question 2

What numbers does `range(1, 5)` generate?

# question 3

What does the `break` statement do inside a loop?

# question 4

What does the `continue` statement do inside a loop?

# question 5

What is an infinite loop?

# question 6

What does `range(3)` generate?

# question 7

How many times will `for x in [10, 20, 30]:` run?

# question 8

What is an off-by-one error?

# question 9

Can you iterate over characters of a string with a `for` loop?

discussion

# comments · loops

  • @priya · 2h ago

    The typewriter hero is such a nice touch.

    • @alex · 1h ago

      Totally agree! Made it super engaging.

  • @sam · 1d ago

    Finally a course where I can run code without leaving the page.