Python For Loop - Syntax and Examples

For Loop

A for loop in Python allows you to iterate over a sequence—like a list, string, tuple, or range — and execute a block of code for each item in that sequence. Unlike traditional C-style for-loops, Python takes a more readable and elegant approach.

Basic Syntax

for variable in sequence:
    # block of code

Here, variable takes the value of each item in sequence, one by one, and executes the indented block.

Example 1: Looping Over a List

Each item in the list is printed on a new line. The loop automatically stops after the last element.

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

Example 2: Using Range with For Loop

range(5) generates numbers from 0 to 4. If you need to include 5, use range(6). Reference: range() bulitin function.

for i in range(5):
    print("Step", i)

Output:

Step 0
Step 1
Step 2
Step 3
Step 4

Example 3: Looping Through a String

Strings are iterable in Python—each character is treated as an element in the sequence.

for char in "Python":
    print(char)

Output:

P
y
t
h
o
n

Example 4: Using range(start, stop, step)

This version of range lets you control the start, stop, and increment values. It’s great for custom numeric patterns.

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

Output:

2
4
6
8

Example 5: Loop with else Clause

The else clause runs after the loop completes normally. It’s skipped if the loop is exited via break.

for i in range(3):
    print("Counting", i)
else:
    print("Loop finished!")

Output:

Counting 0
Counting 1
Counting 2
Loop finished!

Example 6: Nested For Loop

Nested loops let you work with multidimensional data or matrix-style problems.

for i in range(2):
    for j in range(2):
        print(f"i={i}, j={j}")

Output:

i=0, j=0
i=0, j=1
i=1, j=0
i=1, j=1

Example 7: Breaking a Loop

The loop stops when num equals 5. Use break carefully—it ends the loop prematurely.

for num in range(10):
    if num == 5:
        break
    print(num)

Output:

0
1
2
3
4

Example 8: Skipping Iterations with continue

continue skips the current iteration and jumps to the next one.

for num in range(5):
    if num == 2:
        continue
    print(num)

Output:

0
1
3
4

Example: Using enumerate()

The enumerate() function is used to loop through the list names while keeping track of both the index and the value at the same time. In each iteration, index holds the position (0, 1, 2) and name holds the corresponding value from the list. This allows printing both index and name together.

names = ["Alice", "Bob", "Charlie"]
for index, name in enumerate(names):
    print(index, name)

Output:

0 Alice
1 Bob
2 Charlie

Tips and Gotchas

  • Always check indentation. Python uses indentation to define loop blocks.
  • Modifying the sequence you're iterating over can lead to unpredictable behavior.
  • Use range(len(list)) only if you need index values. Otherwise, loop directly over items.
  • Prefer enumerate() if you need both index and item.