Thuta Learning
BasicProgrammingbeginner

Python Loops: for, while, break and continue

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Loop over an iterable with for
  • Write a while condition
  • Tell break and continue apart

Let's break it down simply

A Python for loop steps through the items of a sequence or iterable one by one. Use range() when you want to repeat a fixed number of times, and while when you want to keep going as long as a condition holds. break stops the loop entirely, while continue skips to the next iteration.

python
scores = [68, 42, 91, 77]

for index, score in enumerate(scores, start=1):
    if score < 50:
        continue
    print(f"{index}: {score}")

attempts = 3
while attempts > 0:
    print(f"Attempts left: {attempts}")
    attempts -= 1
You should see
1: 68
3: 91
4: 77
Attempts left: 3
Attempts left: 2
Attempts left: 1

Try it yourself

Write a loop from 1 to 30 that prints Fizz for multiples of 3 and Buzz for multiples of 5.

Python Control FlowPython Software Foundation

Easy traps

  • Creating an infinite loop because the while condition never changes
  • Using range(len(...)) when you don't actually need the list index

Exercise

Write a loop from 1 to 30 that prints Fizz for multiples of 3 and Buzz for multiples of 5.

You'll know it worked when: 1: 68 3: 91 4: 77 Attempts left: 3 Attempts left: 2 Attempts left: 1

Python Loops: for, while, break and continue | Thuta Learning