Loops: Repeating Without Retyping

 

What you’ll learn in this lesson

  • What a loop is, and why programmers love them
  • How a for loop repeats a set number of times
  • How range() counts for you
  • How a while loop repeats until a condition changes
  • How to avoid the one loop mistake every beginner makes

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.


Loops: Repeating Without Retyping

Python for Absolute Beginners · Lesson 10

What you’ll learn

  • What a loop is, and why programmers love them
  • How a for loop repeats a set number of times
  • How range() counts for you
  • How a while loop repeats until a condition changes
  • How to avoid the one loop mistake every beginner makes
1 / 15

Why loops exist

  • A loop repeats a block of code for you, so you write the action once and say how many times to do it
  • Printing 1 to 100 without a loop would take a hundred lines — and changing the plan means rewriting all of them
2 / 15

Without a loop: five lines for 1 to 5

print(1)
print(2)
print(3)
print(4)
print(5)

Output:
1
2
3
4
5

That works, but it does not scale.

3 / 15

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
  • range(5) produces 0, 1, 2, 3, 4 — five numbers, starting at 0
  • range(5) means “five times”, and the counting variable takes each value in turn
  • range(start, stop) counts from start up to, but not including, stop
4 / 15

A for loop with range()

for number in range(5):
    print(number)

Output:
0
1
2
3
4

That zero start surprises everyone once, then becomes habit.

5 / 15

Range with a start and stop

for number in range(1, 6):
    print(number)

Output:
1
2
3
4
5

The stop value is always one past the last number you get.

6 / 15

A loop through a string

for letter in "cat":
    print(letter)

Output:
c
a
t

Same shape: “for each thing in this sequence, do the indented block” — here the sequence is characters.

7 / 15

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
  • The block must change something the condition tests — otherwise nothing ever changes
8 / 15

A while loop in code

count = 1
while count <= 5:
    print(count)
    count = count + 1

Output:
1
2
3
4
5

When count reaches 6, the condition is false and the loop stops.

9 / 15

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, so the tested value never changes
  • If a program prints the same thing endlessly, stop it — in IDLE, use Shell → Restart Shell, or close the shell window
  • A for loop cannot run forever this way, because range() always runs out
10 / 15

An infinite loop (do not run this as-is)

count = 1
while count <= 5:
    print(count)

This prints 1 forever, because count never changes.

11 / 15

Hands-on exercise

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

Key takeaways

  • A loop repeats an indented block so you write an action once
  • 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
  • A while loop that never changes its condition runs forever
13 / 15

Take the quiz

Take the Lesson 10 quiz — 5 quick questions to check what you learned.

14 / 15

Next lesson

Drawing with Turtle

15 / 15

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)

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.

Lesson 10: Loops: Repeating Without Retyping

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.

Loading Python… this can take a few seconds the first time. Please wait — the Run button will become available when Python is ready.

Try this: Change the number to 3 and press Run again — only 1, 2 and 3 print.

The code that runs



Output

Press Run to see the output here.

Practice file

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)

Key takeaways

  • A loop repeats an indented block so you write an action once.
  • 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.
  • A while loop that never changes its condition runs forever.

Next lesson: Drawing with Turtle.

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