Loops in Python: Types, Examples, and Usage

← PrevNext →

Loops are one of the most important concepts in Python programming. They allow you to execute a block of code multiple times without writing the same statements repeatedly. In this beginner-friendly guide, you will learn about different types of loops in Python, including for loops, while loops, break, continue, nested loops, and the range() function with practical examples.

What Are Loops in Python?

Loops are used to execute a block of code repeatedly as long as a specific condition is satisfied or for each item in a sequence.

For example, if you want to print numbers from 1 to 10, you do not need to write ten separate print statements. You can use a loop to perform this task.

Python provides two main types of loops:

Let's understand both with examples.

1. For Loop in Python

A for loop is used to iterate over a sequence, such as a list, tuple, string, or range of numbers.

Syntax

for variable in sequence:
statements

Example

for i in range(5):
print(i)

Output

0
1
2
3
4

The range(5) function generates numbers from 0 to 4.

Using for Loop with a List

You can use a for loop to access each item in a list.

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

for fruit in fruits:
    print(fruit)

Output

Apple
Banana
Mango

Using range() Function

The range() function is commonly used with for loops. It supports three arguments,

Example

for i in range(1, 11, 2):
print(i)

Output

1
3
5
7
9

2. While Loop in Python

A while loop executes a block of code repeatedly as long as its condition is True.

Syntax

while condition:
statements

Example

i = 1

while i <= 5:
    print(i)
    i += 1

Output

1
2
3
4
5

In this example, the loop continues until i becomes greater than 5. It is important to update the loop variable to avoid an infinite loop.

3. Break Statement

The break statement is used to terminate a loop immediately when a particular condition is satisfied.

Example

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

    print(i)

Output

1
2
3
4

When i becomes 5, the loop stops.

4. Continue Statement

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

Example

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

    print(i)

Output

1
2
4
5

Here, the number 3 is skipped.

5. Nested Loops in Python

A nested loop means using one loop inside another loop. The inner loop executes completely for each iteration of the outer loop.

Example

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

Nested loops are useful for working with tables, patterns, and multidimensional data.

6. Else with Loops

Python allows you to use an else block with both for and while loops. The else block executes when the loop finishes normally without being terminated by a break statement.

Example

for i in range(3):
    print(i)
else:
    print("Loop completed")

Output

0
1
2
Loop completed

Difference Between for and while Loops

For Loop While Loop
Used to iterate over a sequence. Used to repeat while a condition is True.
Often suitable when the number of iterations is known. Often suitable when repetitions depend on a condition.
Commonly used with range(). Requires a condition.
Iterates over items automatically. The condition must eventually become False to stop.

Conclusion

Loops are an important concept in Python because they reduce repetitive code and make programs more efficient.

Python provides for loops and while loops for different situations. You should also understand break, continue, nested loops, and else blocks to control loop execution effectively. Practicing these concepts with simple programs will help you build a strong foundation in Python programming.

← PreviousNext →