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
4That 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
5The 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
tSame 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
5When 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.