Python Nested Loops

A nested loop is present inside the loop. Read this article to learn more about Python Nested Loop.

What is Python Nested Loops

Nested loops in Python refer to the concept of having one loop inside another. For instance, placing a ‘while‘ loop within a ‘for’ loop or vice versa. This approach enables programmers to perform more intricate and dynamic operations, enhancing the versatility of their code.

What are Python Nested Loop Example?

Example 1- Example of Python Nested Loop

for i in range(1, 6):  # Outer loop for rows
    for j in range(1, 6):  # Inner loop for columns
        result = i * j
        print(f"{i} * {j} = {result}", end='\t')
    print()  # Move to the next line after each row

Output

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

Example 2: Printing Multiplication Table using the Python Nested Loops

# Define the range for the multiplication table
start_range = 1
end_range = 10

# Nested loop to generate the multiplication table
for i in range(1, end_range + 1):  # Outer loop for rows
    for j in range(1, end_range + 1):  # Inner loop for columns
        result = i * j
        print(f"{i} * {j} = {result}", end='\t')
    print()  # Move to the next line after each row

Output

1 * 1 = 1	1 * 2 = 2	1 * 3 = 3	1 * 4 = 4	1 * 5 = 5	1 * 6 = 6	1 * 7 = 7	1 * 8 = 8	1 * 9 = 9	1 * 10 = 10	
2 * 1 = 2	2 * 2 = 4	2 * 3 = 6	2 * 4 = 8	2 * 5 = 10	2 * 6 = 12	2 * 7 = 14	2 * 8 = 16	2 * 9 = 18	2 * 10 = 20	
3 * 1 = 3	3 * 2 = 6	3 * 3 = 9	3 * 4 = 12	3 * 5 = 15	3 * 6 = 18	3 * 7 = 21	3 * 8 = 24	3 * 9 = 27	3 * 10 = 30	
4 * 1 = 4	4 * 2 = 8	4 * 3 = 12	4 * 4 = 16	4 * 5 = 20	4 * 6 = 24	4 * 7 = 28	4 * 8 = 32	4 * 9 = 36	4 * 10 = 40	
5 * 1 = 5	5 * 2 = 10	5 * 3 = 15	5 * 4 = 20	5 * 5 = 25	5 * 6 = 30	5 * 7 = 35	5 * 8 = 40	5 * 9 = 45	5 * 10 = 50	
6 * 1 = 6	6 * 2 = 12	6 * 3 = 18	6 * 4 = 24	6 * 5 = 30	6 * 6 = 36	6 * 7 = 42	6 * 8 = 48	6 * 9 = 54	6 * 10 = 60	
7 * 1 = 7	7 * 2 = 14	7 * 3 = 21	7 * 4 = 28	7 * 5 = 35	7 * 6 = 42	7 * 7 = 49	7 * 8 = 56	7 * 9 = 63	7 * 10 = 70	
8 * 1 = 8	8 * 2 = 16	8 * 3 = 24	8 * 4 = 32	8 * 5 = 40	8 * 6 = 48	8 * 7 = 56	8 * 8 = 64	8 * 9 = 72	8 * 10 = 80	
9 * 1 = 9	9 * 2 = 18	9 * 3 = 27	9 * 4 = 36	9 * 5 = 45	9 * 6 = 54	9 * 7 = 63	9 * 8 = 72	9 * 9 = 81	9 * 10 = 90	
10 * 1 = 10	10 * 2 = 20	10 * 3 = 30	10 * 4 = 40	10 * 5 = 50	10 * 6 = 60	10 * 7 = 70	10 * 8 = 80	10 * 9 = 90	10 * 10 = 100

Example 3:  Printing using different inner and outer nested loops

# Define the range for the nested loops
outer_range = 3
inner_range = 5

# Nested loops with different ranges
for i in range(1, outer_range + 1):  # Outer loop
    for j in range(1, inner_range + 1):  # Inner loop
        result = i + j
        print(f"Outer: {i}, Inner: {j}, Result: {result}")

