Python For Loops

Welcome to The Coding College, your one-stop destination for mastering Python programming! In this article, we’ll explore Python For Loops, a crucial tool for iterating over sequences and performing repetitive tasks efficiently.

What is a For Loop in Python?

A for loop in Python is used to iterate over a sequence (like a list, tuple, string, or range) and execute a block of code for each element in that sequence.

Why Use For Loops?

For loops are ideal when:

  • You know the number of iterations in advance.
  • You want to process elements of a sequence systematically.
  • You need clean and efficient code for repetitive tasks.

Syntax of a Python For Loop

for variable in sequence:  
    # Code to execute for each element  
  • variable: A temporary variable that stores the current element.
  • sequence: The collection to iterate over (e.g., list, string, range).

Examples of Python For Loops

Example 1: Looping Through a List

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

Output:

apple  
banana  
cherry  

Example 2: Looping Through a String

for letter in "Python":  
    print(letter)  

Output:

P  
y  
t  
h  
o  
n  

Example 3: Using the range() Function

The range() function generates a sequence of numbers.

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

Output:

1  
2  
3  
4  
5  

Example 4: Nested For Loops

for x in range(1, 4):  
    for y in range(1, 4):  
        print(f"({x}, {y})")  

Output:

(1, 1)  
(1, 2)  
(1, 3)  
(2, 1)  
(2, 2)  
(2, 3)  
(3, 1)  
(3, 2)  
(3, 3)  

Special Keywords in For Loops

1. break

The break statement exits the loop prematurely.

for number in range(1, 10):  
    if number == 5:  
        break  
    print(number)  

Output:

1  
2  
3  
4  

2. continue

The continue statement skips the current iteration and moves to the next one.

for number in range(1, 10):  
    if number % 2 == 0:  
        continue  
    print(number)  

Output:

1  
3  
5  
7  
9  

3. else in For Loops

An else block can be used with a for loop to execute code when the loop completes without encountering a break.

for number in range(1, 5):  
    print(number)  
else:  
    print("Loop completed!")  

Output:

1  
2  
3  
4  
Loop completed!  

Practical Applications of For Loops

Example 1: Sum of Numbers

total = 0  
for number in range(1, 11):  
    total += number  
print(f"The sum is: {total}")  

Output:

The sum is: 55  

Example 2: Finding Prime Numbers

for num in range(2, 20):  
    for i in range(2, num):  
        if num % i == 0:  
            break  
    else:  
        print(num)  

Output:

2  
3  
5  
7  
11  
13  
17  
19  

Exercises to Practice Python For Loops

Exercise 1: Squares of Numbers

Write a program using a for loop to print the squares of numbers from 1 to 10.

Exercise 2: Reverse a String

Write a for loop to print a given string in reverse order.

Exercise 3: Multiplication Table

Write a program to generate a multiplication table for a given number.

Exercise 4: Count Vowels in a String

Write a program to count the number of vowels in a given string using a for loop.

Exercise 5: Filter Even Numbers

Write a program to filter even numbers from a given list using a for loop.

Why Learn For Loops with The Coding College?

At The Coding College, we believe in breaking down complex programming concepts into manageable lessons. For loops are a cornerstone of programming logic, and mastering them will help you write efficient, elegant code.

Conclusion

Python for loops are a versatile and essential feature for iterating over sequences. Understanding their syntax, use cases, and practical applications will empower you to tackle a wide range of programming challenges.

Leave a Comment