Loops & Iteration
🔁 Topic 05 · Iteration
Loops & Iteration
Repeat actions efficiently using for and while loops, and control iteration with break, continue, and handy built-ins like enumerate and zip.
⏱ ~40 min
🟢 Beginner
🔄 Core skill
for Loop
The for loop iterates over any sequence: lists, strings, ranges, tuples, and more.
while Loop
Runs as long as a condition stays true. Always ensure the condition eventually becomes false to avoid an infinite loop.
⚠️ Infinite loop risk
If the condition never becomes False the program hangs. Press Ctrl+C to interrupt. Always verify the loop variable changes each iteration.
break and continue
break exits the loop immediately. continue skips the rest of the current iteration and moves to the next.
enumerate & zip
enumerate gives index + value in each iteration. zip pairs two sequences together.
List Comprehensions
A concise, readable way to build a new list by transforming or filtering an existing sequence.