Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

Python Nested Loops (Complete Guide for Beginners)

Nested loops in Python mean a loop inside another loop. In simple words, it is when:
👉 One loop runs inside another loop.

Nested loops are very powerful when working with:

  • Patterns (stars, numbers)
  • Tables
  • Multi-dimensional data (like matrices)
  • Repeated combinations

🔹 What is a Nested Loop in Python?

A nested loop is a loop inside another loop.

👉 Basic Idea:

  • Outer loop controls the number of times inner loop runs
  • Inner loop runs completely for each iteration of outer loop

🔹 Syntax of Nested Loops

for outer in range(n):
for inner in range(m):
# code block

You can also use:

  • for inside for
  • while inside while
  • for inside while (and vice versa)

🔹 How Nested Loop Works

Example flow:

Outer loop → runs 1 time
Inner loop → runs full cycle
Outer loop → runs next time
Inner loop → runs again fully

🔹 Example 1: Basic Nested Loop

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

🔸 Output:

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

🔍 Explanation:

  • Outer loop controls i
  • Inner loop controls j
  • Inner loop runs completely for each i

🔹 Example 2: Multiplication Table

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

🔸 Output:

1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50

🔍 Explanation:

  • Outer loop selects number
  • Inner loop prints multiplication table

🔹 Example 3: Star Pattern (Rectangle)

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

🔸 Output:

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

🔹 Example 4: Triangle Pattern

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

🔸 Output:

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

🔹 Example 5: Nested While Loop

i = 1

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

🔹 Real-Life Example of Nested Loops

👉 Example: Seating Arrangement

rows = 3
cols = 4

for i in range(rows):
for j in range(cols):
print(f"Seat({i},{j})", end=" ")
print()

🔍 Explanation:

  • Rows = outer loop
  • Columns = inner loop
  • Used in real systems like:
    • Movie theaters
    • Bus seating
    • Game grids

🔹 Nested Loop with Lists (2D Data)

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

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

🔸 Output:

1 2 3
4 5 6
7 8 9

🔹 Time Complexity Note

Nested loops can be slow if data is large:

  • Single loop → O(n)
  • Nested loop → O(n²)

So use carefully in big programs.


🔹 Common Mistakes

❌ Forgetting indentation:

for i in range(3):
for j in range(3):
print(i, j)

✔ Correct:

for i in range(3):
for j in range(3):
print(i, j)

🔹 Key Points to Remember

  • Nested loop = loop inside loop
  • Inner loop completes fully for each outer loop iteration
  • Used in patterns, grids, and tables
  • Can increase time complexity

🚀 Conclusion

Python nested loops are essential for solving real-world programming problems like patterns, matrices, and structured data processing.

Once you understand nested loops, you can easily build:

  • Game grids
  • Pattern programs
  • Data tables
  • Complex logic systems 




Post a Comment

0 Comments