Flow control

The For loop

A for loop is used to iterate over a sequence (such as a list, tuple, string, or range) and execute a block of code for each element in that sequence. It's a convenient way to loop through a collection of items without needing to manage the loop counter manually (as in a while loop).

1. Basic looping:

for variable in sequence:
    # code block to be executed

  • variable: The loop variable that takes the value of each item in the sequence one by one.
  • sequence: The sequence (like a list, tuple, string, or range) that the loop iterates over.
  • code bloc: The block of code to be executed for each item in the sequence.

Example:

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

  • The list fruits contains three items: "apple", "banana", and "cherry".
  • The for loop iterates over this list, and for each iteration, the variable fruit takes the value of the current item in the list.
  • The print(fruit) statement outputs the value of fruit for each iteration.

Output:

apple
banana
cherry


2. Looping through a range():

The range() function generates a sequence of numbers, which is often used with a for loop to iterate a specific number of times.

Example:

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

  • range(5) generates the numbers 0, 1, 2, 3, 4 (the stop value 5 is excluded).
  • For each iteration, i takes the next value in the range, and the loop prints the value of i.

Output:

0
1
2
3
4

You can also use range(start, stop, step) to customize the range:
- start: Starting number (included).
- stop: Ending number (excluded).
- step: The amount by which the loop variable is incremented.

Example:

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

Output:

2
4
6
8


3. Looping with else:

The for loop can also have an optional else clause. The code in the else block is executed when the loop finishes normally (i.e., it is not terminated by a break statement).

Example:

for i in range(5):
    print(i)
else:
    print("Loop completed!")

Output:

0
1
2
3
4
Loop completed!

If the loop is stopped with break, the else clause will not be executed.


4. When to Use a for Loop:

- When you know the sequence you want to iterate over.
- When you want to repeat a block of code for every item in a sequence.

It's time to take a quiz!

Test your knowledge and see what you've just learned.

What is the basic structure of a for loop in Python?

for variable in sequence: code block
for code block: variable in sequence
do for variable in sequence: code block
for each variable in sequence: code block
Check Answer

How does a for loop iterate over a list in Python?

It accesses elements by index only
It uses a while loop internally
It assigns each element to a variable in each iteration
It requires a counter variable
Check Answer

What does range(5) generate in a for loop?

5 numbers from 1 to 5
5 numbers from 0 to 5
5 numbers from 0 to 4
5 numbers from 1 to 4
Check Answer

When is the else block executed in a for loop?

When the loop iterates more than 10 times
When the loop is terminated by break
When the loop finishes normally
When an exception occurs
Check Answer

When should you use a for loop?

When you know the sequence to iterate over
When the number of iterations is unknown
When you want to break out of a loop
When iterating in reverse order only
Check Answer