5 Unique Star Pyramid Patterns with Python
In this, we will be printing different interesting star pyramid patterns in terminal using python.
Pattern - 1
n = int(input("Enter no. of rows"))
for x in range(1,n+1):
for y in range(1,x+1):
print("*", end=" ")
print("") #For changing line
Output
Enter no. of rows6
*
* *
* * *
* * * *
* * * * *
* * * * * *
Logic and Explanation
In this pattern, you can see first row contains one star then next row contains two stars and so on. We will take input the number of rows from the user. Then we will iterate from 1 to that number using for loop and we will print the stars according to the number 'x' in the iterations as shown in the above code. This program is a example of nested looping in which we ran one loop for the number of stars and rows and other loop for printing the stars in one line.Pattern - 2
n = int(input("Enter no of rows"))
for x in range(1,n+1):
for y in range(1,x+1):
print(y, end=" ")
print("") #to change line
Output
Enter no of rows7
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
Logic and Explanation
This pattern is very similar to the first pattern except that in first pattern we printed stars and in this pattern we printed numbers. For this, you just have to do a small change. In place of printing the star we will print "y" variable whose value is controlled in the nested loop.
Pattern - 3
n = int(input("Enter no of row"))
for x in range(1,n+1):
for y in range(1,x+1):
print(x,end=" ")
print("")
Output
Enter no of row7
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
Logic and Explanation
In this pattern we will do almost the same thing we done in the last pattern. You can see that in first row all the numbers are 1, in second row all are 2 and so on. So, basically we have to print the row number and the row number is the value stored in "x" variable. So we will print "x" and iterate it as we done in the last two patterns.
Pattern - 4
n = int(input("Enter no of rows"))
for x in range(1,n+1):
for z in range(n-x,0,-1):
print(" ", end=" ")
for y in range(1,x+1):
print(" * ", end= " ")
print("")
Output
Enter no of rows8
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
Logic and Explanation
This pattern is quite interesting one. In this pattern, we have to print the stars like they are centre align. For this we will first create a loop iterating for number of rows i.e. "n" times. Then we will create a loop which will print the spaces first and then another loop which we will print the stars. In last row, there is no space in front of stars, so you can understand this as grid of n by n. For star to be in centre, we have to leave n-x spaces to the left where x is the number of stars in that row.
Pattern - 5
n = int(input("Enter no of rows"))
i = 0
for x in range(1,n+1):
for y in range(1,n+1-i):
print(y,end=" ")
print("")
i = i+1
Output
Enter no of rows6
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Logic and Explanation
In all the above patterns you can see that the number of stars in row is equal to the row number. But in this pattern we will be printing stars
n - row number +1 times in each row. You can check the code, we just have to change that printing of stars with the above formula.
If you like the content, share it in your social media platforms so that we can help the maximum. If you have any query then feel free to ask in the comments below.
We are ready to help you.
Check Similar Programs Here - Python Programs - Question and Answers
Enjoy Coding!!
Post a Comment
For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.