Loops
๐ What are loops? Loops are commands that **repeat** work automatically so you do not type the same lines again and again. A `for` loop is great when you know how many steps you want (or you want to walk through a list). A `while` loop keeps going **as long as** a condition stays true.

Appy Saysโฆ
Without loops, you'd need to copy-paste the same code hundreds of times. With loops, you write it once and the computer repeats it as many times as you need. Games, animations, data processing โ everything uses loops.
What are Loops?
A loop repeats a block of code automatically. Python has two kinds: for loops (repeat a specific number of times or go through every item in a list) and while loops (keep repeating until a condition becomes False).
- โข
for i in range(5):runs the block 5 times (i = 0, 1, 2, 3, 4) - โข
for item in my_list:runs the block once for every item in the list - โข
while condition:keeps running as long as the condition is True - โข
breakexits the loop early;continueskips to the next round
Think of it like spawning enemies in a game
In a wave-based game, the engine loops through each enemy in the wave and spawns it: for each enemy in wave: spawn(enemy). When all enemies are dead, the condition changes and the loop stops. A while loop is like a game lobby โ it keeps checking 'are all players ready?' and only moves on when they are.
How For Loops Work
- โขrange(n) generates numbers from 0 up to (but not including) n
- โขrange(start, stop) generates from start up to stop
- โขrange(start, stop, step) goes in steps:
range(0, 10, 2)gives 0, 2, 4, 6, 8 - โขThe loop variable (
i) changes each round โ you can use it inside the loop - โขTo iterate a list:
for fruit in fruits:โfruitbecomes each item in turn
Real-World Examples
- โขSending a notification to every follower:
for user in followers: send_notification(user) - โขAnimating 60 frames per second: the game loop runs 60 times every second
- โขChecking every product in a shop:
for item in cart: total += item.price - โขLoading a playlist:
for song in playlist: add_to_queue(song) - โขA quiz app:
for question in questions: ask(question)
Key Facts
- โข
range()does NOT include the end number โrange(5)is 0,1,2,3,4 - โขAn infinite loop (
while True:) runs forever โ you need abreakinside to exit - โขLoops can be nested inside other loops (a loop inside a loop โ like a grid)
- โข
len(my_list)gives the number of items โ useful in range:range(len(items)) - โขMost professional code uses loops thousands of times โ they're everywhere
Watch Out: Infinite Loops
A while loop that never becomes False will run forever and freeze your program. Always make sure your loop condition can eventually become False, or include a break statement.
Remember
Use for when you know the count or have a list to go through. Use while when you're waiting for something to change (user input, a sensor value, a timer). If you're not sure, start with for.
What You Learned
- โข
for i in range(n):โ repeats n times - โข
for item in list:โ goes through every item in a list - โข
while condition:โ repeats until condition is False - โข
breakexits early;continueskips to the next round - โขLoops are one of the most-used features in all programming
Key Facts
- โ
range()does NOT include the end number โrange(5)is 0,1,2,3,4 - โAn infinite loop (
while True:) runs forever โ you need abreakinside to exit - โLoops can be nested inside other loops (a loop inside a loop โ like a grid)
- โ
len(my_list)gives the number of items โ useful in range:range(len(items)) - โMost professional code uses loops thousands of times โ they're everywhere
Real-World Examples
Remember
Use for when you know the count or have a list to go through. Use while when you're waiting for something to change (user input, a sensor value, a timer). If you're not sure, start with for.
Quick Quiz
How many times does range(3) run?