Python Pattern Programs


Pattern Programs

Writing the pattern programs is an excellent way to enhance your problem-solving skills, and logical building skills and along with that, you will gain a lot of experience using the loops and conditional statements.

Code for each pattern can be written in multiple ways, using the for loop, using the while loops, and logic for each can be written in different ways. So we encourage you to try recreating each pattern using some new technique.

Star Pattern Programs

Half Pyramid Pattern

Pattern:

Please enter the number of rows : 6
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * * 

Code: Approach 1

rows = int(input('Please enter the number of rows : '))

for i in range(1, rows+1):
    print('* '  * i)

Code: Approach 2

rows = int(input('Please enter the number of rows : '))

for i in range(1, rows+1):
    for j in range(i):
        print('*', end=' ')
    print('')

Right Triangle Pyramid Pattern

Pattern:

Please enter the number of rows : 6
          * 
        * * 
      * * * 
    * * * * 
  * * * * * 
* * * * * * 

Code: Approach 1

rows = int(input('Please enter the number of rows : '))

for i in range(1, rows+1):
    print((rows - i) * '  ' + ('* ') * i)

Code: Approach 2

rows = int(input('Please enter the number of rows : '))

for i in range(1, rows+1):
    for j in range(rows - i):
        print(' ', end=' ')
    for k in range(i):
        print('*', end=' ')
    print('')

Downward Half Pyramid Pattern

Pattern:

Please enter the number of rows : 6
* * * * * * 
* * * * * 
* * * * 
* * * 
* * 
* 

Code: Approach 1

rows = int(input('Please enter the number of rows : '))

for i in range(rows, 0, -1):
    print( ('* ') * i)

Code: Approach 2

rows = int(input('Please enter the number of rows : '))

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

Downward Inverted Half Pyramid – Right Down Mirror Star Pyramid

Pattern:

Please enter the number of rows : 6
* * * * * * 
  * * * * * 
    * * * * 
      * * * 
        * * 
          * 

Code: Approach 1

rows = int(input('Please enter the number of rows : '))

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

Code: Approach 2

rows = int(input('Please enter the number of rows : '))

for i in range(rows, 0, -1):
    for j in range(rows - i):
        print(' ', end=' ')
    for k in range(i):
        print('*', end=' ')
    print('')

Downward Full Pyramid Pattern

Pattern:

Please enter the number of rows : 6
* * * * * * 
 * * * * * 
  * * * * 
   * * * 
    * * 
     * 

Code: Approach 1

rows = int(input('Please enter the number of rows : '))

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

Code: Approach 2

rows = int(input('Please enter the number of rows : '))

for i in range(rows, 0, -1):
    for j in range(rows - i):
        print(' ', end='')
    for k in range(i):
        print('*', end=' ')
    print('')

Upward Full Pyramid Pattern

Pattern:

Please enter the number of rows : 6
     * 
    * * 
   * * * 
  * * * * 
 * * * * * 
* * * * * * 

Code: Approach 1

rows = int(input('Please enter the number of rows : '))

for i in range(1, rows+1):
    print((rows - i) * ' ' + ('* ') * i)

Code: Approach 2

rows = int(input('Please enter the number of rows : '))

for i in range(1, rows + 1):
    for j in range(rows - i):
        print(' ', end='')
    for k in range(i):
        print('*', end=' ')
    print('')

Diamond Shaped Pyramid Pattern

Pattern:

Please enter the number of rows : 6
     * 
    * * 
   * * * 
  * * * * 
 * * * * * 
* * * * * * 
 * * * * * 
  * * * * 
   * * * 
    * * 
     * 

Code:

rows = int(input('Please enter the number of rows : '))

for i in range(1, rows):
    for j in range(rows - i):
        print(' ', end='')
    for k in range(i):
        print('*', end=' ')
    print('')
    

for i in range(rows, 0, -1):
    for j in range(rows - i):
        print(' ', end='')
    for k in range(i):
        print('*', end=' ')
    print('')

Sand glass Pyramid Pattern

