A loop lets you repeat a block of code without writing it out multiple times. Python has two loop types: `for` loops, which repeat a fixed number of times or over a known sequence, and `while` loops, which repeat as long as a condition stays true.
Use a `for` loop when you know what you're iterating over — a list of students, a range of numbers, the characters in a string. Example: `for student in students: print(student)` runs once per item in the list, in order, and stops automatically when the list is exhausted.
`range(n)` is commonly paired with `for` when you need a fixed count rather than an existing list: `for i in range(5): print(i)` prints 0 through 4. A frequent beginner mistake is expecting `range(5)` to include 5 — it doesn't; it stops one before the given number.
Use a `while` loop when you don't know in advance how many repetitions you need — only the condition that should stop it. Example: `while attempts < 3: attempts += 1` keeps running until the condition becomes false. The most common bug here is forgetting to update the variable inside the loop (like `attempts += 1`), which creates an infinite loop that never stops.
`break` exits a loop immediately, useful when you find what you're looking for and don't need to keep checking. `continue` skips the rest of the current iteration and moves to the next one, useful for skipping items that don't meet a condition without exiting the whole loop.
A good rule of thumb: if you can say "for each item in this collection," reach for `for`. If you can only say "keep going until this becomes true," reach for `while`.