Ever looked at a pattern problem and thought, "Why can't I get this simple star pyramid to align properly?"
You're not alone. Pattern problems might look easy, but they’re the secret weapon for building powerful logic using loops — and today, we’re going to decode them step by step.
Pattern: Mirrored Right Angle Triangle Using Nested Loop 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)
* * * * * * * * * * * * * * *
Logic Overview
To form this pyramid:
- You need spaces before star to centre-align them.
- The number of stars increases with each row.
1 | 2 | 3 | 4 | 5 | |
---|---|---|---|---|---|
1 | (1,1) | (1,2) | (1,3) | (1,4) | * |
2 | (2,1) | (2,2) | (2,3) | * | * |
3 | (3,1) | (3,2) | * | * | * |
4 | (4,1) | * | * | * | * |
5 | * | * | * | * | * |
- As you see row increase the column decrease. (1,4),(2,3),(3,2),(4,1). or we can say,
Spaces increases and star decreases.
Code
row=5 for i in range(1,row+1): for j in range(1,row-i+1): print(" ",end=" ") for k in range(1,i+1): print("*",end=" ") print()
Explanation
-
row=5
this is for how many rows you want. -
for i in range(1,row+1):
This is the outer loop, which runs 5 times (from 1 to 5). Each loop iteration represents one row of the output. -
for j in range(1,row-i+1):
This inner loop is responsible for printing spaces before the stars. Why? To make the pattern right-aligned, each row needs less space than the previous one. -
for k in range(1,i+1):
This loop prints the stars * in each row.
Dry Run Table
i | j | k | |
---|---|---|---|
Row(i) | Spaces(row-i+1) | Stars(i+1) | Output |
1 | 4 | 1 | * |
2 | 3 | 2 | * * |
3 | 2 | 3 | * * * |
4 | 1 | 4 | * * * * |
5 | 0 | 5 | * * * * * |
Summary:
Outer loop (i)
: controls the number of rows, increasing each time
Inner loop (j)
: controls the trailing spaces.
Inner loop (k)
: prints the star.
end=" "
prints stars on the same line
print()
moves to the next line after each row
Pattern: Pyramid Using Nested Loop in Python
Output:
* * * * * * * * * * * * * * * * * * * * * * * * *
Step by Step Explanation
row=5 for i in range(1,row+1): for j in range(row-i): print(" ",end=" ") for k in range(2*i-1): print("*",end=" ") print()
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
---|---|---|---|---|---|---|---|---|---|
1 | * | ||||||||
2 | * | * | * | ||||||
3 | * | * | * | * | * | ||||
4 | * | * | * | * | * | * | * | ||
5 | * | * | * | * | * | * | * | * | * |
-
i
->1,row+1, how many row you want. -
j
->row-i, it is for spaces, as it decreasing. -
k
2*i-1, it is to print star.
i | j | k |
---|---|---|
1 | 4 | 1 |
2 | 3 | 3 |
3 | 2 | 5 |
4 | 1 | 7 |
5 | 0 | 9 |
Summary
Outer loop (i)
: controls the number of rows, increasing each time
Inner loop (j)
: controlling the spaces which is decreasing each time.
Inner loop (k)
: It will print the star.
end=" " prints stars on the same line
print() moves to the next line after each row
How to make inverted pyramid?
Build Your Logic from Scratch: Python Pattern Problems Explained. Star Pattern-1
Build Your Logic from Scratch: Python Pattern Problems Explained. Star Pattern-3
Top comments (1)
Very interesting ^^