for loop repeats a set number of timesrange() counts for youwhile loop repeats until a condition changesWithout loops, printing the numbers 1 to 5 takes five lines:
print(1)
print(2)
print(3)
print(4)
print(5)
Output:
1
2
3
4
5
That works, but it does not scale. Printing 1 to 100 would take a
hundred lines, and changing the plan means rewriting all of them. A
loop repeats a block of code for you, so you write the action
once and say how many times to do it.
A for loop repeats its block once for each item in a sequence.
The simplest sequence comes from range(), which counts:
for number in range(5):
print(number)
Output:
0
1
2
3
4
range(5) produces the numbers 0, 1, 2, 3, 4 — five numbers, starting
at 0. That zero start surprises everyone once, then becomes habit:range(5) means “five times”, and the counting variable takes each
value in turn. The variable name is yours to choose — number here
could just as well be i or count.
range() can also start and stop where you say:
for number in range(1, 6):
print(number)
Output:
1
2
3
4
5
range(1, 6) counts from 1 up to — but not including — 6. The stop
value is always one past the last number you get. Remember that, andrange() will never confuse you again.
A loop can also walk through a string, one character at a time:
for letter in "cat":
print(letter)
Output:
c
a
t
Same shape as before: “for each thing in this sequence, do the
indented block.” The sequence just happens to be characters instead
of numbers.
A while loop repeats its block as long as its condition stays
true, and stops the moment it becomes false:
count = 1
while count <= 5:
print(count)
count = count + 1
Output:
1
2
3
4
5
Walk through it: count starts at 1, which is <= 5, so the block
prints 1 and raises count to 2. Still <= 5, so it prints 2, raises
to 3 — and so on until count reaches 6, when the condition is false
and the loop stops. That last line, count = count + 1, is doing the
same update you met in Lesson 4. Without it, nothing would ever
change, which brings us to the mistake.
A while loop whose condition never becomes false runs forever — an
infinite loop. It usually comes from forgetting the update line:
count = 1
while count <= 5:
print(count)
This prints 1 forever, because count never changes. If you ever
run a program that prints the same thing endlessly, stop it (in IDLE,
use Shell → Restart Shell, or close the shell window) and check
whether your loop changes the thing its condition tests. A for loop
cannot run forever this way, because range() always runs out — one
more reason beginners often prefer for when the number of repeats
is known in advance.
Press play on any slide and the lesson plays on by itself from there. You can still click Next or click a slide to move on.
Download the Lesson 10 slides (PowerPoint, with narration)
Download the Lesson 10 slides (PDF)
for loop with range() to print the numbers 1 to 10.while loop to count down from 5 to 1, then print "Blastfor loop to print each letter of your name on its own line.range(3) produces before running it, then check.One loop prints the numbers 1 up to your number. In the lesson, range(1, 6) gives 1 to 5; here the end is your number + 1, so the loop stops at your number. Change the number and run again.
Try this: Change the number to 3 and press Run again — only 1, 2 and 3 print.
Press Run to see the output here.
Save this as lesson10-practice.py and run it with F5 in IDLE.
# Lesson 10: Loops — Repeating Without Retyping
# WHAT TO DO: A for loop repeats its indented line once per number in
# range(). range(1, 11) counts from 1 up to, but not including, 11 —
# that is, 1 to 10. This loop stops too early. Fix line 9: change
# range(1, 2) to range(1, 11).
# HINT: The stop value in range() is one past the last number you want.
# Run with F5 in IDLE. You are done when it prints 1 to 10, one per line.
for number in range(1, 2): # <-- CHANGE THIS LINE: count from 1 to 10
print(number)
for repeats once per item in a sequence; range(n) counts 0 to n−1.range(start, stop) counts from start up to, but not including, stop.while repeats while its condition is true; the block must change something the condition tests.while loop that never changes its condition runs forever.Next lesson: Drawing with Turtle.
range(5) produce, and why does it start at 0?range(1, 6) produce, and why does it stop at 5?while loop decide when to stop?Answer key: 1. Repeating an action without retyping it — write once, run many times. 2. 0, 1, 2, 3, 4 — range() counts from 0 by default. 3. 1, 2, 3, 4, 5 — the stop value is one past the last number produced. 4. It repeats while its condition is true and stops the moment it becomes false. 5. A condition that never becomes false, usually because the loop never updates the tested value; stop it with Shell → Restart Shell (or close the shell window).