How To Create Half Pyramid Pattern With Stars in Python
In this lesson, I show you how to print patterns in Python. By printing different patterns, you can build a solid understanding of loops in Python. After reading this article you can create various types of patterns.
Steps to Print Pattern in Python
- Decide the number of rows and columns
- Iterate rows
- Iterate columns
- Print star or number
- Add new line after each iteration of outer loop
Code:
# number of rows
rows = 5
for i in range(0, rows):
# nested loop for each column
for j in range(0, i + 1):
# print star
print("*", end=' ')
# new line after each row
print("\r")
rows = 5
for i in range(0, rows):
# nested loop for each column
for j in range(0, i + 1):
# print star
print("*", end=' ')
# new line after each row
print("\r")
Output:
*
* *
* * *
* * * *
* * * * *
* *
* * *
* * * *
* * * * *