Hello everyone, welcome back to programminginpython.com! I recently started a new series on pattern programming, I explained how to print a Floydβs Triangle pattern in the previous post. Here I will explain to you how to print a pyramid pattern or a triangle pattern in Python.
You can also watch the video on YouTube here.
Pyramid Pattern β Code Visualization
Task
Python Program to print a Pyramid/Triangle Pattern.
Approach
- Read an input integer for asking the range of the triangle using
input() - Run 3 for loops, one main for column looping and other 2 sub-loops for row looping, in the first loop, loop through the range of the triangle for column looping.
- In the second loop, loop through the value of the range β 1, this is to print space
- In the third loop, loop through the value of i+1 and print
*
Program
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
__author__ = 'Avinash' # Print a Triangle # Range of the triangle num = int(input("Enter the range: \t ")) # i loop for range(height) of the triangle # first j loop for printing space ' ' # second j loop for printing stars '*' for i in range(num): for j in range((num - i) - 1): print(end=" ") for j in range(i + 1): print("*", end=" ") print() |
Output

That is it for this tutorial. Also feel free to look at programs on other patterns or some algorithms implementation in python here or look at all of the posts here.
Course Suggestion
Machine Learning everywhere! So I strongly suggest you to take the course below.
Course: Machine Learning Adv: Support Vector Machines (SVM) Python
hi I did the same thing but I am getting different type of pattern
num=int(input(βenter the number: β))
for i in range(num):
for j in range((num-i)-1):
print(end=ββ)
for j in range(0,i+1):
print(β*β,end=ββ)
print()
output:
*
**
***
****
*****
******
You missed the spaces.
end is a space in line 13 and 15.
print(β*β,end=β β).Watch the video for more clarity.