Python For Loops

In this article, we will delve into the topic of Python for loops. A for loop is an important construct in Python, which is used to execute a block of code repeatedly. It is widely used in Python programming, and therefore, it is essential to have a good understanding of it.

What is a for loop?

A for loop is a type of loop that is used to iterate over a sequence. The sequence can be a list, tuple, set, dictionary, or any other iterable object. The basic syntax of a for loop is:

for item in sequence:
    # do something with item

Here, item is a variable that takes on each value in the sequence, one by one. The block of code inside the loop is executed once for each value of item.

Example of a for loop

Let's take a simple example to understand how a for loop works:

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

Output:

apple
banana
cherry

Here, we have a list of fruits. We use a for loop to iterate over the list, and print each fruit on a new line.

Range function

The range() function is commonly used in for loops. It generates a sequence of numbers, which can be used as the sequence in a for loop. The syntax of the range() function is:

range(start, stop, step)

Here, start is the starting number of the sequence (default is 0), stop is the ending number of the sequence (not included), and step is the step size (default is 1).

Example of range function in a for loop

Let's take an example to see how the range() function can be used in a for loop:

for i in range(1, 6):
    print(i)

Output:

1
2
3
4
5

Here, we use the range() function to generate a sequence of numbers from 1 to 5. We then use a for loop to iterate over the sequence and print each number on a new line.

Nested for loops

A for loop can also be nested inside another for loop. This is useful when we need to iterate over multiple sequences simultaneously. The syntax for a nested for loop is:

for item1 in sequence1:
    for item2 in sequence2:
        # do something with item1 and item2

Here, the inner for loop is executed once for each value of the outer for loop.

Example of nested for loop

Let's take an example to see how a nested for loop works:

adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
for a in adj:
    for f in fruits:
        print(a, f)

Output:

red apple
red banana
red cherry
big apple
big banana
big cherry
tasty apple
tasty banana
tasty cherry

Here, we have two lists, adj and fruits. We use a nested for loop to iterate over both lists simultaneously, and print each combination of an adjective and a fruit on a new line.

Conclusion

In conclusion, we have learned about the for loop in Python, which is an important construct used to iterate over a sequence. We have seen how the range function can be used to generate a sequence of numbers, and how nested for loops can be used to iterate over multiple sequences simultaneously. With this knowledge, you

Practice Your Knowledge

What are the primary uses of 'for' loops in Python according to the article from w3docs?

Quiz Time: Test Your Skills!

Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.

Do you find this helpful?