In the previous article we explored the printing of square patterns in Python, now let us look at another common printing pattern, the printing of triangle patterns in Python. The program prints the triangle according to the respective values given by the user, suppose the user enters the value of 4 the resultant output is:
*
**
***
****
Implementation of Pyramid Patterns in Python
As seen in the previous article, will use the range( ) function to return the range 0 to n-1 of numbers for the triangle pattern.
n= int(input("Enter n"))
for i in range(n):
for j in range(i_1):
print("*", end =" ")
print()
Output 1:
Enter n : 6
*
* *
* * *
* * * *
* * * * *
* * * * * *
Output 2:
Enter n : 12
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
The program iterates through the initial loop and the loop nested within to print the character and the pattern exactly according to the value specifications of the user.
Hence we have seen the implementation of the triangle pattern in Python, in the next part of the pattern series we will explore the same concept of the Pyramid pattern or structure but in an inverse reversed variation of it.