๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท
๐Ÿ Python

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.

5 min 10 XP Lesson 5 of 21
Loops
๐ŸŒ

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
  • โ€ขbreak exits the loop early; continue skips 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: โ€” fruit becomes 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 a break inside 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
  • โ€ขbreak exits early; continue skips 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 a break inside 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

โ€ข Sending a notification to every follower: <code>for user in followers: send_notification(user)</code> โ€ข Animating 60 frames per second: the game loop runs 60 times every second โ€ข Checking every product in a shop: <code>for item in cart: total += item.price</code> โ€ข Loading a playlist: <code>for song in playlist: add_to_queue(song)</code> โ€ข A quiz app: <code>for question in questions: ask(question)</code>

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

1 / 2

How many times does range(3) run?

    Loops โ€” Applaa Academy