Reverse pyramid pattern in Python
Here, you will learn different Python programs to print the reverse pyramid pattern. This type of logical question is generally asked in interviews or examinations. This can help you to improve your logical programming skills, which can be useful in game development and other logical application development.
The following Python program prints a reverse pyramid pattern made up of stars up to the specified n lines. In this Python program, we take a row number as input from the user. Here the row demonstrates the number of lines that will be printed in the reverse triangle shape.
# Get range of the reverse pyramid
n = int(input("Enter the range: \t "))
# outer loop over the range
for rows in range(n, 0, -1):
# Inner loop to create gap
for columns in range(0, n-rows):
print(end=" ")
# Inner loop to print star
for columns in range(0, rows):
print("*", end=" ")
print()
Output of the above code-
Enter the range: 10
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
Reverse pyramid character pattern in Python
The given Python program prints the reverse pyramid character pattern.
# Print reverse character
# Pyramid pattern
def reverse_pyramid( n ):
# outer loop to deal with number
# number of rows
for i in range(n, 0, -1):
# inner loop
# create right triangle gaps
# on the left side of pyramid
for gap in range(n-1, i-1, -1):
print(" ", end = '')
print(" ", end = '')
# initializing character value
ch = ord('A')
# Print characters on
# left side of pyramid
for x in range(1, i+1):
print(chr(ch), end = ' ')
ch += 1
# Print characters on
# right side of pyramid
for y in range(i - 1, -1, -1):
ch -= 1
print(chr(ch), end = ' ')
print("\n", end = '')
n = int(input("Enter the range: \t "))
reverse_pyramid(n)
Output of the above code-Enter the range: 10
A B C D E F G H I J J I H G F E D C B A
A B C D E F G H I I H G F E D C B A
A B C D E F G H H G F E D C B A
A B C D E F G G F E D C B A
A B C D E F F E D C B A
A B C D E E D C B A
A B C D D C B A
A B C C B A
A B B A
A A
Reverse pyramid number pattern in Python
The following Python program prints the reverse pyramid number pattern. We have used the reversed loop to print the downward inverted pyramid where the number is reduced after each iteration.
# Print reverse number
# Pyramid pattern
rows = int(input("Enter the number of rows: "))
k = 0
# Reversed loop for downward inverted pattern
for i in range(rows, 0, -1):
k += 1
for j in range(0, rows-i):
print(end=" ")
for j in range(1, i + 1):
print(k, end=' ')
print()
Output of the above code:
Enter the number of rows: 6
1 1 1 1 1 1
2 2 2 2 2
3 3 3 3
4 4 4
5 5
6
Reverse pyramid pattern in descending order of number in Python
The following Python program prints the reverse pyramid number pattern in descending order. We have used the reversed loop to print the downward inverted pyramid where number reduced after each iteration.
rows = int(input("Enter the number of rows: "))
k = 0
# Reversed loop for downward inverted pattern
for i in range(rows, 0, -1):
n = i
for j in range(0, rows-i):
print(end=" ")
for j in range(0, i):
# print reduced value in each new line
print(n, end=' ')
print()
Output of the above code:
Enter the number of rows: 10
10 10 10 10 10 10 10 10 10 10
9 9 9 9 9 9 9 9 9
8 8 8 8 8 8 8 8
7 7 7 7 7 7 7
6 6 6 6 6 6
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
Related Articles
Alphabet pattern programs in Python
Print hollow diamond pattern in Python
Pascal triangle pattern in Python
Convert array to list Python
numpy dot product
glob in Python
Python heap implementation
zip function in Python
Remove last element from list Python
Check if list is empty Python
Remove element from list Python
Python split multiple delimiters
Python loop through list
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list
Remove character from string Python
Python raise keyword