If you're a visual learner, pattern problems in Python are the perfect playground.
From simple triangles to pyramids and diamonds — every pattern teaches you how loops and logic work together. Ready to visualize code like never before?
Inverted Pyramid Using Nested Loop in Python.
* * * * * * * * * * * * * * * * * * * * * * * * *
Before diving in, check out the previous patterns:
Reverse Right-Angled Triangle Pattern
Pyramid Using Nested Loop in Python
Once you understand both the pyramid and the reverse right-angled triangle, the logic behind the inverted pyramid becomes intuitive.
Code
row=5 for i in range(row,0,-1): for j in range(row-i): print(" ",end=" ") for k in range(2*i-1): print("*",end=" ") print()
Explanation:
-
range(row, 0, -1)
makes the rows decrease from 5 to 1, it will inverse the Pyramid. -
row - i
controls the leading spaces to shift the stars rightward. -
2*i - 1
ensures that each row has the correct number of stars to form a centered triangle.
Diamond Pyramid Pattern.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Concept:
- First, you build the pyramid (top half).
- Then, you mirror it by adding the inverted pyramid (bottom half).
- Both parts share a common middle (the row with 9 stars).
Code
row=5 for i in range(1,row+1): # Upper Pyramid for j in range(row-i): print(" ",end=" ") for k in range(2*i-1): print("*",end=" ") print() for i in range(row,0,-1): # Inverted Pyramid for j in range(row-i): print(" ",end=" ") for k in range(2*i-1): print("*",end=" ") print()
Explanation
-
range(1, row+1)
builds the upper pyramid. -
range(row, 0, -1)
builds the inverted lower half. - Since both pyramids use the same logic (2*i - 1 stars and shifting spaces), the transition is seamless.
Explained the Inverted Right Angle Triangle Pattern
Explained the Pyramid Pattern
Top comments (0)