Learn 20 Most Important Pattern Programs in Python

20 important pattern programs in python

Introduction

It's important to understand the logic of pattern programs after you grab the basics of computer programming. This helps to build a logical basis for solving a problem.

In this tutorial, you'll learn 20 important pattern programs in python that you must know if you're starting your coding journey with python.

If you have already learned the pattern program, look again, it can help you. Because I have tried to present all the examples simply so that everyone can understand.

🔹Read Also: Check if a string has all unique characters in python

Star Patterns

Example #1: Right Triangle star pattern

Enter the height of the pattern: 5
*
* *
* * *
* * * *
* * * * *

Code


"""Right Sided Star Triangle"""
n = int(input("Enter the height of the pattern: "))

# Outer loop: manage rows
for i in range(0, n):
# Inner loop: manage columns
for j in range(0, i+1):
print("* ", end='')
print("\r")

Example #2: Left Triangle Star Pattern

Enter the height of the pattern: 5
     *
    **
   ***
  ****
 *****

Code


'''Left sided star triangle'''
n = int(input("Enter the height of the pattern: "))

m = 1

# Outer loop: manage rows
for i in range(n, 0, -1):
# Inner loop 1: manage #spaces
for j in range(0, i):
print(" ",end='')
# Inner loop 2: manage columns
for k in range(0, m):
print("*", end='')

print("\r")
m += 1

# *
# **
# ***
# ****
# *****

Example #3: Full Pyramid Star Pattern

Enter the height of the pattern: 5
      *
     * *
    * * *
   * * * *
  * * * * *

Code


'''Full Triangle/Pyramid star pattern'''

n = int(input("Enter the height of the pattern: "))

m = 1

# Outer loop: manage rows
for i in range(n, 0, -1):
# Inner loop 1: manage #spaces
for j in range(0, i):
print(" ",end='')
# Inner loop 2: manage columns
for k in range(0, m):
print(" *", end='')

print("\r")
m += 1

Example #4: Reverse Half Pyramid Pattern (Right-Sided)

Enter the height of the pattern: 5
*****
****
***
**
*

Code


'''Reverse Pyramid Pattern - Right Sided'''
n = int(input("Enter the height of the pattern: "))

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

Example #5: Reverse Half Pyramid Pattern (Left-Sided)

Enter the height of the pattern: 5
 *****
  ****
   ***
    **
     *

Code


'''Reverse Pyramid Pattern - Left Sided'''
n = int(input("Enter the height of the pattern: "))

# Outer loop: manage rows
for i in range(n, 0, -1):
# Inner loop 1: manage #spaces
for j in range(0, n-i+1):
print(" ",end='')
# Inner loop 2: manage and print columns
for k in range(0, i):
print("*", end='')

print("\r")

# *****
# ****
# ***
# **
# *

Example #6: Reverse Full Pyramid Pattern

Enter the height of the pattern: 10
 * * * * * * * * * *
  * * * * * * * * *
   * * * * * * * *
    * * * * * * *
     * * * * * *
      * * * * *
       * * * *
        * * *
         * *
          * 

Code


'''Reverse Full Triangle - star pattern'''
n = int(input("Enter the height of the pattern: "))

# Outer loop: manage rows
for i in range(n, 0, -1):
# Inner loop 1: manage #spaces
for j in range(0, n-i+1):
print(" ",end='')
# Inner loop 2: print columns
for k in range(0, i):
print("* ", end='')

print("\r")

Example #7: Diamond Pattern


Enter the height of the pattern: 10
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*

Code


'''Diamond pattern in python'''
n = int(input("Enter the height of the pattern: "))

# Loop 1: For the first half of the diamond pattern
for i in range(n):
print(" " * (n - i), "*" * (2*i + 1))
# Loop 2: For the second half of the diamond
for j in range(n - 2, -1, -1):
print(" " * (n - j), "*" * (2*j + 1))

Example #8: Half Diamond Pattern

Enter the height of the pattern: 5
*
**
***
****
*****
*****
****
***
**
*

Code


'''Half Diamond Star Pattern'''
n = int(input("Enter the height of the pattern: "))

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

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

Number Pattern Programs

Example #9: Simple Number Pattern - Right Triangle

Enter the height of the pattern: 5
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Code


'''Simple number triangle'''
n = int(input("Enter the height of the pattern: "))

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

Example #10: Half Pyramid Number Pattern - Increment Order

Enter the height of the pattern: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5 

Code


'''Half Pyramid Number Pattern'''
n = int(input("Enter the height of the pattern: "))

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

Example #11: Reverse Half Pyramid Number Pattern - Increment Order

Enter the height of the pattern: 10
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3
4 4 4 4 4 4 4
5 5 5 5 5 5
6 6 6 6 6
7 7 7 7
8 8 8
9 9
10

Code


'''Half Pyramid - Reverse'''
n = int(input("Enter the height of the pattern: "))

m = 0

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

Example #12: Reverse Half Pyramid Number Pattern - Decrement Order

Enter the height of the pattern: 5
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1

Code


'''Half Pyramid - Reverse - Decreasing by row'''
n = int(input("Enter the height of the pattern: "))

for i in range(n, 0, -1):
m = i
for j in range(0, i):
print(m, end=" ")
print("\r")

Example #13: Increasing Number Pattern

Enter the height of the pattern: 4
1
2 3
4 5 6
7 8 9 10

Code


'''Half Pyramid - Increasing by row and column'''
n = int(input("Enter the height of the pattern: "))

