No history yet

Introduction to Loops

The Power of Repetition

In programming, you often need to perform the same action multiple times. You could copy and paste code, but that's inefficient and prone to errors. A much better way is to use a loop. A loop is a control structure that repeats a block of code as long as a certain condition is met.

Think of it like this: if you had to sign your name 100 times, you wouldn't write out 100 separate instructions. You'd grab a pen and simply repeat the action of signing. Loops are the programming equivalent of that—a way to automate repetitive tasks efficiently. Python provides two main types of loops to handle this: for loops and while loops.

Loops in Python are essential for handling repetitive tasks efficiently.

The for Loop

The for loop is used for iterating over a sequence, like a list, a tuple, a dictionary, a set, or a string. For each item in the sequence, the for loop executes a block of code. It's perfect when you know exactly how many times you want to repeat an action—once for every item in your collection.

iteration

noun

The process of repeating a set of instructions. Each single pass through the set of instructions is one iteration.

Here’s a basic for loop that prints each fruit from a list:

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

# For each item in the 'fruits' list, assign it to the variable 'fruit' and run the code block
for fruit in fruits:
    print(fruit)

# Output:
# apple
# banana
# cherry

You can also use the range() function to generate a sequence of numbers. This is useful when you want to run a loop a specific number of times.

# This loop will run 3 times, for numbers 0, 1, and 2
for i in range(3):
    print(f"This is loop number {i + 1}")

# Output:
# This is loop number 1
# This is loop number 2
# This is loop number 3

The while Loop

The while loop is different. Instead of iterating over a sequence, it executes a block of code as long as a specified condition is True. You use a while loop when you don't know beforehand how many times the loop needs to run. The loop continues until the condition becomes False.

It's crucial to ensure that something inside the loop eventually changes the condition to False. Here is an example of a countdown:

countdown = 3

# Keep looping as long as 'countdown' is greater than 0
while countdown > 0:
    print(f"{countdown}...")
    countdown = countdown - 1 # Decrease countdown, so the loop eventually ends

print("Liftoff!")

# Output:
# 3...
# 2...
# 1...
# Liftoff!

Use a for loop when you have a definite number of iterations (e.g., for every item in a list). Use a while loop when the number of iterations is unknown and depends on a condition being met.

Quiz Questions 1/5

What is the primary purpose of using a loop in programming?

Quiz Questions 2/5

Which type of loop is most suitable when you want to iterate over every item in a known sequence, like a list of names?

Loops are fundamental to programming. They help you write concise, powerful code that can handle repetitive tasks without breaking a sweat. Mastering them is a key step in becoming a proficient Python developer.