๐Ÿงฑ Python Nested Loops โ€” Loops Inside Loops

Introduction ๐ŸŒŸ

A nested loop means placing one loop inside another. The inner loop runs completely **for each iteration** of the outer loop. Nested loops are commonly used in pattern printing, matrix operations, working with 2D lists, and performing repeated tasks within repeated tasks.

Note

๐Ÿ’ก A nested loop = Outer loop ร— Inner loop executions.

1. Basic Nested For Loop ๐Ÿงฑ

basic_nested_for.py

for i in range(3):        # Outer loop
    for j in range(3):            # Inner loop
        print(i, j)

โœ”๏ธ Output will show all pairs of (i, j).
โœ”๏ธ Inner loop runs fully for each outer loop iteration.

2. Nested While Loop ๐Ÿ”

nested_while.py

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

3. For Loop Inside a While Loop ๐Ÿ”„

for_inside_while.py

i = 1

while i <= 3:
    for j in range(1, 4):
        print(i, j)
    i += 1

4. While Loop Inside a For Loop ๐Ÿงฉ

while_inside_for.py

for i in range(1, 4):
    j = 1
    while j <= 3:
        print(i, j)
        j += 1

5. Pattern Printing Using Nested Loops โญ

Star Triangle

pattern_star.py

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

Number Pattern

pattern_number.py

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

6. Working With 2D Lists (Matrix) ๐Ÿงฎ

2d_list.py

matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

for row in matrix:
    for value in row:
        print(value, end=" ")
    print()

7. Nested Loops With Break ๐Ÿ›‘

nested_break.py

for i in range(3):
    for j in range(3):
        if j == 1:
            break
        print(i, j)

Note

โœ”๏ธ Break affects only the INNER loop.

8. Nested Loops With Continue ๐Ÿ”„

nested_continue.py

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

9. Nested Loops With Else ๐ŸŽฏ

nested_else.py

for i in range(3):
    for j in range(3):
        print(i, j)
    else:
        print("Inner loop completed for i =", i)

10. Real-World Example ๐ŸŒ

Searching an item in 2D list

search_2d_list.py

matrix = [
    [10, 20, 30],
    [40, 50, 60],
    [70, 80, 90]
]

search = 50

for row in matrix:
    for item in row:
        if item == search:
            print("Found", search)
            break

Multiplication Table

multiplication_table.py

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

11. Performance Consideration โš ๏ธ

Nested loops multiply work. Example: 3ร—3 = 9 iterations; 100ร—100 = 10,000 iterations. Use them wisely for large datasets.

Note

๐Ÿง  Try to optimize logic when deeply nested loops become slow.

Conclusion ๐ŸŽ‰

>>โ€œNested loops unlock powerful repetitive structures โ€” perfect for patterns, matrices, and complex iterations.โ€ โœจ

You now fully understand Pythonโ€™s nested loops! Want the next topic? Try Loop Control Statements (break, continue, pass), Range(), or List Comprehensions. Just tell me! ๐Ÿ˜Š