curr_num = 1
# Outer loop: Manage Rows
for i in range(1, n+1):
# Inner loop: Manage Columns
for j in range(0, i):
print(curr_num, end=" ")
curr_num += 1
print("\r")

Example #14: Odd Number Pattern

Enter the height of the pattern: 5
1
3 5
7 9 11
13 15 17 19
21 23 25 27 29

Code


'''Odd Number Pattern'''
n = int(input("Enter the height of the pattern: "))

curr_num = 1
# Outer loop: Manage Rows
for i in range(1, n+1):
# Inner loop: Manage Columns
for j in range(0, i):
print(curr_num, end=" ")
curr_num += 2
print("\r")

Example #15: Reverse Half Pyramid with Reverse Number Pattern

Enter the height of the pattern: 8
8 7 6 5 4 3 2 1
7 6 5 4 3 2 1
6 5 4 3 2 1
5 4 3 2 1
4 3 2 1
3 2 1
2 1

Code


'''Reverse Number Pattern'''
n = int(input("Enter the height of the pattern: "))

for i in range(n, 0, -1):
curr_num = i
for j in range(0, i):
print(curr_num, end=" ")
curr_num -= 1
print("\r")

Example #16: Multiplication Number Pattern by Column

Enter the height of the pattern: 5
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
 

Code


'''Multiplication Number Pattern'''
n = int(input("Enter the height of the pattern: "))

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

Example #17: Palindrome Half Pyramid Pattern

Enter the height of the pattern: 5
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 

Code


'''Palindrome number pattern - Half Pyramid'''
n = int(input("Enter the height of the pattern: "))

# Outer loop: Manage rows
for i in range(1, n+1):
# Inner loop 1: for handling columns, left of the middle
for j in range(1, i+1):
print(j, end=" ")
# Inner loop 2: Manage columns, right of the middle
for k in range(i-1, 0, -1):
print(k, end=" ")

print("\r")

Example #18: Palindrome Full Pyramid Pattern

Enter the height of the pattern: 5
    1
   121
  12321
 1234321
123454321

Code


'''Palindrome Pyramid pattern in Python'''
n = int(input("Enter the height of the pattern: "))

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

'''Inner loop 2: for handling left side columns
of the middle number'''
for k in range(1, i+1):
print(k, end="")

'''Inner loop 3: for handling right side columns
of the middle number'''
for l in range(i-1, 0, -1):
print(l, end="")
print("\r")

# 1
# 121
# 12321
# 1234321
# 123454321

Alphabet Pattern Programs

Example #19: A Simple Alphabet Pattern

Enter the height of the pattern: 5
A
A A
A A A
A A A A
A A A A A 

It is as simple as printing star pattern but the main agenda is in the next example(Example #20).

Code


'''A simple alphabet pattern'''
n = int(input("Enter the height of the pattern: "))

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

Example #20:  Alphabets Pattern with Increment Order

Enter the height of the pattern: 5
Enter the ASCII value of the letter: 65
A
B C
D E F
G H I J
K L M N O 

In the previous example, we print a simple alphabet pattern. It is as easy just like a printing star pattern. But if you want to print the pattern with letters in the increment order then the previous method will not work.

In computer language, every single character has a unique number called ASCII number. For instance, the ASCII value of capital 'A' is 65, and capital 'Z' is 90. In the same way small case 'a' equal to 97 and 'z' = 122. I hope now you've understood how to find the ASCII value of an alphabet.

In the program, you need to give two inputs. First, the height of the pattern, second, the ASCII value of the alphabet from where you want to print in the increasing order.

Code


'''Alphabets Pattern - Increasing by rows and columns'''
n = int(input("Enter the height of the pattern: "))

# A = 65, Z = 90, a = 97, z = 122
Ascii = int(input("Enter the ASCII value of the letter: "))
if (65<= Ascii <= 90) or (97<= Ascii <= 122):
for i in range(1, n+1):
for j in range(0, i):
print(chr(Ascii), end=" ")
Ascii += 1
print("\r")
else:
print("Please enter a valid ASCII Number.")

Exercises - Try it Yourself

Problem #1:

Enter the height of the pattern: 5
        A
       B C
      D E F
     G H I J
    K L M N O 

Problem #2:

Enter the height of the pattern: 5
Enter the ASCII value of the letter: 65
A A A A A
B B B B
C C C
D D

Problem #3:

Enter the height of the pattern: 5
Enter the ASCII value of the letter: 70
F F F F F
E E E E
D D D
C C
B
 

Problem #4:

Enter the height of the pattern: 5
Enter the ASCII value of the letter: 97
a
c c
e e e
g g g g
i i i i i

🔹Read AlsoCheck Permutation of two strings in Python - most efficiently

Summary

In this tutorial, we have solved 20 important pattern programs using python. These examples are very important for building the logical concept of your programming knowledge.

If you are a newbie to programming then this tutorial will definitely help you; If you aren't, it may remind you of the start time of your programming journey.

I just added an exercise section above. Solve the problems provided there and share your code with me in the comments section. Let me know if there are any difficulties; I will guide you.

Thanks for reading!💙

PySeek

Subhankar Rakshit

Meet Subhankar Rakshit, a Computer Science postgraduate (M.Sc.) and the creator of PySeek. Subhankar is a programmer, specializes in Python language. With a several years of experience under his belt, he has developed a deep understanding of software development. He enjoys writing blogs on various topics related to Computer Science, Python Programming, and Software Development.

Post a Comment (0)
Previous Post Next Post