Pattern problems are the gym for your brain. They strengthen your looping logic, thinking in steps, and algorithmic intuition — all while keeping it fun.
Pattern: Right-Angled Triangle Using Nested Loops in Python
We’re going to build this simple star pattern and understand the logic behind nested loops:
Logic Overview
We're using two for loops:
- The outer loop controls the rows
 - The inner loop controls the columns (stars)
 
Output:
* * * * * * * * * * * * * * *
Step-by-Step Explanation
row = 5
for i in range(1, row + 1):       # Outer loop: for each row (1 to 5)
    for j in range(i):            # Inner loop: print i stars
        print("*", end=" ")
    print()                       # Move to the 
next line after each row
- 
Why
range(1, row + 1)?- 
range(n)goes from0 to n-1 - So 
range(1, row + 1)gives:1, 2, 3, 4, 5 - That means we’ll have 5 rows, as required.
 
 - 
 Why
range(i)for inner loop?
In each row, the number of stars is equal to the row number:
Row 1 → 1 star
Row 2 → 2 stars
...
Row 5 → 5 stars
Dry Run Table
| Row (i) | Inner Loop (j in range(i)) | Output | 
|---|---|---|
| 1 | 0 | * | 
| 2 | 0, 1 | * * | 
| 3 | 0, 1, 2 | * * * | 
| 4 | 0, 1, 2, 3 | * * * * | 
| 5 | 0, 1, 2, 3, 4 | * * * * * | 
Notice how the number of * matches the current row number i.
Summary
Outer loop (i) = rows
Inner loop (j) = columns (stars)
print("*", end=" ") prints stars on the same line
print() moves to the next line after each row
Reverse Right-Angled Triangle Pattern in Python
Output:
* * * * * * * * * * * * * * *
Step-by-Step Explanation:
row = 5
for i in range(row, 0, -1):       # Outer loop: from 5 to 1
    for j in range(i):            # Inner loop: print i stars
        print("*", end=" ")
    print()                       # Move to the next line after each row
- 
row = 5We want 5 rows, so we initialize the row variable as 5. You can increase this number for a larger pattern. - 
for i in range(row, 0, -1)- This loop goes in reverse from row to 1.
 - 
range(5, 0, -1)outputs:5, 4, 3, 2, 1 -  The third parameter 
-1is the step; without it, the loop won’t run in reverse. 
 - 
for j in range(i)- This inner loop controls how many stars get printed in each row — same as the current value of i. So: Row 1 → 5 stars Row 2 → 4 stars ... Row 5 → 1 star
 
 
Dry Run Table
| Row (i) | Inner Loop (j in range(i)) | Output | 
|---|---|---|
| 5 | 0 1 2 3 4 | * * * * * | 
| 4 | 0 1 2 3 | * * * * | 
| 3 | 0 1 2 | * * * | 
| 2 | 0 1 | * * | 
| 1 | 0 | * | 
Summary:
Outer loop (i): controls the number of rows, decreasing each time
Inner loop (j): prints stars equal to the current row number
end=" " prints stars on the same line
print() moves to the next line after each row
What if we make mirror image of Right-Angled Triangle Using Nested Loops in Python?
Build Your Logic from Scratch: Python Pattern Problems Explained. Star Pattern-2
Build Your Logic from Scratch: Python Pattern Problems Explained. Star Pattern-3
              
    
Top comments (2)
N need for inner loop, just print:
print("* " * i)
Yes ofcourse, it is more efficient then this code