Python Pattern Programs to print Star, Pascal, Pyramid, Number, Triangle

Python Pattern Programs

Hello people! Today we are back with another fantastic article on various Python Pattern Programs. We all know how brainstorming these types of Python Pattern Programs can be, thus, this time we published this tutorial to cover almost all the types of patterns that are there. In this article, we will talk about all the pattern programs in python along with their code.

All these Python Pattern Programs will be for Beginners to Advanced levels. We have majorly divided this article into three parts, on the basis of the types of patterns, namely, alphabetical patterns, numerical patterns, and symbolic patter. This article on pattern programs in python will roughly include 45 designs and an explanation. So let us move on to the list of Pattern Programs and start coding for it.

Numerical Python Pattern Programs

1. Printing numbers in the semi-pyramid pattern

n = 9

for n in range(0, n+1):

    for m in range(1, n + 1):

        print(m, end=" ")

    print("")

Output:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9

2. Numbers printed in an inverted semi-pyramid shape

n = 9

for n in range(0, n+1):

    for m in range(1, n + 1):

        print(m, end=" ")

    print("")

Output:

1 1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3
4 4 4 4
5 5 5
6 6
7

3. Same number printed in an inverted semi-pyramid shape

n = 8

digit = n

for k in range(n, 0, -1):

    for m in range(0, k):

        print(digit, end=' ')

    print("\r")

Output:

8 8 8 8 8 8 8 8
8 8 8 8 8 8 8
8 8 8 8 8 8
8 8 8 8 8
8 8 8 8
8 8 8
8 8
8

4. Numbers in an inverted semi-pyramid pattern

rows = 5

for i in range(rows, -1, -1):

    for j in range(0, i + 1):
        print(j, end=' ')
        
    print("\r")

Output:

0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0

5. Numbers printed in an inverted semi-pyramid shape in descending order

n = 7

for k in range(n, 0, -1):

    digit = k

    for m in range(0, k):

        print(digit, end=' ')

    print("\r")

Output:

7 7 7 7 7 7 7 7
6 6 6 6 6 6
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1

6. Alternate numbers printed in a semi-pyramid pattern

rows = 5
i = 1
while i <= rows:
    j = 1
    while j <= i:
        print((i * 2 - 1), end=" ")
        j = j + 1
    i = i + 1
    print('')

Output:

1
3 3
5 5 5
7 7 7 7
9 9 9 9 9

7. Reverse numbers in the semi-pyramid shape

rows = 6

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

Output:

1
2 1
3 2 1
4 3 2 1
5 4 3 2 1

8. 1 to 15 numbers in the semi-pyramid pattern

start = 1
stop = 2
current_num = stop
for row in range(2, 7):
    for col in range(start, stop):
        current_num -= 1
        print(current_num, end=' ')
    print("")
    start = stop
    stop += row
    current_num = stop

Output:

1
3 2
6 5 4
10 9 8 7
15 14 13 12 11

9. Numbers in a right-angled triangle pattern

rows = 6
for i in range(1, rows):
    num = 1
    for j in range(rows, 0, -1):
        if j > i:
            print(" ", end=' ')
        else:
            print(num, end=' ')
            num += 1
    print("")

Output:

        1 
      1 2
    1 2 3
  1 2 3 4
1 2 3 4 5

10. Pascal’s triangle pattern

def print_pascal_triangle(size):
    for i in range(0, size):
        for j in range(0, i + 1):
            print(decide_number(i, j), end=" ")
        print()


def decide_number(n, k):
    num = 1
    if k > n - k:
        k = n - k
    for i in range(0, k):
        num = num * (n - i)
        num = num // (i + 1)
    return num

# set rows
rows = 7
print_pascal_triangle(rows)

Output:

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1

11. Numbers in a connected pyramid pattern

n = 8

for k in range(0, n):

    for m in range(n-1, k-1):

        print(m, end="")

    for j in range(k):

        print(' ', end="")

    for h in range(k + 1, n):

        print(h, end="")

    print('\n')

Output:

1234567

 234567

  34567

   4567

    567

     67

      7

12. Different numbers in a square pattern

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

Output:

1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

13. Printing unique pattern

n = 8

for k in range(1, n + 1):

    for m in range(1, k-1):

        print(m, end=" ")

    for m in range(k-1, 0, -1):

        print(m, end=" ")

    print()

Output:

1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 6 7 6 5 4 3 2 1

14. Even numbers printed in a pyramid pattern

n = 8

last_even = 2 * n
even_num = last_even

for k in range(1, n+1):
    even_num = last_even
    for m in range(k):
        print(even_num, end=' ')
        even_num -= 2
    print("\r")

Output:

