In this simple python program, we need to print the inverted star pattern. It's a pattern python program.
To understand this example, you should have knowledge of the following Python programming topics:
In this simple python program on pattern printing, we need to print an inverted star pattern. For that, we have to know about the inverted star pattern. The inverted star pattern is decrementing stars in each row. Suppose take an example of 5, which means we need to print 5 rows of stars as the first row have 5 stars and the second row has 4 stars like that last row have 1 star.
* * * * * * * * * * * * * * *
In this pattern python program example, we use a for loop from the number to zero decrementing by 1 in each iteration. In every for loop
iteration, we have to print the sum of the number of stars and the number of blanks spaces. For the first row, we use the formula [(n-i) * ' ' + i * '*'] which means (n-i) multiplied with blank space + i multiplied with *.
So in the first for loop value of n = 5 and i = 5. so n - i = 5 -5 = 0, so there will be no blank space and in the next part, i = 5 so 5 multiplied with * so we get printed 5 stars *****, in each iteration, the values change and we get inverted pattern star using python programming basics.
STEP 1: Accept the input number from the user using the input function and convert that string to an integer using int()
.
STEP 2: Use a for loop
from the number to zero decrementing by 1 in each iteration.
STEP 3: Use the print in the python programming language to print the starts by applying the formula (n-i) * ' ' + i * '*.'
n=int(input("Enter number of rows: "))
for i in range (n,0,-1):
print((n-i) * ' ' + i * '*')
Enter number of rows: 5 ***** **** *** ** *