When Python Repeats Things Inside Other Repeated Things.
Yes, the name sounds intense.
Nested loops are exactly what they sound like:
a loop inside another loop.
Before you panic, this is not as bad as it sounds.
You already understand loops.
Now we’re just stacking them.
The Big Idea (Before Any Code)
A nested loop means:
“For every time the outer loop runs,
run the inner loop completely.”
That’s it.
Outer loop → big steps
Inner loop → small steps inside each big step
Think:
Days → hours
Rows → columns
Levels → rooms
Tables → seats
Nested loops describe structure, not complexity.
A Very Simple Example
Let’s start tiny:
for i in range(3):
for j in range(2):
print("Hi")
What happens?
The outer loop runs 3 times
Each time, the inner loop runs 2 times
Total prints:
3 × 2 = 6
Python is calm about this.
It does exactly what you told it.
Seeing the Structure Clearly
Let’s add some labels:
for i in range(3):
print("Outer loop:", i)
for j in range(2):
print(" Inner loop:", j)
Output (simplified):
Outer loop: 0
Inner loop: 0
Inner loop: 1
Outer loop: 1
Inner loop: 0
Inner loop: 1
Outer loop: 2
Inner loop: 0
Inner loop: 1
Notice the pattern:
Python enters the outer loop
Runs the inner loop fully
Goes back to the outer loop
Repeats
That’s nested loops in action.
How to Read Nested Loops Like a Human
Read this:
for row in range(3):
for column in range(4):
print("*", end="")
print()
As plain English:
“For each row,
print four stars,
then move to the next line.”
Output:
****
****
****
Nested loops are often used to build shapes, grids, and patterns.
Why Nested Loops Are Useful
Nested loops shine when you’re dealing with:
Grids
Tables
Patterns
Repeated structures
Coordinates
Games (later on)
Any time something repeats inside something else, nested loops are the right tool.
A Friendly Pattern Example
for i in range(5):
for j in range(i + 1):
print("*", end="")
print()
Output:
*
**
***
****
*****
This looks fancy but it’s just:
One loop controlling rows
One loop controlling stars per row
Simple pieces. Big result.
Indentation Matters More Than Ever
With nested loops, indentation is everything.
for i in range(3):
for j in range(3):
print(i, j)
vs
for i in range(3):
print(i)
for j in range(3):
print(j)
Same loops.
Very different meaning.
When confused:
Look at indentation
Ask: “Which loop am I inside right now?”
Common Beginner Confusion (Totally Normal)
“Why is the inner loop restarting?”
Because it’s supposed to.
Every time the outer loop moves one step forward, the inner loop starts fresh.
That’s not a bug.
That’s the design.
When Not to Use Nested Loops
Nested loops are powerful but don’t use them just because you can.
Avoid them when:
A single loop would work
You’re repeating logic unnecessarily
The code becomes hard to read
Clarity always beats cleverness.
What You’ve Learned
You now understand:
What nested loops are
How inner and outer loops interact
Why indentation is critical
How patterns and grids are built
That nested loops aren’t scary, just structured
This is a major milestone.
Mini Quiz
Try these:
How many times does the inner loop run in total?
for i in range(4):
for j in range(3):
print("X")
2. Which loop controls rows in a pattern?
3. Why does the inner loop restart each time?
4. What will this print?
for i in range(2):
for j in range(2):
print(i, j)
Coming Up Next
You’ve finished all the core loop concepts.
Now it’s time to use them in something fun.