Lesson 10: Loops: Repeating Without Retyping


What you'll learn in this lesson


Why loops exist

Without 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.


For loops and range()

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, and range() 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.


While loops: repeat until something changes

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.


The one loop mistake every beginner makes

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.


Hands-on exercise

  1. Use a for loop with range() to print the numbers 1 to 10.
  2. Use a while loop to count down from 5 to 1, then print "Blast off!" after the loop.
  3. Use a for loop to print each letter of your name on its own line.
  4. Predict what range(3) produces before running it, then check.

Quick quiz

  1. What problem does a loop solve?
  2. What numbers does range(5) produce, and why does it start at 0?
  3. What does range(1, 6) produce, and why does it stop at 5?
  4. How does a while loop decide when to stop?
  5. What causes an infinite loop, and how do you stop one in IDLE?

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).


Key takeaways

Next lesson: Drawing with Turtle.


Slides

Work through the slides with Helio's narration, or download them:

Open the slidesDownload PowerPointDownload PDF

Try it yourself

Run the lesson's example with real Python, right in your browser (Python loads from the internet, so you need to be online).

Open the demo

Practice file

Download lesson10-practice.pyDownload the solution

# 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)

Save the file as lesson10-practice.py and run it with F5 in IDLE. On a Mac, if F5 does nothing, use Run, then Run Module, from the menu.

Check yourself

Five quick questions on this lesson. Take the Lesson 10 quiz

Want graded quizzes and progress tracking?

Enroll free in the full course — same 17 lessons, with your progress saved. Free, no payment.