Please enter the number of rows : 6
* * * * * * 
 * * * * * 
  * * * * 
   * * * 
    * * 
     * 
     * 
    * * 
   * * * 
  * * * * 
 * * * * * 
* * * * * * 

Code:

rows = int(input('Please enter the number of rows : '))

for i in range(rows, 0, -1):
    for j in range(rows - i):
        print(' ', end='')
    for k in range(i):
        print('*', end=' ')
    print('')

for i in range(1, rows + 1):
    for j in range(rows - i):
        print(' ', end='')
    for k in range(i):
        print('*', end=' ')
    print('')

Number Pattern Programs

Number Pattern of Same Number Per Row

Pattern:

Please enter the number of rows : 6
1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5 
6 6 6 6 6 6 

Code: Approach 1

rows = int(input('Please enter the number of rows : '))

for i in range(1, rows+1):
    for j in range(i):
        print(i, end=' ')
    print('')

Code: Approach 2

rows = int(input('Please enter the number of rows : '))

for i in range(1, rows+1):
    print((str(i) + ' ') * i)

Number Pattern of Increasing Numbers

Pattern:

Please 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 

Code:

rows = int(input('Please enter the number of rows : '))

for i in range(1,rows+1):
    for j in range (1,i+1):
        print(j, end =  ' ')
    print('')

Inverted Pyramid Pattern of Numbers

Pattern:

Please enter the number of rows : 6
1 1 1 1 1 1 
2 2 2 2 2 
3 3 3 3 
4 4 4 
5 5 
6 

Code:

rows = int(input('Please enter the number of rows : '))

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

Inverted Pyramid Pattern of Increasing Numbers

Pattern:

Please enter the number of rows : 6
1 2 3 4 5 6 
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 

Code:

rows = int(input('Please enter the number of rows : '))

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

Inverted Pyramid Pattern with The Same Digit

Pattern:

Please enter the number of rows : 6
6 6 6 6 6 6 
6 6 6 6 6 
6 6 6 6 
6 6 6 
6 6 
6 

Code:

rows = int(input('Please enter the number of rows : '))

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

Alternate Numbers Pyramid Pattern

Pattern:

Please enter the number of rows : 5
1 
3 3 
5 5 5 
7 7 7 7 
9 9 9 9 9 

Code:

rows = int(input('Please enter the number of rows : '))

for i in range(1,rows+1):
    for j in range (1,i+1):
        print(i*2-1, end =  ' ')
    print('')

Inverted Pyramid Pattern with Reverse Numbers

Pattern:

Please 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 

Code:

rows = int(input('Please enter the number of rows : '))

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

Pyramid Pattern of Reverse Numbers

Pattern:

Please enter the number of rows : 6
1 
2 1 
3 2 1 
4 3 2 1 
5 4 3 2 1 
6 5 4 3 2 1 

Code:

rows = int(input('Please enter the number of rows : '))

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

Inverted Pyramid Pattern of Reverse Numbers

Pattern:

Please enter the number of rows : 6
6 5 4 3 2 1 
5 4 3 2 1 
4 3 2 1 
3 2 1 
2 1 
1 

Code:

rows = int(input('Please enter the number of rows : '))

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

Pyramid Pattern of Sequential Numbers

Pattern:

Please 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 

Code:

num  = 1
rows = int(input('Please enter the number of rows : '))

for i in range(1, rows+1):
    for j in range(i):
        print(num, end=' ')
        num  += 1
    print('')

Pyramid Pattern of Sequential Numbers – Reverse in each line

Pattern:

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

Code:

temp_num  = 1
rows = int(input('Please enter the number of rows : '))

for i in range(1, rows+1):
    num = temp_num + i - 1
    for j in range(i):
        print(num, end=' ')
        num  -= 1
    print('')
    temp_num = temp_num + i

Triangle Pattern

Pattern:

Please enter the number of rows : 6
          1 
        2 2 
      3 3 3 
    4 4 4 4 
  5 5 5 5 5 
6 6 6 6 6 6 

Code:

rows = int(input('Please enter the number of rows : '))

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

Alphabet Pattern Programs

Pyramid Alphabet Pattern – Same Alphabet

