Table of Contents
Repeating Yourself vs. Using Loops
Imagine you want to print the numbers 1 to 10. Without loops, you would have to write:
print(1)
print(2)
print(3)
print(4)
print(5)
print(6)
print(7)
print(8)
print(9)
print(10)This works, but it is:
- Boring to write
- Hard to change (what if you want 1 to 100 instead?)
- Easy to make mistakes
Loops exist so you can tell Python:
“Do this action many times, following this pattern.”
You write the instruction once, and the loop repeats it for you.
Reducing Repetition in Code
Loops help remove “copy‑paste” style code.
Example task: print “Hello!” 5 times.
Without a loop:
print("Hello!")
print("Hello!")
print("Hello!")
print("Hello!")
print("Hello!")With a loop (conceptually):
repeat 5 times:
print("Hello!")The exact Python syntax will be shown in later sections, but the idea is:
- You describe what to repeat.
- The loop takes care of how many times and in what order.
Making Changes Easier
Suppose you first print numbers from 1 to 10, then later decide to print from 1 to 100.
With repeated code, you must:
- Add 90 more lines, or
- Edit lots of lines manually
With a loop, you usually only change one number (for example, the “end” of a range), and the loop automatically handles the new amount of work.
Loops make your programs:
- Easier to update
- Less error‑prone
- Shorter and clearer
Handling Large or Unknown Amounts of Work
Sometimes you don’t even know in advance how many times something should happen.
Examples:
- Keep asking the user for input until they type “quit”.
- Keep reading lines from a file until you reach the end.
- Keep moving a game character while it still has health.
You cannot realistically write:
ask user...
ask user...
ask user...hundreds or thousands of times.
Loops solve this by repeating actions based on conditions (like “until user types quit” or “while there are more lines in the file”). You describe when to stop, instead of manually writing all repetitions.
Working with Collections of Data
A very common pattern in programs is:
“Do something with every item in a list.”
Examples:
- Print each name in a list of students
- Add up all numbers in a list of scores
- Check every password in a list for strength
- Send a message to each contact in an address book
Without loops, you might do:
print(students[0])
print(students[1])
print(students[2])
# ...But that only works if you know exactly how many students there are, and it is still repetitive.
With loops, you can say:
“For each student in this list, do something.”
This works no matter if there are 3 students or 3000.
Automating Repeated Steps
Loops are a key tool for automation—getting the computer to do boring, repetitive tasks for you.
Typical automated tasks that use loops:
- Renaming many files in a folder
- Processing many images (resizing, watermarking, etc.)
- Checking many websites for updates
- Sending emails to a list of addresses
- Simulating something many times (e.g., rolling dice 1000 times)
You write the steps once, and the loop runs those steps on each item or for each repetition.
Building Simulations and Games
Loops are essential in games and simulations, for example:
- Keep updating the game screen every frame.
- For each enemy, move it and check for collisions.
- For each turn in a board game, ask the next player for a move.
In many games, there is a loop that keeps the game running:
“While the game is not over, keep updating and drawing the game.”
Without loops, you couldn’t easily keep the game running smoothly or respond continuously to user actions.
Saving Time and Reducing Mistakes
Using loops has two big practical benefits:
- You write less code
- Less typing
- Less “copy-paste”
- Faster to create new programs
- You reduce bugs
- When you fix the logic inside the loop, it’s fixed for every repetition
- You don’t have to fix the same mistake in many duplicated lines
This is one of the reasons loops are often taught early: they make your code better and your life easier very quickly.
Thinking in Patterns
Beyond saving time, loops help you think differently about problems.
You start to ask:
- “What action should happen repeatedly?”
- “What changes each time I repeat it?”
- “When should the repetition stop?”
This way of thinking—finding patterns and repetition—is a core programming skill.
In the rest of this chapter, you will see:
- How to write loops that repeat while something is true
- How to write loops that go through a sequence of values
- How to control how many times a loop runs
For now, remember: loops are useful whenever you want to do the same kind of work multiple times, especially when the amount of work is large, changing, or unknown.