Unit 4: Loops – Repeating Tasks in Python

Unit 4: Loops – Repeating Tasks in Python

Overview

Imagine you have to write “I will practice Python” 100 times. You could copy-paste the line 100 times… or you could tell Python:

“Repeat this line 100 times.”

That’s what loops are for—automating repetition in your code.

Python provides two main types of loops:

       •      for loops: Best for repeating over a known sequence (like a list or a range of numbers)

       •      while loops: Best when you want to repeat something until a condition changes

1. For Loops: Iterating Over a Sequence

Analogy:

Think of a for loop like going through a playlist. You hit play, and the player goes through each song, one by one.

Syntax:

for item in sequence:

    # do something with item

Looping Over a List

fruits = [“apple”, “banana”, “cherry”]

for fruit in fruits:

    print(“I like”, fruit)

Output:

I like apple 

I like banana 

I like cherry

Each time through the loop, fruit holds the next item from the list.

Using range() to Loop a Specific Number of Times

The range() function gives you a sequence of numbers.

for i in range(5):

    print(“Number:”, i)

Output:

Number: 0 

Number: 1 

Number: 2 

Number: 3 

Number: 4

Explanation:

range(5) gives numbers from 0 to 4. It starts at 0 by default and stops before 5.

Using range(start, stop, step)

for i in range(1, 10, 2):

    print(i)

Output:

9

This starts at 1, stops before 10, and increases by 2 each time.

2. While Loops: Repeat Until a Condition is False

Analogy:

A while loop is like a light with a motion sensor: it stays on while it detects movement. As soon as the condition stops being true, the loop ends.

Syntax:

while condition:

    # repeat this block

Simple Example:

count = 0

while count < 5:

    print(“Count is:”, count)

    count += 1

Output:

Count is: 0 

Count is: 1 

Count is: 2 

Count is: 3 

Count is: 4

Explanation:

As long as count < 5, the loop continues. The count += 1 increases the number by 1 each time.

3. Breaking Out of Loops

Sometimes, you want to exit a loop early, even if the loop’s condition is still true.

Using break: Exit Immediately

for i in range(10):

    if i == 3:

        break

    print(i)

Output:

2

Explanation:

As soon as i reaches 3, the loop stops completely.

4. Skipping an Iteration

Using continue: Skip This Turn

for i in range(5):

    if i == 2:

        continue

    print(i)

Output:

4

Explanation:

When i == 2, Python skips the print(i) and jumps to the next iteration.

5. Real-Life Example: Password Checker

correct_password = “python123”

attempt = “”

while attempt != correct_password:

    attempt = input(“Enter password: “)

print(“Access granted!”)

Explanation:

The loop keeps asking for the password until the user types “python123”. Once the input matches, the loop ends and “Access granted!” is printed.

6. Nested Loops: Loops Inside Loops

Example: Multiplication Table

for i in range(1, 4):

    for j in range(1, 4):

        print(i * j, end=” “)

    print()  # Line break

Output:

1 2 3 

2 4 6 

3 6 9

Explanation:

The outer loop runs for each row. The inner loop runs for each column. You get a 3×3 multiplication table.

Conclusion

In this unit, you learned how to repeat tasks using loops:

       •      for loops: Use when looping over items or a known range

       •      while loops: Use when repeating until a condition changes

       •      break and continue: Control the loop flow

       •      range(): Useful for counting or creating number sequences

       •      Nested loops: Use when looping within loops (e.g. tables, grids)

These tools let your programs automate work—no need for copy-paste repetition!

Next up: Unit 5 – Functions: Breaking Down Code. You’ll learn how to organize your programs into reusable, modular blocks. Let’s keep going!

Leave a Comment