Lesson 5: Python Loops

⏱️ 3 min read

Imagine having to write the exact same line of code one hundred times. It would be tedious, visually messy, and a complete waste of your time. This is exactly the problem loops solve.

Loops allow you to automate repetitive tasks by telling Python to run a specific block of code over and over again. Instead of repeating yourself, you write the code once and let the computer do the heavy lifting.

In Python, there are two main tools for this: the for loop and the while loop. Let’s look at how and when to use each one.

1. The for Loop (The Iteration Master)

You use a for loop when you know exactly how many times you want to run a block of code, or when you want to go through a collection of items (like a list) one by one.

Think of it like dealing cards in a game: you hand out one card to every player until the deck is empty.

Example: Looping through a list

fruits = ["apple", "banana", "cherry"]

# Python grabs the first item, assigns it to the variable 'fruit', and runs the code.
# Then it goes back, grabs the next item, and repeats.
for fruit in fruits:
    print("I love eating " + fruit)

Example: Looping a specific number of times using range() If you just want to repeat an action 5 times, you can use Python’s built-in range() function.

# range(5) generates numbers from 0 up to 4
for number in range(5):
    print("This is loop iteration number", number)

2. The while Loop (The Condition Checker)

You use a while loop when you do not know beforehand how many times the loop needs to run. Instead of counting, a while loop keeps running continuously as long as a specific condition remains True.

Think of it like eating dinner: you keep taking bites while you are still hungry. Once you are full (the condition becomes False), you stop.

Example: A simple countdown

countdown = 5

while countdown > 0:
    print(countdown)
    # Crucial step: We must decrease the number so the loop eventually ends
    countdown -= 1 

print("Liftoff!")

⚠️ The Infinite Loop Warning: With while loops, you must always make sure the condition will eventually become False (like subtracting 1 from the countdown above). If the condition never changes, the loop will run forever, freezing your program!

3. Taking Control: break and continue

Sometimes you need to interrupt a loop before it naturally finishes. Python gives you two special keywords to control the flow from inside the loop:

  • break: Instantly stops the entire loop and breaks out of it completely.
  • continue: Stops the current iteration and jumps straight back to the top of the loop for the next round.

Example: Using break and continue

for number in range(1, 10):
    
    if number == 3:
        # Skip the number 3
        continue 
        
    if number == 7:
        # Stop the entire loop when it hits 7
        break 
        
    print(number)

# The output will be: 1, 2, 4, 5, 6

Ready to continue?

Move on to the next lesson to keep learning.

Continue: Lesson 6: Python Functions

Tutorial: Python