← Python Programming learning path
Python Programming · Module 5 of 10

Loops: for and while

Repeat work with for loops over sequences and while loops on conditions.

📖 Key ideas

  1. for item in list: runs once per item.
  2. range(5) gives 0, 1, 2, 3, 4.
  3. while condition: repeats while the condition stays True.
  4. break stops a loop; continue skips to the next round.
  5. Watch out for infinite while loops: update the variable.
💡 Real-life example: Counting students in a queue one by one is a for loop; waiting until the bus arrives is a while loop.
forLoops over each item
whileLoops while a condition is True
breakExits the loop early
range(n)Numbers from 0 to n-1