1. Why bother with loops?
Imagine you want to print “Hello” five times. You could write it out five times:
print("Hello") print("Hello") print("Hello") print("Hello") print("Hello")
But what about 100 times? Or a million? That is exactly what loops are for: say it once, repeat it as many times as you like. There are two kinds, and the trick is knowing which to reach for.
forloop: use it when you know how many times (or you have a list of things to go through).whileloop: use it when you don’t know how many times, and you just want to keep going until something becomes true.
2. The for loop
A for loop repeats its block once for each item it is given. The indented lines underneath are the body, the part that repeats.
for i in range(5): print("Hello")
That prints “Hello” five times. Two things to notice, exactly like with if:
- The first line ends with a colon
: - The repeating part is indented (4 spaces).
3. Counting with range
range makes a sequence of numbers for the loop to walk through. The variable (often i) holds the current number each time around:
for i in range(1, 4): print(i) # prints 1, then 2, then 3 (4 is the stopping point, not included)
You can also count backwards with a step of -1, handy for a countdown:
for i in range(5, 0, -1): print(i) # prints 5, 4, 3, 2, 1
4. Looping through things
A for loop can also walk through the letters of a word (or items in a list), no numbers needed:
for letter in "cat": print(letter) # prints c, then a, then t
It reads almost like English: “for each letter in cat, print the letter.”
5. The while loop
A while loop keeps repeating as long as a condition is True. Use it when you don’t know how many goes it will take, like asking for a password until the user gets it right:
password = "" while password != "swordfish": password = input("Password? ") print("Welcome in!")
It checks the condition, runs the body, then checks again, over and over, until the condition is finally False. Here it keeps asking while the password is wrong, and only moves on once it’s right.
6. Watch out: infinite loops
A while loop only stops when its condition becomes False. If nothing inside the loop ever changes that condition, it runs forever, an infinite loop:
count = 0 while count < 5: print(count) # oops! count never changes, so this never stops
The fix is to make sure something in the loop moves you towards the exit; here, adding count = count + 1 so count eventually reaches 5:
count = 0 while count < 5: print(count) count = count + 1 # now it counts up and stops at 5
Golden rule: every while loop needs a way to end. Before you run it, ask yourself: “what will eventually make this condition False?”
7. Quick check Part 1 of 3
Five quick questions. This is half of completing the module. The two coding tasks below make up the other half.
Which loop is best when you know exactly how many times to repeat?
What does range(1, 4) give the loop?
When does a while loop keep repeating?
What causes an infinite loop?
What goes at the end of the for / while line, before the indented body?
8. Coding tasks Parts 2 & 3 of 3
Two real programs to write. Each one is worth 25% of the module. Pass both (plus the quick check above) to reach 100%. Press Check to mark each one. Python loads the first time you press Run or Check (5–10 seconds), then it’s instant, and both tasks share the same Python, so it only loads once.
Task 1 of 2 · the for loop
Task 2 of 2 · the while loop
Ctrl+Enter runs your code · Tab inserts 4 spaces · use the Maximise button on either editor for more room.