Pattern program in Python

Pattern program in Python

Hello Techies,

Pattern program in Python is one of the most frequently asked questions in the interview process. In this blog, we will learn how to draw different shapes in Python.

In my previous blog, I covered programs based on the shapes of squares and triangles.

In this blog, let’s check out other than the triangle and square shapes program.

Different shapes Pattern program in Python

We are writing below patterns program

  1. Star pattern programs
  2. Number pattern programs

Star pattern programs

#1. Pyramid pattern in Python

input_number = int(input("Enter the number of rows:"))

for i in range(0, input_number):
    for j in range(0, input_number-i-1):
        print(end=" ")
    for j in range(0, i+1):
        print("*", end=" ")
    print()

After executing the above code, the following results will be obtained –

Enter the number of rows:5
    *
   * *
  * * *
 * * * *
* * * * *

#2. Inverted pyramid pattern program in Python

input_number = int(input("Enter the number of rows:"))

for i in range(input_number, 0, -1):
    for j in range(0, input_number-i):
        print(end=" ")
    for j in range(0, i):
        print("*", end=" ")
    print()

After executing the above code, the following results will be obtained –

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

#3. Hollow Pyramid pattern program in Python

input_number = int(input("Enter the number of rows:"))

k = 2
for i in range(1, input_number+1):
    for j in range(1, 2*input_number):
        if i+j==input_number+1 or j-i==input_number-1:
            print("*", end="")
        elif i==input_number and j!=k:
            print("*", end="")
            k=k+2
        else:
            print(end=" ")
    print()

After executing the above code, the following results will be obtained –

Enter the number of rows:5
    *
   * *
  *   *
 *     *
* * * * *

#4. Hollow diamond pattern program in Python

input_number = int(input("Enter the number of rows:"))

for i in range(input_number):
    for j in range(input_number):
        if i+j==3 or j-i==3 or i-j==3 or i+j==9:
            print("*", end="")
        else:
            print(end=" ")
    print()

After executing the above code, the following results will be obtained –

Enter the number of rows:7
   *
  * *
 *   *
*     *
 *   *
  * *
   *

#5. Diamond pattern program in Python

input_number = int(input("Enter the number of rows:"))

for i in range(input_number):
    print(''*(input_number-i-1)+'* '*(i+1))
for j in range(input_number-1, 0, -1):
    print('' * (input_number - j) + '* ' * (j))

After executing the above code, the following results will be obtained –

Enter the number of rows:7
      *
     * *
    * * *
   * * * *
  * * * * *
 * * * * * *
* * * * * * *
 * * * * * *
  * * * * *
   * * * *
    * * *
     * *
      *

Number pattern programs

#1. Pascal Triangle pattern in Python

input_number = int(input("Enter the number of rows:"))

list1 = []
for i in range(input_number):
    temp_list = []
    for j in range(i+1):
        if j==0 or j==i:
            temp_list.append(1)
        else:
            temp_list.append(list1[i-1][j-1] + list1[i-1][j])
    list1.append(temp_list)

for i in range(input_number):
    for j in range(input_number - i -1):
        print(format(" ", "<2"), end="")
    for j in range(i + 1):
        print(format(list1[i][j], "<3"), end=" ")
    print()

After executing the above code, the following results will be obtained –

Enter the number of rows:6
          1
        1   1
      1   2   1
    1   3   3   1
  1   4   6   4   1
1   5   10  10  5   1

#2. Reverse Number pattern in python

input_number = int(input("Enter the number of rows:"))

for i in range(1, input_number):
    for j in range(i, 0, -1):
        print(j, end=" ")
    print()

After executing the above code, the following results will be obtained –

Enter the number of rows:6
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1

#3. Floyd’s triangle in python

input_number = int(input("Enter the number of rows:"))

num =1
for i in range(1, input_number+1):
    for j in range(1, i+1):
        print(num, end=" ")
        num=num+1
    print("")

After executing the above code, the following results will be obtained –

Enter the number of rows:6
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21

#4. Pyramid Pattern of Numbers in Python

input_number = int(input("Enter the number of rows:"))

for i in range(1, input_number+1):
    for j in range(1, input_number-i+1):
        print(end=" ")
    for j in range(i, 0, -1):
        print(j, end="")
    for j in range(2, i+1):
        print(j, end="")
    print("")

After executing the above code, the following results will be obtained –

Enter the number of rows:6
     1
    212
   32123
  4321234
 543212345
65432123456

#5. Inverted Pyramid of Descending Numbers in Python

input_number = int(input("Enter the number of rows:"))

for i in range(input_number, 0, -1):
    num = i
    for j in range(0, i):
        print(num, end= ' ')
    print()

After executing the above code, the following results will be obtained –

Enter the number of rows:6
6 6 6 6 6 6
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1

#6. Half Pyramid Pattern of Numbers in Python

input_number = int(input("Enter the number of rows:"))

for row in range(1, input_number+1):
    for column in range(1, row + 1):
        print(column, end=" ")
    print()

After executing the above code, the following results will be obtained –

Enter the number of rows:6
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6

#7. Inverted mirrored right triangle Numbers in Python

input_number = int(input("Enter the number of rows:"))

for i in range(input_number+1, 1, -1):
    print(" "*(input_number+1-i), end="")
    for j in range(1, i):
        print(j, end="")
    print()

After executing the above code, the following results will be obtained –

Enter the number of rows:6
123456
 12345
  1234
   123
    12
     1

I have covered a different pattern program in python in this blog. Comment below if you want any other kind of pattern program in Python.

Leave a Comment