Lesson 7: Loops
Loops let you repeat code without rewriting it. Instead of writing print() 100 times, you write it once inside a loop and let Python do the rest.
Key Concepts
The for Loop
for i in range(5): repeats the indented code 5 times, with i being 0, 1, 2, 3, 4 each time. range(start, stop) lets you control where counting begins and ends.
The while Loop
while condition: keeps repeating as long as the condition is True. Be careful — if the condition never becomes False, the loop runs forever (an infinite loop)!
Loop Control
break immediately exits a loop. continue skips the rest of the current loop turn and goes to the next one. These give you fine control over loops.
🆕 Try It: Live Python
Try it — edit the code and click Run!
✅ Check Your Understanding
1. What does range(3) produce?
2. What does break do inside a loop?
3. A while loop runs as long as its condition is...