#output


Output

Outer: 1, Inner: 1, Result: 2
Outer: 1, Inner: 2, Result: 3
Outer: 1, Inner: 3, Result: 4
Outer: 1, Inner: 4, Result: 5
Outer: 1, Inner: 5, Result: 6
Outer: 2, Inner: 1, Result: 3
Outer: 2, Inner: 2, Result: 4
Outer: 2, Inner: 3, Result: 5
Outer: 2, Inner: 4, Result: 6
Outer: 2, Inner: 5, Result: 7
Outer: 3, Inner: 1, Result: 4
Outer: 3, Inner: 2, Result: 5
Outer: 3, Inner: 3, Result: 6
Outer: 3, Inner: 4, Result: 7
Outer: 3, Inner: 5, Result: 8

Break Statement in Nested Loop

In Python, the ‘break’ statement acts as a loop control mechanism, offering a means for premature termination. When triggered within a loop, be it a ‘for’ or ‘while’ loop, the ‘break’ statement immediately halts the iteration and exits the loop, allowing the program to resume execution after the loop construct. This provides a valuable tool for managing flow control and responding to specific conditions during program execution.

Check out the example provide below:

# Using break statement in nested loops

for i in range(1, 4):  # Outer loop
    for j in range(1, 4):  # Inner loop
        print(f"Outer: {i}, Inner: {j}")
        if i == 2 and j == 2:
            print("Break statement triggered!")
            break  # Exit the inner loop when condition is met

# Output

Output

Outer: 1, Inner: 1
Outer: 1, Inner: 2
Outer: 2, Inner: 1
Outer: 2, Inner: 2
Break statement triggered!

Continue statement in nested loops

“In Python, there’s something called a ‘continue’ statement. It works in loops and does the opposite of ‘break’. When ‘continue’ appears, it doesn’t stop the loop; instead, it skips the rest of the code for that round and jumps to the next go-around. This is different from ‘break’, which ends the whole loop altogether.”

Let’s look at an example to better understand this concept



# Using continue statement in nested loops

for i in range(1, 4):  # Outer loop
    for j in range(1, 4):  # Inner loop
        if i == 2 and j == 2:
            print("Continue statement triggered! Skipping this iteration.")
            continue  # Skip the rest of the inner loop and move to the next iteration
        print(f"Outer: {i}, Inner: {j}")

# Output

Output


Outer: 1, Inner: 1
Outer: 1, Inner: 2
Continue statement triggered! Skipping this iteration.
Outer: 1, Inner: 3
Outer: 2, Inner: 1
Outer: 2, Inner: 3
Outer: 3, Inner: 1
Outer: 3, Inner: 2
Outer: 3, Inner: 3

Single-line Nested loops using list comprehension

In Python, you can make your code shorter by using something called ‘list comprehension.’ It’s like a shortcut that combines nested loops into a single line. You put an expression in brackets and use a ‘for’ loop to do the work.

What is the List Comprehension?

newList = [ expression(element) for element in oldList if condition ] 

Look at the example given below

# Using list comprehension to create a list of squares
original_list = [1, 2, 3, 4, 5]
squares = [x**2 for x in original_list]

print(squares)

Output

[1, 4, 9, 16, 25]

Conclusion

To conclude, “In conclusion, Python nested loops are a powerful way to perform complex iterations by having one loop inside another. Whether using ‘for’ or ‘while’ loops, this technique allows for versatile code structures.

Additionally, techniques like ‘break’ and ‘continue’ statements can enhance control within nested loops. List comprehension further streamlines nested loops, providing a concise and readable solution for handling multiple iterations in Python.

Python Nested Loops- FAQs

Q1. How many nested loops does Python allow?

Ans. It is possible to add as many loops.

Q2. What is loop syntax in Python?

Ans. It is referred to as a Control flow statement that will run the group of statements until the condition is satisfied.

Q3. What is a delay loop?

Ans. The delay loop functions to delay the running of the program.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment