Python/Python Loop

Python For Loop

Updated on February 10, 2026
1 min read

for Loop in Python

A for loop in Python is used to repeat a block of code for each item in a sequence. Instead of writing the same code again and again, a for loop allows a program to perform repetitive tasks in a clear and structured way.

Whenever a program needs to process multiple values—such as items in a list, characters in a string, or a range of numbers—a for loop is the right tool.

How the for Loop Works

The for loop takes one element at a time from a sequence and executes the loop body for each element.

Syntax:

for item in sequence:
    # code block

Example:

fruits = ["apple", "banana", "orange"]

for fruit in fruits:
    print(fruit)

Using for Loop with range()

The range() function is used to run a loop a specific number of times.

Example:

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

This prints numbers from 1 to 5.

The break Statement

The break statement is used to stop a loop immediately. When Python encounters break, it exits the loop, even if the loop has not finished all iterations.

Example:

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

Output:

1
2
3
4

Real-Life Meaning

Think of searching for a book in a shelf. Once you find it, you stop searching. break does exactly that.

The continue Statement

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

Example:

for i in range(1, 6):
    if i == 3:
        continue
    print(i)

Output:

1
2
4
5

Real-Life Meaning

Think of checking items on a list and skipping damaged ones. You don’t stop the task—you just skip that item.

Nested for Loop

A nested loop means placing one loop inside another. This is useful when working with multi-dimensional data or combinations.

Example:

for i in range(1, 4):
    for j in range(1, 4):
        print(i, j)

Here:

  • The outer loop runs 3 times
  • The inner loop runs 3 times for each outer loop

Real-Life Example of Nested Loop

  • Think of a clock.
  • Hours loop from 1 to 12.
  • Inside each hour, minutes loop from 1 to 60.
  • This is exactly how nested loops work.

Conclusion

The for loop is a powerful tool for repetition in Python. The break statement helps stop loops early, continue allows skipping unwanted iterations, and nested loops enable complex data processing. Together, these features make loops flexible and efficient for real-world programming.