Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programs for printing pyramid patterns in Python
Taking advantage of the for loop and range function in Python, we can draw a variety of pyramid structures. The key to the approach is designing the appropriate for loop which will leave both vertical and horizontal space for the position of the symbol we choose for drawing the pyramid structure.
Pattern 1: Right-Angled Triangle
We draw a right angle based pattern where each row contains an increasing number of stars ?
def pyramid(p):
for m in range(0, p):
for n in range(0, m+1):
print("* ", end="")
print()
p = 10
pyramid(p)
The output of the above code is ?
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Pattern 2: Right-Aligned Triangle
We make a 180 degree rotation to the above pattern by adding leading spaces ?
def pyramid(p):
X = 2*p - 2
for m in range(0, p):
for n in range(0, X):
print(end=" ")
X = X - 2
for n in range(0, m+1):
print("* ", end="")
print()
p = 10
pyramid(p)
The output of the above code is ?
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
Pattern 3: Isosceles Triangle
In this case we draw a triangle shape of type isosceles. Both the sides of the triangle are of equal length ?
n = 0
r = 12
for m in range(1, r+1):
for gap in range(1, (r-m)+1):
print(end=" ")
while n != (2*m-1):
print("* ", end="")
n = n + 1
n = 0
print()
The output of the above code is ?
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * *
Pattern 4: Equilateral Triangle
Now we draw a case of triangle in which all the three sides are equal. Also called equilateral triangle ?
length = 12
k = (2 * length) - 2
for p in range(0, length):
for n in range(0, k):
print(end=" ")
k = k - 1
for n in range(0, p + 1):
print("@", end=' ')
print()
The output of the above code is ?
@
@ @
@ @ @
@ @ @ @
@ @ @ @ @
@ @ @ @ @ @
@ @ @ @ @ @ @
@ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @ @ @
Comparison
| Pattern | Shape | Alignment | Use Case |
|---|---|---|---|
| Pattern 1 | Right-angled | Left-aligned | Simple step pattern |
| Pattern 2 | Right-angled | Right-aligned | Rotated step pattern |
| Pattern 3 | Isosceles | Center-aligned | Classic pyramid |
| Pattern 4 | Equilateral | Center-aligned | Symmetrical design |
Conclusion
Pyramid patterns in Python are created using nested loops to control spacing and symbol placement. Use right-angled patterns for simple steps, and isosceles triangles for classic pyramid shapes.
