Python Loops Foundation
for / while Topic 04 Foundation

Loops repeat work for you

Foundation lesson for Python loops. Read theory and syntax first, then map it to a real-world repeat process.

Picture this: repeat until done

for walks a sequence. while repeats while a condition stays true.

Starti = first
→
Bodydo the work
→
Nexti = next
→
Done?leave the loop

The body can run many times. You write it once.

In easy words: What is a loop?

A loop repeats the same step again and again, so you do not write duplicate code. You write one instruction and Python runs it multiple times.

Diagram — Loop cycle

Start Check condition Body Next repeat while condition is true

Where loops are used

Loops are used for printing numbers, checking every item in a list, processing files line-by-line, game updates, retries, and automation scripts.

for loop

  • for i in range(1, n+1): — i takes 1, 2, 3, … n
  • Perfect for “do something for each number / each item”

while loop

  • while condition: — check first, maybe run, repeat
  • Need to change something inside or it runs forever!

Loop syntax highlight

1for i in range(1, n + 1):
2print(i)
3while val <= 4:
4val += 1

Real world: washing machine

for cycle in range(3): rinse three times — you know the count.
while clothes_dirty: keep washing — stop when clean. Same idea in games (spawn enemies), timers, and counting scores.

for i in range(1, 5):

Press “Run one step” to see each repeat

Example Program

1for i in range(1, 6):
2print(i)

Loops — Topic Quiz

Check understanding

Answer all questions, then click Check answers. Use this before Practice in the editor.

Q1. What is a loop for?

Q2. `for i in range(3)` repeats how many times?

Q3. What usually stops a `while` loop?

Q4. Summing 1..N is best done with…

Score: —