Python is a popular high-level programming language that is widely used for various purposes such as web development, data analysis, machine learning, and more. In this article, we will learn how to write Python programs to print the below patterns. These patterns are a great way to practice basic programming concepts like loops and conditional statements, and they can also be used to create interesting designs.

Before we dive into the code, let's take a closer look at the patterns we want to print. The pattern we will be printing is a pyramid of asterisks. The first line of the pyramid will have one asterisk, and each subsequent line will have one more asterisk than the previous line. The pyramid will have a total of five lines.

Now, let's take a look at the Python code to print this pattern:

Steps:

- Create a file with .py extension. - Write solution of given problem and save - Run to get the Output

**Solution/code: starpattern.py file**

# Python program to print pyramid of asterisks # Define the number of rows rows = 5 # Outer loop to handle number of rows for i in range(0, rows): # Inner loop to handle number of spaces for j in range(0, rows-i-1): print(end=" ") # Inner loop to handle number of asterisks for k in range(0, i+1): print("* ", end="") # Ending line after each row print("\r")

**Output**

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

**Explanation:**

Let's break down this code to understand how it works. First, we define the number of rows we want to print, which in this case is five. Then, we use an outer loop to handle the number of rows. Inside the outer loop, we use two inner loops to handle the number of spaces and asterisks. - The first inner loop handles the number of spaces that come before the asterisks in each row. We start with the maximum number of spaces and reduce the number of spaces as we move down the pyramid. The formula we use to calculate the number of spaces is rows-i-1. - The second inner loop handles the number of asterisks in each row. We start with one asterisk in the first row and add one more asterisk in each subsequent row. The formula we use to calculate the number of asterisks is i+1. - Finally, we add a line break after each row using the print("\r") statement. This ensures that the next row starts on a new line. - In conclusion, the Python code we have written prints a pyramid of asterisks using loops and conditional statements. This pattern is a great way to practice your programming skills, and it can also be used to create interesting designs. By using this code, you can easily print the pyramid of asterisks in your Python program.

## 0 Comments