Inverse Pyramid In Python

Python Short Codes
1 min readDec 24, 2022

--

To create an inverted pyramid in Python, we can use a combination of loops and print statements. Here is an example of a program that creates an inverted pyramid of asterisks with a base of 5 asterisks:

# define the base size of the pyramid
base_size = 5

# use a for loop to iterate over the rows of the pyramid
for row in range(base_size):
# use another for loop to iterate over the columns of the pyramid
for column in range(base_size - row):
# print an asterisk for each column in the row
print("*", end="")
# move to the next line after printing the row
print()

This program works similar to the previous example, but it uses the base_size - row expression to determine the number of asterisks to print in each row. This results in the pyramid being inverted, with the base at the top and the tip at the bottom.

The output of this program will be an inverted pyramid with a base of 5 asterisks that looks like this:

Output
*****
****
***
**
*

I hope this helps! Let me know if you have any questions or need further clarification.

--

--

Python Short Codes
Python Short Codes

Written by Python Short Codes

Hi Rajat here, Well versed with predictive modelling, data processing, and data mining algorithms to solve challenging business problems.

No responses yet