In Python, a for loop is a control structure used for iterating over a sequence (or iterable) of items, such as lists, tuples, strings, or other iterable objects. It allows you to perform a set of operations on each item in the sequence, making it a powerful tool for automating repetitive tasks. Let's break down how for loops work and cover the various aspects, including lists, tuples, and nested for loops.

Understanding the basic for loop

A basic for loop in Python follows this syntax:

for item in iterable
# Code to execute for each item

Here, item is a variable that represents each individual item in the iterable during each iteration, and iterable is a collection of items, such as a list, tuple, string, or other iterable objects. The loop executes the block of code inside it for each item in the iterable.

Example with a List
Consider a simple example using a list of fruits:
fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
print(fruit)

In this example, the for loop iterates over each item in the fruits list and prints it. The output will be:

apple
banana
cherry

Iterating over tuples with for loops

Just like lists, for loops can be used with tuples. The syntax remains the same, and you can iterate through the items of a tuple using a for loop. This allows you to process each element in the tuple sequentially.

Example with a Tuple
Here’s an example using a tuple of colors:
colors = ('red', 'green', 'blue')
for color in colors:
print(color)

In this case, the for loop iterates over each item in the colors tuple and prints it. The output will be:

red
green
blue

Working with nested for loops

A nested for loop is a for loop inside another for loop. It's used to iterate over multiple sequences or to create combinations of items from different iterables. Nested loops are particularly useful when working with multi-dimensional data structures, such as lists of lists or matrices.

Example of a Nested For Loops
Consider an example where we use nested for loops to iterate over two lists, numbers and letters, and print combinations of their items:
numbers = [1, 2, 3]
letters = ['a', 'b', 'c']

for number in numbers:
for letter in letters:
print(number, letter)

This demonstrates how nested for loops can be used to generate all possible combinations of items from two lists.

1 a
1 b
1 c
2 a
2 b
2 c
3 a
3 b
3 c

In this example, the outer for loop iterates over the numbers list, and the inner for loop iterates over the letters list for each iteration of the outer loop. The output will be:

Practical use cases of for loops

For loops are versatile and can be used in a variety of scenarios, such as:

  • Processing Data in Lists and Tuples: Iterating over items in lists and tuples to perform operations like calculations, filtering, and transformations.

  • Generating Sequences: Creating sequences of numbers using functions like range().

  • Working with Strings: Iterating over characters in a string to perform text processing tasks.

  • Multi-dimensional Data Structures: Handling nested data structures by using nested for loops.

Example: Summing a List of Numbers
numbers = [10, 20, 30, 40, 50]
total = 0
for number in numbers:
total += number
print("Total sum:", total)

In this example, the for loop iterates over each number in the numbers list and accumulates the sum in the total variable. The final output will be:

Total sum: 150

Quiz Question

Which of the following statements best describes the purpose of a for loop in Python?

Quiz Question

Given the following code, what will be printed to the console?

fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
print(fruit)

Quiz Question

What is the purpose of a nested for loop in Python?