Python: Repeat Tasks with For and While Loops
This tutorial will guide you through using `for` and `while` loops in Python. Loops are fundamental for repeating blocks of code, making them essential for automating tasks and processing data. We’ll explore the differences between these two loop types and how to use them effectively.
Understanding For Loops
A `for` loop is typically used when you know in advance how many times you want to repeat a block of code. It iterates through a sequence (like a list, string, or range) and executes the code block for each item in the sequence.
Example 1: Iterating Through a List
Let’s create a list of fruits and then iterate through it using a `for` loop. We’ll print each fruit to the console.
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
Explanation:
- We initialize a list named `fruits` containing three strings.
- The `for` loop iterates through each element in the `fruits` list.
- In each iteration, the current element (which is assigned to the variable `fruit`) is printed to the console.
Input: `fruits = [“apple”, “banana”, “cherry”]`
Intermediate Values:
- Iteration 1: `fruit` = “apple”, Output: “apple”
- Iteration 2: `fruit` = “banana”, Output: “banana”
- Iteration 3: `fruit` = “cherry”, Output: “cherry”
Output:
apple
banana
cherry
Example 2: Using `range()` with For Loops
The `range()` function generates a sequence of numbers. We can use it to control the number of iterations in a `for` loop.
for i in range(5):
print(i)
Explanation:
- `range(5)` generates a sequence of numbers from 0 to 4 (exclusive of 5).
- The `for` loop iterates through each number in this sequence, assigning it to the variable `i`.
- In each iteration, the value of `i` is printed to the console.
Input: `range(5)`
Intermediate Values:
- Iteration 1: `i` = 0, Output: 0
- Iteration 2: `i` = 1, Output: 1
- Iteration 3: `i` = 2, Output: 2
- Iteration 4: `i` = 3, Output: 3
- Iteration 5: `i` = 4, Output: 4
Output:
0
1
2
3
4
Understanding While Loops
A `while` loop repeats a block of code as long as a specified condition is true. Unlike `for` loops, you don’t typically specify the number of iterations; instead, the loop continues until the condition becomes false.
Example 3: Counting with a While Loop
Let’s create a `while` loop that counts from 1 to 5 and prints the number in each iteration.
count = 1
while count <= 5:
print(count)
count += 1
Explanation:
- We initialize a variable `count` to 1.
- The `while` loop continues as long as `count` is less than or equal to 5.
- In each iteration:
- The value of `count` is printed to the console.
- `count` is incremented by 1.
Input: `count = 1`
Intermediate Values:
- Iteration 1: `count` = 1, Output: 1, `count` becomes 2
- Iteration 2: `count` = 2, Output: 2, `count` becomes 3
- Iteration 3: `count` = 3, Output: 3, `count` becomes 4
- Iteration 4: `count` = 4, Output: 4, `count` becomes 5
- Iteration 5: `count` = 5, Output: 5, `count` becomes 6
Output:
1
2
3
4
5
Common Mistakes and Corrections:
- Infinite Loop: If the condition in a `while` loop never becomes false, the loop will run forever. This happens if you don’t update the variables involved in the condition. Ensure the condition eventually evaluates to `False`.



Leave a Reply