Star(asterisk) pattern in Python

In this tutorial, you are going to learn about the star or asterisk pattern in Python. Star or asterisk patterns are a series of * which forms a pattern or any geometrical shape like triangle, square, rhombus etc. These patterns are created with the help of for loop. For the good understanding of the topic, you should know about the for loop. So let’s learn how to print various start patterns in Python.

A for loop can iterate over the item of any sequence (such as string or a list). For the first iteration of the loop, the list is evaluated and the first item of the list is assigned to the iterating variable “iterating_var” then the body of the for loop is executed. Each item of the list is assigned to the “iterating_var” and the body of for will be executed until all the list items are exhausted. Nested for loop is used in the program to make a star or asterisk pattern.

Syntax:

for iterating_var in sequence:
          
    body of for
list=[1,2,2,3,4]  # list

# by sequence
print("By sequence: ",end=" ")
for l in list:
      print(l,end=" ")

# by range
print("By range: ",end=" ")
for l in range(6):
      print(l,end=" ")

Output:-

By sequence: 1 2 2 3 4
By range: 0 1 2 3 4 5

Inverted right angled triangle star pattern in Python

  1. The outer loop gives i=0 in the first iteration and goes to the inner loop which will work for the range (0,6-i) and print the star(*) for 6 time in a line and the inner loop work is completed.
  2. After that it will come to next line by print().
  3. The outer loop will iterate again to give i=i+1. Repeat all the steps again until i=5.
# Outer loop
for i in range(0,6):

    # Inner loop
    for j in range(0,6-i):
        print("*",end="")
    print()

Output:-

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

Mirrored inverted right angled triangle pattern in Python

  1. The outer loop gives i=0 in the first iteration and goes to the inner loop 1 to print the space for a range of (o,i) and print no space for the first line.
  2. After completing the inner loop 1 then it goes to inner loop 2 to print the star(*) for a range of (0,6-i) and print the 6 star in the same line.
  3. After that inner loop 2 is completed and the pointer goes to the next line by print().
  4. Then the outer loop will iterate for the second time. Repeat all the above steps again to form the pattern.
  5. The outer loop will continue its work until i=5.
# Outer loop
for i in range(0,6): 
    # Inner loop 1 
    for k in range(0,i):
        print(" ",end="")
    # Inner loop 2
    for j in range(0,6-i):
        print("*",end="")
    print()

Output:-

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

Square star pattern in Python

  1. The outer loop gives i=0 in the first iteration and goes to the inner loop which will work for the range (0,5) and print the star(*) for 5 time in a line and the inner loop work is completed.
  2. After that it will come to next line by print().
  3. The outer loop will iterate again to give i=i+1.  Repeat all the steps again until i=4.
# Outer loop
for i in range(0,5):
    # Inner loop
    for j in range(0,5):
        print("*",end="")
    print()

Output:-

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

Mirrored right angled triangle pattern in Python

  1. The outer loop gives i=0 in the first iteration and goes to the inner loop 1 to print the space for a range of (o,5-i) and print 5 spaces for the first line.
  2. After completing the inner loop 1 then it goes to inner loop 2 to print the star(*) for a range of (0,i+1). Print star only one time in the same line.
  3. After that inner loop 2 is completed and the pointer goes to the next line by print().
  4. Then the outer loop will iterate for the second time. Repeat all the above steps again to form the pattern.
  5. The outer loop will iterate until i becomes 5.
# Outer loop
for i in range(0,6):
    # Inner loop 1
    for j in range(0,5-i):
        print(" ",end="")
    # Inner loop 2
    for k in range(0,i+1):
        print("*",end="")
    print()

Output:-

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

Python program to print non square numbers

Leave a Reply

Your email address will not be published. Required fields are marked *