MWZ

MINDWAREZONE

Nested Loops in python

Loops are one of the most powerful features in Python, allowing developers to execute a block of code repeatedly. Sometimes, a single loop is not enough to solve a problem. In such cases, Python provides nested loops, which are loops placed inside another loop.

Nested loops are commonly used for working with tables, matrices, patterns, and multidimensional data structures. Understanding nested loops is essential for writing efficient and organized Python programs.

What Are Nested Loops?

A nested loop is simply a loop inside another loop. The outer loop controls how many times the inner loop executes.

Syntax

for outer in range(3): 
    for inner in range(2): 
        print("Outer:", outer, "Inner:", inner)
            

Output

Outer: 0 Inner: 0 
Outer: 0 Inner: 1 
Outer: 1 Inner: 0 
Outer: 1 Inner: 1 
Outer: 2 Inner: 0 
Outer: 2 Inner: 1
            

In this example:

  • The outer loop runs 3 times.
  • For each iteration of the outer loop, the inner loop runs 2 times.
  • Total executions = 3 × 2 = 6.

How Nested Loops Work

Think of nested loops as a clock:

  • The outer loop represents the hours.
  • The inner loop represents the minutes.

For every hour, the minutes cycle through completely before the hour changes.

for hour in range(3): 
    for minute in range(4): 
        print(f"Hour {hour}, Minute {minute}")
            

Output

Hour 0, Minute 0
Hour 0, Minute 1
Hour 0, Minute 2
Hour 0, Minute 3
Hour 1, Minute 0
Hour 1, Minute 1
Hour 1, Minute 2
Hour 1, Minute 3
Hour 2, Minute 0
Hour 2, Minute 1
Hour 2, Minute 2
Hour 2, Minute 3
            

Nested for Loops

The most common form of nesting is using a for loop inside another for loop.

Multiplication Table Example

for i in range(1, 6): 
    for j in range(1, 6): 
         print(i * j, end="\t") 
    print()
            

Output

1	2	3	4	5	
2	4	6	8	10	
3	6	9	12	15	
4	8	12	16	20	
5	10	15	20	25	
            

This creates a multiplication table using nested loops.

Nested while Loops

Nested loops can also be created using while statements.

i = 1 
while i <= 3: 
    j = 1 
    while j <= 3: 
        print(i, j) 
        j += 1 
    i += 1
            

Output

1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
            

Pattern Printing with Nested Loops

Pattern generation is one of the most popular uses of nested loops.

Star Pattern

for i in range(5): 
    for j in range(i + 1):
        print("*", end="") 
    print()
            


Output

*
**
***
****
*****
            

Square Pattern

for i in range(4): 
    for j in range(4): 
        print("*", end=" ") 
    print()
            

Output

* * * * 
* * * * 
* * * * 
* * * *
            

Nested loops are useful when dealing with multidimensional lists.

Real-World Applications of Nested Loops

Nested loops are widely used in:

  1. Matrix operations
  2. Game development
  3. Data analysis
  4. Image processing
  5. Pattern generation
  6. Searching and sorting algorithms
  7. Spreadsheet-like data manipulation