Fun With Print() Function: How To Create Half Pyramid Pattern With Stars in Python

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

  1. Decide the number of rows and columns
  2. Iterate rows
  3. Iterate columns
  4. Print star or number
  5. 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")

Output:

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

Post a Comment

Previous Post Next Post