1. Nested loop = loop inside another loop (e.g. for inside for).
  2. How it runs: outer runs once β†’ inner runs fully β†’ outer runs again β†’ inner runs fully again ...
  3. Total iterations: outer count Γ— inner count (e.g. 5Γ—5 = 25).
  4. Examples: (i,j) grid, simple star patterns, nested loops over strings.

Let’s go! πŸš€


Part 1: What is a Nested Loop?

πŸ“Œ A nested loop is a loop written INSIDE another loop.

for i in range(5):        ← outer loop
    for j in range(5):    ← inner loop (nested)
        print(i, j)

You can mix: for inside while, while inside for, etc. Here we use for inside for.


Part 2: How Does It Run?

πŸ“Œ Rule: For each iteration of the OUTER loop, the INNER loop runs completely.

  • Outer: i = 0 β†’ inner runs j = 0, 1, 2, 3, 4 β†’ then outer goes to next.
  • Outer: i = 1 β†’ inner runs j = 0, 1, 2, 3, 4 again (starts fresh).
  • Outer: i = 2 β†’ inner runs j = 0, 1, 2, 3, 4 again.
  • ... same for i = 3, i = 4.

So the inner body runs 5 Γ— 5 = 25 times. In general: outer m times, inner n times β†’ m Γ— n times.

Demo: (i, j) for i in range(3), j in range(3) β†’ 9 pairs

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

Part 3: Print on One Line (end=" ")

πŸ“Œ print(i, j) by default goes to a new line each time. To print on the same line, use: print(i, j, end=" ") Then after the inner loop, use print() to go to the next line (one line per i).

So each "row" is one value of i; each "column" is j.

Demo: grid with end=' ', one row per i

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

Part 4: Simple Patterns (condition on i, j)

πŸ“Œ You can print only when a condition is true. Examples:

  • Print "*" only when j <= i β†’ triangle of stars (first row 1, second 2, ...).
  • Print "*" only when i <= j β†’ other triangle.
  • No condition β†’ full rectangle of stars.

Demo: 5Γ—5 stars (full block)

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

Demo: triangle (print * when j <= i)

for i in range(5):
    for j in range(5):
        if j <= i:
            print("*", end=" ")
        else:
            print(" ", end=" ")
    print()

Part 5: Nested Loops over Strings

πŸ“Œ for works on any sequence. So you can do: for i in "abc": for j in "xyz": print(i, j) That gives: a x, a y, a z, b x, b y, b z, c x, c y, c z.

Demo: for i in 'ab', j in '12'

for i in "ab":
    for j in "12":
        print(f"   {i}{j}", end=" ")
    print()

Part 6: Summary

βœ… Nested loop = loop inside loop. Inner runs fully for each outer iteration.

βœ… Total runs = (outer iterations) Γ— (inner iterations).

βœ… print(..., end=" ") keeps output on same line; print() starts new line.

βœ… Use conditions (e.g. j <= i) to draw patterns. Nested loops work with range, strings, etc.


Congratulations! You now understand Nested Loops in Python! πŸŽ‰

Key Takeaways:

  • Inner loop runs completely for every outer iteration
  • Total executions = outer Γ— inner
  • Use end=" " + print() for grids and patterns
  • Works with strings, ranges, lists, etc.

Next Steps:

  • Create more patterns (diamond, pyramid, multiplication table)
  • Try nested loops over two strings
  • Combine with previous lessons (prime check inside a range)