16
16 14
16 14 12
16 14 12 10
16 14 12 10 8
16 14 12 10 8 6
16 14 12 10 8 6 4
16 14 12 10 8 6 4 2

15. Printing rectangle shape

rows = 3
columns = 8

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

Output:

1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3

16. Inverted triangle pattern with numbers

n = 5

for i in range(n):

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

Output:

123456789
 1234567
  12345
   123
    1

17. Printing squares of numbers pattern

l_number = 9
for i in range(1, l_number):
    for j in range(-1+i, -1, -1):
        print(format(2**j, "4d"), end=' ')
    print("")

Output:

1
2 1
4 2 1
8 4 2 1
16 8 4 2 1
32 16 8 4 2 1
64 32 16 8 4 2 1
128 64 32 16 8 4 2 1

18. Empty triangle pattern

n = 5
for i in range(n):
    
    for j in range(n - i - 1):
        print(' ', end='')

   
    count = 1
    for k in range(2 * i + 1):
        
        if k == 0 or k == 2 * i:
            print(count, end='')
            count += 1
        else:
            if i == n - 1:
                print(count, end='')
                count += 1
            else:
                print(' ', end='')
    print()

Explanation:

Here for the formation of this type of triangle, we made use of 2 for loops and some conditionals. The first loop works for the handling of the formation of the number of rows. The other for loop actually works for the printing of the numbers in that particular row. Here we have coded in such a way that from the third row, we have started putting empty spaces in between. This was done using the if-else conditionals.

Output:

    1
   1 2
  1   2
 1     2
123456789

19. Triangle filled with numbers pattern

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

Output:

      1
     123
    12345
   1234567
  123456789
1234567891011

20. Empty right-sided triangle

n = 10
for i in range(1, n+1):
    count = 1
    for j in range(i):
    
        if j == 0 or j == i-1:
            print(count, end='')
            count += 1
      
        else:
            if i != n:
                print(' ', end='')
            else:
                print(count, end='')
                count += 1
    print()

Explanation:

This is the almost same case as an empty triangle. The only difference is that here we are starting from the margin. Keep in mind a few things when creating this: print just numbers in the first and last row, and in all other rows, print only numbers in the first and last positions, with the remaining gaps.

Output:

1
12
1 2
1  2
1   2
1    2
1     2
1      2
1       2
12345678910

Alphabetical Python Pattern Programs

Moving on to another category in our article on Python Pattern Programs and that is patterns using alphabets. The main logic behind making pattering using alphabets is that at first, we need to convert it to ASCII code, and then we can work on our pattern. So let us start with our first pattern in this list of Alphabetical Python Pattern Programs.

21. Alphabets in a square pattern

size = 5
count = 0

for i in range(size):
    for j in range(size):
        print(chr(65 + count), end=" ")
        
        count += 1
    print()

Explanation:

The character changes every time in the pattern above, which has 5 rows and 5 columns.
Simply design two nested for loops, the outer loop repeating a row and the inner loop printing the character in a column, to do this. You may set a number and increase it by 1 each time in the inner loop to alter the character on each iteration.

Output:

A B C D E
F G H I J
K L M N O
P Q R S T
U V W X Y

22. Printing a square of the same alphabet

size = 5

for i in range(size):
    for j in range(size):
        print(chr(65 + i), end=" ")
    print()

Output:

A A A A A 
B B B B B 
C C C C C 
D D D D D 
E E E E E

23. Left-sided triangle with alphabets

n = 5
for i in range(n):
    for j in range(i+1):
        print(chr(j + 65), end=" ")
    print()

Output:

A
A B
A B C
A B C D
A B C D E

24. Full pyramid alphabet pattern

n = 5
for i in range(n):
    for j in range(n - i - 1):
        print(' ', end=' ')
    for k in range(2 * i + 1):
        print(chr(65 + k), end=' ')
    print()

Output:

       A 
     A B C
   A B C D E
 A B C D E F G
A B C D E F G H I

25. Reverse full pyramid pattern

n = 5

for i in range(n):

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

Output:

A B C D E F G H I 
  A B C D E F G
    A B C D E
      A B C
        A

26. Diamond pattern with alphabets

n = 5

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

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

Explanation:

If you look closely, you will notice that the diamond design is divided into two pieces. The first half is identical to the pyramid pattern, while the second part is identical to the reverse pyramid pattern. Therefore, you may execute two sets of loops that print the pattern’s upward and downward components to make this.

Output:

          A      
        A B C    
      A B C D E  
    A B C D E F G 
  A B C D E F G H I
    A B C D E F G
      A B C D E
        A B C
          A

27. Sandclock pattern with alphabets

n = 5

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

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

