What is For Loops in Python

A for loop in Python is used for iterating over a sequence (like a list, tuple, string, or range). It automates repetitive tasks by executing a block of code multiple times.

Basic Syntax of a For Loop

for variable in sequence:
    # Code to execute in each iteration
  • The loop runs for each item in the sequence.
  • The variable takes the value of the current item in the sequence.

Example 1: Printing Numbers from 1 to 5

for num in range(1, 6):  # range(start, stop)
    print(num)

Output:

1  
2  
3  
4  
5  

Explanation:

  • range(1, 6) generates numbers from 1 to 5.
  • The loop prints each number in the sequence.

Example 2: Iterating Over a List

fruits = ["Apple", "Banana", "Cherry"]

for fruit in fruits:
    print(fruit)

Output:

Apple  
Banana  
Cherry  

Explanation:

  • The loop iterates over each item in the list fruits.

Example 3: Iterating Over a String

word = "Python"

for letter in word:
    print(letter)

Output:

P  
y  
t  
h  
o  
n  

Explanation:

  • Each letter in “Python” is printed separately.

Example 4: Sum of First N Natural Numbers

n = int(input("Enter a number: "))
total = 0

for i in range(1, n + 1):
    total += i

print("Sum of first", n, "natural numbers is:", total)

Output:

Enter a number: 5  
Sum of first 5 natural numbers is: 15  

Explanation:

  • The loop runs from 1 to n, adding each number to total.

Example 5: Using the Break Statement

for num in range(1, 10):
    if num == 5:
        print("Loop stopped at:", num)
        break
    print(num)

Explanation:

  • The loop stops when num == 5 due to the break statement.

Example 6: Using the Continue Statement

for num in range(1, 6):
    if num == 3:
        continue  # Skip number 3
    print(num)

Explanation:

  • The continue statement skips printing 3 and moves to the next iteration.

Example 7: Nested For Loops (Multiplication Table)

for i in range(1, 4):  # Outer loop
    for j in range(1, 4):  # Inner loop
        print(f"{i} x {j} = {i * j}")
    print("------")  # Separator

Output:

1 x 1 = 1  
1 x 2 = 2  
1 x 3 = 3  
------  
2 x 1 = 2  
2 x 2 = 4  
2 x 3 = 6  
------  
3 x 1 = 3  
3 x 2 = 6  
3 x 3 = 9  
------  

Explanation:

  • The outer loop runs for numbers 1 to 3.
  • The inner loop calculates multiplication for each number.

Example 8: Iterating with the Enumerate Function

colors = ["Red", "Green", "Blue"]

for index, color in enumerate(colors):
    print(f"Index {index}: {color}")

Output:

Index 0: Red  
Index 1: Green  
Index 2: Blue  

Explanation:

  • enumerate() provides both the index and the value from the list.

Example 9: Using Else with a For Loop

for num in range(1, 4):
    print(num)
else:
    print("Loop completed successfully!")

Output:

1  
2  
3  
Loop completed successfully!  

Explanation:

  • The else block runs after the loop completes normally.

Python’s for loop is a powerful tool for iterating over sequences, automating repetitive tasks, and improving code efficiency. From simple iterations to nested loops and break/continue controls, mastering for loops is essential for writing clean and efficient Python programs.

Keep Learning 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *