← Python Programming learning pathPython Programming · Module 5 of 10
Loops: for and while
Repeat work with for loops over sequences and while loops on conditions.
📖 Key ideas
- for item in list: runs once per item.
- range(5) gives 0, 1, 2, 3, 4.
- while condition: repeats while the condition stays True.
- break stops a loop; continue skips to the next round.
- 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