Pattern:

Please enter the number of rows : 6
A 
B B 
C C C 
D D D D 
E E E E E 
F F F F F F 

Code: Approach 1

rows = int(input('Please enter the number of rows : '))

for i in range(rows):
    for j in range(i+1):
        print(chr(i + 65), end=' ')
    print('')

Code: Approach 2

rows = int(input('Please enter the number of rows : '))

for i in range(1, rows+1):
    print((chr(i + 64) + ' ') * i)

Pyramid Alphabet Pattern – Different Alphabet

Pattern:

Please enter the number of rows : 6
A 
A B 
A B C 
A B C D 
A B C D E 
A B C D E F 

Code:

rows = int(input('Please enter the number of rows : '))

for i in range(rows):
    for j in range (i+1):
        print(chr(j + 65), end =  ' ')
    print('')

Triangle Alphabet Pattern

Pattern:

Please enter the number of rows : 6
            A 
          B B 
        C C C 
      D D D D 
    E E E E E 
  F F F F F F 

Code:

rows = int(input('Please enter the number of rows : '))

for i in range(rows):
    print("  " * (rows - i), end = '')
    for j in range(i+1):
        print(chr(i + 65), end= ' ')
    print('')

Upward Full Pyramid

Pattern:

Please enter the number of rows : 6
     A 
    A B 
   A B C 
  A B C D 
 A B C D E 
A B C D E F 

Code:

rows = int(input('Please enter the number of rows : '))

for i in range(1, rows + 1):
    for j in range(rows - i):
        print(' ', end='')
    for k in range(i):
        print(chr(k + 65), end=' ')
    print('')

Upside-Down Full Pyramid

Pattern:

Please enter the number of rows : 6
A B C D E F 
 A B C D E 
  A B C D 
   A B C 
    A B 
     A 

Code:

rows = int(input('Please enter the number of rows : '))

for i in range(rows, 0, -1):
    for j in range(rows - i):
        print(' ', end='')
    for k in range(i):
        print(chr(k+65), end=' ')
    print('')

Diamond Shaped Pyramid Pattern

Pattern:

Please enter the number of columns : 6
     A 
    A B 
   A B C 
  A B C D 
 A B C D E 
A B C D E F 
 A B C D E 
  A B C D 
   A B C 
    A B 
     A 

Code:

rows = int(input('Please enter the number of columns : '))

for i in range(1, rows):
    for j in range(rows - i):
        print(' ', end='')
    for k in range(i):
        print(chr(k+65), end=' ')
    print('')
    

for i in range(rows, 0, -1):
    for j in range(rows - i):
        print(' ', end='')
    for k in range(i):
        print(chr(k+65), end=' ')
    print('')

Sand glass Pyramid Pattern

Pattern:

Please enter the number of columns : 6
A B C D E F 
 A B C D E 
  A B C D 
   A B C 
    A B 
     A 
    A B 
   A B C 
  A B C D 
 A B C D E 
A B C D E F 

Code:

rows = int(input('Please enter the number of columns : '))

for i in range(rows, 1, -1):
    for j in range(rows - i):
        print(' ', end='')
    for k in range(i):
        print(chr(k + 65), end=' ')
    print('')

for i in range(1, rows + 1):
    for j in range(rows - i):
        print(' ', end='')
    for k in range(i):
        print(chr(k + 65), end=' ')
    print('')

Square Pattern – Same Alphabet

Pattern:

Please enter the number of rows : 6
A A A A A A 
B B B B B B 
C C C C C C 
D D D D D D 
E E E E E E 
F F F F F F 

Code:

rows = int(input('Please enter the number of rows : '))

for i in range(rows):
    for j in range(rows):
        print(chr(i + 65), end=' ')
    print('')

Square Pattern – Different Alphabet

Pattern:

Please enter the number of rows : 6
A B C D E F 
A B C D E F 
A B C D E F 
A B C D E F 
A B C D E F 
A B C D E F 

Code:

rows = int(input('Please enter the number of rows : '))

for i in range(rows):
    for j in range(rows):
        print(chr(j + 65), end=' ')
    print('')