Introduction to Loops

What are Loops?

Loops are a way to repeat code multiple times. Instead of writing the same code over and over, you can use a loop to execute it as many times as needed. Think of a loop like a playlist that repeats your favorite songs!

Why Use Loops?

Loops help you: - Avoid repetition: Write code once, run it many times - Process collections: Work with lists, strings, and other data structures - Automate tasks: Perform actions repeatedly without manual intervention - Save time: Handle large amounts of data efficiently

Real-World Examples

Think about everyday tasks that repeat: - Counting from 1 to 10 - Printing each name in a list - Checking every item in a shopping cart - Sending an email to each person in a contact list

All of these can be done with loops!

Types of Loops in Python

Python has two main types of loops:

  1. For loops: Used when you know how many times to repeat (or when iterating over a collection)
  2. While loops: Used when you want to repeat until a condition becomes false

A Simple Example

Here's what code looks like without loops:

print("Hello")
print("Hello")
print("Hello")
print("Hello")
print("Hello")

And here's the same thing with a loop:

for i in range(5):
    print("Hello")

Much cleaner! The loop runs 5 times and prints "Hello" each time.

Understanding Loop Structure

Every loop has these parts:

  1. Loop keyword: for or while
  2. Loop variable: A variable that changes with each iteration
  3. Loop body: The code that repeats (indented)
  4. Colon: Indicates the start of the loop body
for item in collection:  # Loop header
    print(item)          # Loop body (indented)

The Indentation Rule

In Python, indentation is crucial for loops. The code inside the loop must be indented:

# Correct
for i in range(3):
    print(i)  # This is inside the loop

# Wrong
for i in range(3):
print(i)  # This will cause an error - not indented!

Your First Loop

Let's create a simple loop that counts from 1 to 5:

for number in [1, 2, 3, 4, 5]:
    print(f"Number: {number}")

This will output:

Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

Common Mistakes to Avoid

  1. Forgetting the colon: Always include : after the loop header ```python # Wrong for i in range(5) print(i)

# Correct for i in range(5): print(i) ```

  1. Wrong indentation: Code inside the loop must be indented ```python # Wrong for i in range(3): print(i)

# Correct for i in range(3): print(i) ```

  1. Infinite loops: Be careful with while loops - they can run forever if the condition never becomes false!

Try It Yourself!

Create a loop that prints "Learning Python!" three times:

for i in range(3):
    print("Learning Python!")

What's Next?

Now that you understand what loops are and why they're useful, we'll learn about for loops - the most common type of loop in Python!

Introduction to Loops

points

You earned points! 🎉 Keep practicing! 💪

Sign up to save your points and access 10,000+ exercises Sign up to track your progress and access 10,000+ exercises

Daily Limit Reached

🔥 exercises left — Upgrade for more!

This module requires

Try the first module of this course for free, then upgrade to unlock all modules and exercises.

No exercise selected

Click "New Exercise" to begin

Generating exercise...

Syntax Syntax OK Syntax off

Write a short answer to the question above (2-5 sentences).

Score
+ /
Base /10
Bonus
×
Total
+

Feedback

Tip

Your Solution

Correct Solution