Python Program to Create Pyramid Patterns
Pyramid patterns are a type of pattern or design that form a pyramid shape when viewed from a certain perspective. These patterns can be created using a variety of shapes, such as stars, numbers, or letters, arranged in a specific way to create a visually appealing effect.
Python Code :
There are multiple patterns with pyramid, which are asked in entry level interviews.
Python programs to create different types of pyramid patterns using nested loops:
1. Half Pyramid Pattern
*
**
***
****
*****
The below Python program implements Half pyramid pattern :
n = 5
for i in range(n):
for j in range(i+1):
print('*', end='')
print()
2. Inverted Half Pyramid Pattern
*****
****
***
**
*
The below Python program implements Inverted Half pyramid pattern :
n = 5
for i in range(n):
for j in range(n-i):
print('*', end='')
print()
3. Full Pyramid Pattern
*
***
*****
*******
*********
The below Python program implements Full pyramid pattern :
n = 5
for i in range(n):
for j in range(n-i-1):
print(' ', end='')
for k in range(2*i+1):
print('*', end='')
print()
4. Inverted Full Pyramid Pattern
*********
*******
*****
***
*
The below Python program implements Inverted Full pyramid pattern :
n = 5
for i in range(n):
for j in range(i):
print(' ', end='')
for k in range(2*(n-i)-1):
print('*', end='')
print()
In these programs, n is the number of rows in the pyramid. The outer loop controls the rows, and the inner loops control the columns. The first inner loop prints the required number of stars or spaces, depending on the pattern. The second inner loop prints the stars in the center of the pyramid, and its range is determined by the row number i. Finally, the print() statement with no arguments is used to move the cursor to the next line.