Introduction to Star Patterns in Python | Types of Star patterns in Python with Example?



Introduction to Star Patterns in Python

In these star patterns in the python article, we will see the printing of different patterns of stars in a simple manner. You will learn patterns of various types like a pyramid, number, alphabet, asterisk pattern, etc. To print these star patterns, you only need to know the basics of python programming like the use of for loop, if loop, the input, and the print functions. And even if you are new to python, we assure you will learn this topic at ease. Here, there will be different patterns created by writing different lines of code, but the basic program consists of two for loops were in the first outer for loop for rows and the other inner for loop for columns in the pattern. And, of course, the print function to print the output and the input function to get the user input. Also, the range function’s use will iterate the loop between the starting range mainly from 0 and ends with an integer number whatever the user inputs. Firstly we will see the logic behind the printing of the pattern of stars.

👉What is Star Patterns in Python?

In the star pattern program, we will ask the user to input the number of rows that says 5, then using a variable I, the outer for loop iterates using range function starting from 0, which ends with 5. Further, using variable j, the inner for loop iterates using range function again for the printing of spaces. Next, again using the variable j, the innermost for loop for the printing of stars and then control will go to the next line, which is the last step in the program, the print function. And this will work for i= 0 row, i=1 row, i=2 row, i= 3 row, and i=4 row and depending on these I values, the next two for loops will be processed.

Point to Remember

You will find the same syntax behind each program with a little variation (like using a variable k to increment value within the loop and to print it); the rest is the same.

👉Types of Star patterns in Python

Here we are to see the different type of Star Patterns in python.

code #1

# Program to print full pyramid
num_rows = int(input("Enter the number of rows"));
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 

Star pattern full pyramid

code #2

In this program, stars are printed from the very first column. Here, we have used only one loop to print the stars. There will not be any other loop to print stars. Also, the print(“* “, end=”) function in the following program prints only the star accompanied by a space. Here are the program and the output for further understanding.

#Program to print Left Half Pyramid
num_rows = int(input("Enter the number of rows"));
k = 1
for i in range(0, num_rows):
for j in range(0, k):
print("* ", end="")
k = k + 1
print()

Output 

Star pattern Half pyramid

code #3

#Program to print Right Half Pyramid
num_rows = int(input("Enter the number of rows"));
k = 8
for i in range(0, num_rows):
for j in range(0, k):
print(end=" ")
k = k - 2
for j in range(0, i+1):
print("* ", end="")
print()

Output 

Star pattern Right half

code #4

# Program to print One More Star Pattern Pyramid
print("Program to print star pattern: \n");
rows = input("Enter maximum stars you want display on a single line")
rows = int (rows)
for i in range (0, rows):
for j in range(0, i + 1):
print("* ", end='')
print("\r")
for i in range (rows, 0, -1):
for j in range(0, i -1):
print("* ", end='')
print("\r")

Output 

Star pattern left half

code #5

print("Program to print star pattern in different style: \n");
num_rows = int(input('Please enter the number of rows'));
for i in range (0,num_rows):
for j in range (num_rows,i,-1):
print("* ", end="")
print()

Output 

Star Patterns - Example 5

code #6

num_rows = int(input("Please enter the number of rows"));
for i in range(num_rows,0,-1):
for j in range(0, num_rows-i):
print(end=" ")
for j in range(0,i):
print("* ", end=" ")
print()

Output

 Example 6

code #7

This program will print the full diamond star pattern, which uses two loops, the first top half and the second bottom half. In the first top half, we will have one for loop and one while loop same holds true for the second bottom half as well. In each half, the for loop is used to print spaces, and the while loop is used to print stars.

num_rows = int(input("Enter the number of rows"))
k = 0
for i in range(1, num_rows + 1):
for j in range (1, (num_rows - i) + 1):
print(end = " ")
while k != (2 * i - 1):
print("*", end = "")
k = k + 1
k = 0
print()
k = 2
m = 1
for i in range(1, num_rows):
for j in range (1, k):
print(end = " ")
k = k + 1
while m <= (2 * (num_rows - i) - 1):
print("*", end = "")
m = m + 1
m = 1
print()

Output 

full Star diamond

Conclusion

I hope this article helped you; we have tried our level best to make it simpler to understand in all the programs. These star patterns are easy to learn if you understand the logic and work on it. All you then have to do is practice the programs multiple times so as to achieve expertise on this topic.

Recommended Article

This has been a guide to Star Patterns in Python. Here we discuss the Introduction and different types of Star Patterns in Python along with the appropriate program and its output. You can also go through our other suggested articles to learn more –

Post a Comment

3 Comments

  1. Your think is good but this useful Post of article, 🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬👌👌👌👌👌👌👌👌👌👌👌👌👌👌👏👏👏👏👏👏👏👏👏👏👏👏👏👌💕💕💕💕💕👌👌👌👏👏👏👍👏👌👌💕👌👏

    ReplyDelete