Star patterns (pyramid patterns) in python is a common question in programming exams and sometimes in interview questions. This can be tricky at some times because there are some complicated patterns too. However, there are various patterns like a pyramid, number, alphabet, and asterisk patterns. We will see the printing of different patterns of stars simply.
In Python, a for loop is used to print the various patterns.
- The outer loop prints the number of rows.
- The inner loops print the number of columns.
- The variable is to print white space according to the required place in Python.
Do you still face difficulties in understanding the logic? That’s perfectly fine; we have covered a very easy way to understand.
For all the practice Videos and Explanations on Python, please click over here. Python Practice Series.
Table of Contents
Star Patterns in Python
Method 1: Star Pyramid Pattern I
Source code:
n = int(input("Enter the number of rows:")) for i in range(0, n): for j in range(0, i + 1): print("* ", end="") print()
Output:
*
* *
* * *
* * * *
* * * * *
Your YouTube video is available to watch here.
Code Explanation Method 1: Star Pyramid Pattern I
In the above code, we have initialized the n variable. We are accepting a number from the user and converting it into an int. Then in the next line, we have written for loop with counter variable I, and this loop will run from 0 to n, but as in range, the last number is opted out hence loop will run till n-1.
This loop is written to print the number of rows. Inside the indent of the for loop, we have another for loop written for the printing star. But look here carefully this loop is changing according to the outer loop. The iteration of the inner for loop depends on the outer loop. The inner loop is responsible for printing the number of columns.
In the first iteration, the value of i is 0, and it increased by 1, so it becomes 0+1. Now, the inner loop is iterated the first time and prints one star(*). In the second iteration, the value of i is 1, and it increased by 1, so it becomes 1+1. Now the inner loop is iterated two times and prints two-star (* *).
Here if you look at the print statement here, we have used the end parameter. By default, the print method ends with a new line. This means there is no need to explicitly specify the parameter end as ‘\n’. But we want to print two or more stars in the same line hence we have defined a blank value to the end parameter hence the program won’t print the star on the next line.
The end argument prevents jumping into another line. It will print the star until the loop is valid. The last print statement is inside the first for loop; it is responsible for ending the line after each row means it will add the next line in the program.
Method 2: Star Pyramid Pattern II
Source Code:
num_rows = 5 for i in range(0, num_rows): for j in range(0, num_rows-i-1): print( end=" ") for j in range(0, i+1): print("*", end=" ") print()
Output:
*
* *
* * *
* * * *
* * * * *
Code Explanation Method 2: Star Pyramid Pattern II
In the above code, we have initialized the num_rows variable. We are accepting numbers from users and converting them into an int. Then in the next line, we have written for loop with counter variable I, and this loop will run from 0 to 4 as the range’s last number is opted out hence the loop will run till 4.
This loop is written to print the number of rows. Inside the indent of the for loop, we have another for loop written for adding space before the star. Hence we have used the end parameter blank. This loop will run till num_rows -i-1, so at the first iteration, we have num_rows value 5 and I have 0 value, so the loop will run for 4 times; next as I will increase by one loop will run for 3 and so on.
Now we have a next for loop that is the same as we have used in the last program, which will print stars in the same line. The end argument prevents jumping into another line. It will print the star until the loop is valid. The last print statement is inside the first for loop; it is responsible for ending the line after each row means it will add the next line in the program.
Method 3: Star Pyramid Pattern III
Source Code:
rows = 5 for i in range(rows + 1, 0, -1): for j in range(0, i - 1): print("*", end=' ') print(" ")
Output:
* * * * *
* * * *
* * *
* *
*
Code Explanation Method 3: Star Pyramid Pattern III
Here we have a variable called rows with a defined value of 5. In the above program, we have used a reverse nested loop, the first time we are using a reverse nested loop while printing a star pattern.
Here we have for loop with i as counter and in range, we have used 3 parameters, at the first position we have initiated initial value it is rows +1 and loop will run till it gets 0 and at the last or 3rd position we have increment or step here we are defined -1 hence it is a decrementing loop.
This loop is used for printing pattern rows. Inside this loop, we have another loop which is for printing stars. This is also a reverse loop here. We have initiated a loop at 0 and it will go to the value of i-1 so, in the first iteration, it is 6 hence it will go till 6-1 times, and inside we have a print statement to print star and end character as blank value for printing star on the same line.
The last print statement is inside the main loop and is used for printing the last line. The last print statement is inside the first for loop, it is responsible for ending the line after each row means it will add the next line in the program.