Print Triangle pyramid pattern in python

    *
   ***
  *****
*********

 

Last updated 2 weeks ago | 40 views 75     5

Tags:- Python

Python | Print  Triangle pyramid pattern

This is an example of how to print the Triangle pyramid pattern. It takes an input number from the user to print the number of rows for the pattern. 

n = int(input('enter no:'))

for i in range(n):
	print(" "*(n-i-1) + '*'*(2*i+1))
 

The output of the above code is:

>>>
========================= RESTART: E:\py\pattern.py =========================
enter no:5
    *
   ***
  *****
 *******
*********

Explanation:

In the above code, there is an input statement so when the code runs it asks to input a number to print the number of rows for the pattern.

let's say 5 is the entered number. So the range for the loop will be 0 to 4.

The print() statement print blank spaces < " "*(n-i-1) > and asterisk(*) < '*'*(2*i+1) > depending on the of < i > passed to the print function.

< n > is 5

< i > will depend on the loop

loop n-i-1 < " "*(n-i-1) >
(blank)
2*i+1 < '*'*(2*i+1) >
(*)
1. 5-0-1 = 4 (blank) 2*0+1 = 1 (*)
2. 5-1-1 = 3 (blank) 2*1+1 3 (*)
3. 5-2-1 = 2 (blank) 2*2+1 5 (*)
4. 5-3-1 = 1 (blank) 2*3+1 7 (*)
5. 5-4-1 = 0 (blank) 2*4+1 9 (*)