Output:

ABCDEFGHI
 ABCDEFG
  ABCDE
   ABC
    A
   ABC
  ABCDE
 ABCDEFG
ABCDEFGHI

28. Right pascal triangle

n = 5

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

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

Output:

A
AB
ABC
ABCD
ABCDE
ABCD
ABC
AB
A

29. Heart shape filled with alphabets

n = 6
for i in range(n//2, n, 2):
 
    for j in range(1, n-i, 2):
        print(" ", end="")
   
    for j in range(i):
        print(chr(65 + j), end="")
    
    for j in range(1, n-i+1, 1):
        print(" ", end="")
   
    for j in range(i):
        print(chr(65 + j), end="")
    print()


for i in range(n, 0, -1):
    for j in range(i, n):
        print(" ", end="")
    for j in range(i*2):
        print(chr(65 + j), end="")
    print()
 ABC   ABC
ABCDE ABCDE
ABCDEFGHIJKL
 ABCDEFGHIJ
  ABCDEFGH
   ABCDEF
    ABCD
     AB

30. All alphabets in a right-sided triangle

a = 65

for i in range(0,6):
   
    for j in range(0,i+1):
        char = chr(a)
        print(char,end="")
        a += 1
    print()

Output:

A
BC
DEF
GHIJ
KLMNO
PQRSTU

31. Inverted alphabets triangle

rows = 5
num = rows
a = 65

for i in range(rows, 0, -1):
  
    for j in range(0, i):
        char = chr(a)
        print(char+' ' ,end="")
        a += 1
    print("\r")

Output:

A B C D E 
F G H I
J K L
M N
O

32. “M” shaped pattern using the alphabet

rows_count = 6
a = 65
for i in range(0, rows_count):
    for j in range(rows_count - 1, i, -1):
        char = chr(a)
        print(char+' ' ,end="")
    for l in range(i):
        print('    ', end='')
    for k in range(i + 1, rows_count):
        print(char, '', end='')
    print('\n')
    a += 1   

Output:

A A A A A A A A A A 

B B B B     B B B B

C C C         C C C

D D             D D

E                 E

33. Printing full name pattern

str= "COPY ASSIGNMENT" 
# Outer loop
for i in range(0,15):
    # inner loop
    for j in range(0,i+1):
        print(str[j],end="")
    print()

Explanation:

In this Python Pattern Program, we simply aim to print a string in a different manner. The basic idea here is to print each letter along with the previous letter in a new row. This way in the end we will showcase a whole string that we passed in our program. We used it for loop along with a range function that will basically print the letters. The parent for loop will change the rows whenever the number of letters that needs to be printed is printed.

Output:

C
CO
COP
COPY
COPY
COPY A
COPY AS
COPY ASS
COPY ASSI
COPY ASSIG
COPY ASSIGN
COPY ASSIGNM
COPY ASSIGNME
COPY ASSIGNMEN
COPY ASSIGNMENT

34. Reverse alphabets printing pattern

for i in range(65,71):
    
    for j in range(i,64,-1):
        print(chr(j),end="")
    print()

Output:

A
BA
CBA
DCBA
EDCBA
FEDCBA

Symbolic Python Pattern Programs

35. Right-sided triangle using an asterisk

i = 8

for k in range(0, i):

    for m in range(0, k + 1):

        print("*", end=' ')

    print("\r")

Explanation:

This is the basic pattern wherein we made use of 2 for loops. One loop will keep track of the switching of rows and the other one will keep the track of printing.

Output:

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

36. Printing letter “A”

resultant="";    
for row in range(0,7):    
    for column in range(0,7):     
        if (((column == 1 or column == 5) and row != 0) or ((row == 0 or row == 3) and (column > 1 and column < 5))):    
            resultant=resultant+"*"    
        else:      
            resultant=resultant+" "    
    resultant=resultant+"\n"    
print(resultant);

Output:

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

37. Printing letter “B”

h="";    
for Row in range(0,7):    
    for Col in range(0,7):     
        if (Col == 1 or ((Row == 0 or Row == 3 or Row == 6) and (Col < 5 and Col > 1)) or (Col == 5 and (Row != 0 and Row != 3 and Row != 6))) :  
            h=h+"*"    
        else:      
            h=h+" "    
    h=h+"\n"    
print(h); 

Output:

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

38. Printing letter “C”

H="";    
for Row in range(0,7):    
    for Col in range(0,7):     
        if ((Col == 1 and (Row != 0 and Row != 6)) or ((Row == 0 or Row == 6) and (Col > 1 and Col < 5)) or (Col == 5 and (Row == 1 or Row == 5))):  
            H=H+"*"    
        else:      
            H=H+" "    
    H=H+"\n"    
print(H);  

Output:

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

39. Printing letter “D”

h="";    
for r_count in range(0,7):    
    for Col in range(0,7):     
        if (Col == 1 or ((r_count == 0 or r_count == 6) and (Col > 1 and Col < 5)) or (Col == 5 and r_count != 0 and r_count != 6)):  
            h=h+"*"    
        else:      
            h=h+" "    
    h=h+"\n"    
print(h);  

Output:

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

40. Printing letter “E”

h="";    
for r_count in range(0,7):    
    for Col in range(0,7):     
        if (Col == 1 or ((r_count == 0 or r_count == 6) and (Col > 1 and Col < 6)) or (r_count == 3 and Col > 1 and Col < 5)):  
            h=h+"*"    
        else:      
            h=h+" "    
    h=h+"\n"    
print(h);    

Output:

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

41. Printing letter “Q”

def print_pattern(n):
    for r_count in range(n):
        for c_count in range(n):
            if (

              
              (r_count == 0 and (c_count != 0 and c_count != n-1)) or

              (r_count == n-2 and (c_count != 0 and c_count < n-2)) or

              (c_count == 0 and (r_count != 0 and r_count < n-2)) or

              (c_count == n - 1 and (r_count != 0 and r_count != n-2)) or

              ((n // 2 < r_count < n) and (c_count > n // 2) and (r_count == c_count))
            ):
                print("*", end=" ")
            else:
                print(" ", end=" ")
        print()


overall_size = int(input("Enter a overall_size:\t"))

if overall_size < 8:
    print("Enter a overall_size minimumin of 8")
else:
    print_pattern(overall_size)

Explanation:

Here in this pattern in the list of Python Pattern Programs, we have made a letter “Q”. The first statement inside the if conditional is for the printing of the first row. And the remaining three on the list are responsible for the printing of the other 3 rows. The last line in the if conditional is for the making of the tail of the letter “Q”/ For the placement of the tail, we use the modulo.

Output:

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

42. Printing a circle shape

row =6
col=4

for i in range(0,row):
    for j in range(0,col):
        if((j == 0 or j == col-1) and (i!=0 and i!=row-1)) :
            print('*',end='')  
        elif( ((i==0 or i==row-1) and (j>0 and j<col-1))):
            print('*',end='')
        else:
            print(end=' ') 

    print()

Output:

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

43. Printing letter “W”

h="";    
for Row in range(0,7):    
    for c_count in range(0,7):     
        if (((c_count == 1 or c_count == 5) and Row < 6) or ((Row == 5 or Row == 4) and c_count == 3) or (Row == 6 and (c_count == 2 or c_count == 4))):  
            h=h+"*"    
        else:      
            h=h+"  "    
    h=h+"\n"    
print(h)

Output:

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

44. Printing heart shape using an asterisk

for row in range(6):  
    for col in range(7):  
        if (row==0 and col %3 !=0)or(row==1 and col %3==0) or(row-col==2) or(row+col==8):  
            print("*",end=" ")  
        else:  
            print(end=" ")  
    print()
 * *  * *  
*   *   *
*      *
 *    *
  *  *
   *

45. Printing hallow diamond pattern using an asterisk

rows = int(input("Enter number of rows you want : "))

print("Hollow Diamond Pattern using asterisk")

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


for i in range(rows - 1, 0, -1):
    for j in range(1, rows - i + 1):
       
        print(' ', end = '')
    for k in range(1, 2 * i):
        if k == 1 or k == i * 2 - 1:
            
            print('*', end = '')
        else:
           
            print(' ', end = '')
    print()

Explanation:

In the above diamond pattern, we made a diamond filled with alphabets or numbers but here in this pattern there is a catch and that is to make it hollow. The concept that we are going to use to make a triangle and then code for an inverted triangle and finally merge them.

Our first for loop will print a normal regular triangle. Now that we want to increase the space between two asterisks, we have done the coding in that way, which will basically increase the space every time a new row is made. This same goes for an inverted triangle.

Output:

Enter number of rows you want : 10
Hollow Diamond Pattern using asterisk
         *
        * *
       *   *
      *     *
     *       *
    *         *
   *           *
  *             *
 *               *
*                 *
 *               *
  *             *
   *           *
    *         *
     *       *
      *     *
       *   *
        * *
         *

Summary

Here is the end of the article on Python Pattern Programs. We mainly used a FOR loop in order to draw the above-listed patterns. This basically made our task quite easy and achievable. We hope you liked this article and we believe that it might turn out to be a great source of learning for our readers. We thank you for reading our article and visiting our website.

Share:

Author: Ayush Purawr