>> Module 01 / Day 09 / Lecture

Loops Meet Conditions

Day 9 of a 40-day journey
You know loops. You know conditions. Today they team up — and that's where the interesting programs start.
Lecture
75 min · 09:00–10:15
Lab
Patterns + Prime Check
Module
M1 · Python Foundations
DAY 09 / 40
>> A combine day — no new syntax

Nothing new — just combined

For three days you learned tools one at a time. Today there are no new keywords. The skill is wiring what you already have together.

  1. A loop inside another loop — nested loops for 2D patterns
  2. A if inside a loop — decide on every step
  3. The flag / early-decision pattern — stop the moment you know
  4. Lab — print a triangle, then check for primes
>> The big idea

Repetition + choice = logic

A loop on its own does the same thing every pass. Add a condition inside it, and each pass can react: count only the even ones, stop early, print a different character. That combination is the engine behind almost every program you'll ever write.

the_pattern.py
# A loop that *reacts* on every step:
for n in range(1, 11):
    if n % 2 == 0:        # condition INSIDE the loop
        print(n, "is even")
>> 01 / 03 · A loop inside a loop

Nested loops — the outer one drives the inner one

For every single pass of the outer loop, the inner loop runs all the way through. Outer counts rows; inner counts columns.

nested.py
for row in range(3):        # outer: 0,1,2
    for col in range(3):    # inner: 0,1,2
        print(row, col)

# 0 0 / 0 1 / 0 2
# 1 0 / 1 1 / 1 2
# 2 0 / 2 1 / 2 2
9 prints, not 6.
Inner runs fully for each outer step: 3 × 3 = 9. Mind your indentation — the inner loop must sit inside the outer.
>> 01 / 03 · From numbers to shapes

Nested loops draw 2D patterns

triangle.py
for row in range(1, 5):
    for star in range(row):
        print("*", end="")
    print()   # end the row
output
*
**
***
****
>> 02 / 03 · A condition inside a loop

Let each pass make a decision

Put an if inside the loop and the body can behave differently every time — count, filter, or branch on the current value.

count_evens.py
count = 0
for n in range(1, 21):
    if n % 2 == 0:      # decide on THIS number
        count = count + 1
print(f"There are {count} even numbers from 1 to 20.")
# → There are 10 even numbers from 1 to 20.
>> 03 / 03 · The flag / early-decision pattern

Assume — then look for proof you're wrong

Start with a hopeful guess. Loop looking for evidence against it. The moment you find evidence, you can decide and stop. This is how is_prime works.

is_prime.py
def is_prime(n):
    if n < 2:
        return False      # 0, 1, negatives
    for d in range(2, n):
        if n % d == 0:
            return False  # found a divisor — done
    return True           # nothing divided it
Decide early.
As soon as one divisor is found, return False — no point checking the rest. The final return True only runs if the loop finished clean.
>> Putting it together

Both ideas in one program

Loop over numbers, ask a question about each (the flag pattern, hidden in is_prime), and print the ones that pass.

primes_up_to.py
for n in range(2, 11):
    if is_prime(n):       # condition inside the loop
        print(n)
# → 2 / 3 / 5 / 7
>> Common failures

Five mistakes on combine day

  1. Indentation. The inner loop or the if must sit inside the outer loop, not beside it.
  2. Forgetting the bare print() after the inner loop — your triangle prints on one long line.
  3. Forgetting end="" — every star jumps to a new line.
  4. Missing the n < 2 check in is_prime — then 1 and 0 wrongly look prime.
  5. Putting return True inside the loop — it fires on the first non-divisor and never checks the rest.
>> 11:00 – 12:00 · Lab

Today's lab — Patterns + Prime Check

PROBLEM 1
print_triangle(height)

Print a left-aligned right triangle of *. Row 1 has one star, row height has height stars. A nested loop.

PROBLEM 2
is_prime(n)

Return True/False. Handle n < 2, then loop the divisors — the flag pattern.

>> End of Day 09

What you can now do

  • ✓ Nest one loop inside another to build 2D patterns.
  • ✓ Put a condition inside a loop so each pass decides for itself.
  • ✓ Use the flag / early-decision pattern to stop the moment you know.
01 / 11
←/→ navigate   F fullscreen   Ctrl/⌘+P save as PDF