Python Programming (138 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (32 Blogs)
  • Data Science (26 Blogs)
  • Mastering Python (92 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

Learn How To Make Python Pattern Programs With Examples

Last updated on Mar 29,2022 129.5K Views

45 / 62 Blog from Python Fundamentals

Python programming language is quite easy to learn. The implementation of various libraries with the ease of syntax makes it stand out, one of the many reasons why it has become the most popular programming language in this decade. While the learning part is easy, the interviewers often seek your approach in building the logic for pattern programs. As tricky as it may sound, with python it is a piece of cake. In this article, we will learn about various pattern programs in python. The following topics are covered in this blog:

The pattern programs incorporate a lot of nested loops. So if you are not familiar with loops in python, make sure to check out the detailed tutorial on loops in python.

Star Pattern Programs

Following are a few star pattern programs in python.

Pyramid Pattern Program

def pattern(n):
      k = 2 * n - 2
      for i in range(0,n):
           for j in range(0,k):
               print(end=" ")
           k = k - 1
           for j in range(0, i+1):
                print("*", end=" ")
           print("&#92r")

pattern(5)

Output: pyramid - python pattern programs - edureka

 

Reverse Pyramid Pattern Program

def pattern(n):
      k = 2*n -2
      for i in range(n,-1,-1):
           for j in range(k,0,-1):
                print(end=" ")
           k = k +1
           for j in range(0, i+1):
                print("*", end=" ")
           print("&#92r")

pattern(5)

Output:reverse pyramid - python pattern programs - edureka

 

Right Start Pattern Program

def pattern(n):
      for i in range(0, n):
           for j in range(0, i + 1):
                print("* ", end="")
           print("&#92r")
      for i in range(n, 0 , -1):
          for j in range(0, i + 1):
               print("* ", end="")
          print("&#92r")

pattern(5)

Output:start pattern - python pattern programs - edureka

 

Left Start Pattern Program

def pattern(n):
    k = 2 * n - 2
    for i in range(0, n-1):
        for j in range(0, k):
            print(end=" ")
        k = k - 2
        for j in range(0, i + 1):
            print("* ", end="")
        print("&#92r")
    k = -1
    for i in range(n-1,-1,-1):
        for j in range(k,-1,-1):
            print(end=" ")
        k = k + 2
        for j in range(0, i + 1):
            print("* ", end="")
        print("&#92r")


pattern(5)

Output: left start pattern - python pattern programs - edureka

 

Hourglass Pattern Program

def pattern(n):
     k = n - 2 
     for i in range(n, -1 , -1):
          for j in range(k , 0 , -1):
               print(end=" ")
          k = k + 1     
          for j in range(0, i+1):
               print("* " , end="")
          print("&#92r")
      k = 2 * n  - 2
      for i in range(0 , n+1):
           for j in range(0 , k):
               print(end="")
           k = k - 1
            for j in range(0, i + 1):
                 print("* ", end="")
            print("&#92r")

pattern(5)

Output: hourglass - python pattern programs - edureka

 

Half-Pyramid Pattern Program

def pattern(n):
     for i in range(0,n):
         for j in range(0, i+1):
              print("* " , end="")
         print("&#92r")

pattern(5)

Output: half pyramid - python pattern programs - edureka

 

Left Half-Pyramid Pattern Program

def pattern(n):
     k = 2 * n - 2
     for i in range(0, n):
          for j in range(0, k):
               print(end=" ")
          k = k - 2 
          for j in range(0, i + 1):
              print("* ", end="")
          print("&#92r")
 
pattern(5)

Output: left half pyramid - python pattern programs - edureka

 

Downward Half-Pyramid Pattern Program

def pattern(n):
      for i in range(n, -1, -1):
           for j in range(0, i + 1):
               print("* ", end="")
           print("&#92r")

pattern(5)

Output:

 

Diamond Shaped Pattern Program

def pattern(n):
     k = 2 * n - 2
     for i in range(0, n):
          for j in range(0 , k):
               print(end=" ")
          k = k - 1
          for j in range(0 , i + 1 ):
               print("* ", end="")
          print("&#92r")
     k = n - 2
     for i in range(n , -1, -1):
          for j in range(k , 0 , -1): 
               print(end=" ")
           k = k + 1
           for j in range(0 , i + 1):
                print("* ", end="")
           print("&#92r")

pattern(5)

Output:

 

Diamond Star Pattern Program

for i in range(5):
    for j in range(5):
        if i + j == 2 or i - j == 2 or i + j == 6 or j - i == 2:
            print("*", end="")
        else:
            print(end=" ")
    print()

Output:

 

Number Pattern Programs

Here are a few programs with numeric patterns in java.

Simple Numbers Program

def pattern(n):
    x = 0 
    for i in range(0 , n):
        x += 1  
        for j in range(0, i + 1):
            print(x , end=" ") 
        print("\r") 
pattern(5)

Output:number patter - python pattern programs - edureka

 

Pascal’s Triangle Program

def pascal(n):
    for i in range(0, n):
        for j in range(0, i + 1):
            print(function(i, j)," ", end="")
        print()

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

    return res

pascal(7)

Output:pascal's traingle - python pattern programs - edureka

 

Half-Pyramid Pattern With Numbers

def pattern(n):
     for i in range(1, n):
         for j in range(1, i + 1):
             print(j, end= " ")
         print("&#92r")
pattern(5)

Output:

 

Diamond Pattern With Numbers

def pattern(n):
    k = 2 * n - 2
    x = 0
    for i in range(0, n):
        x += 1
        for j in range(0, k):
            print(end=" ")
        k = k - 1
        for j in range(0, i + 1):
            print(x, end=" ")
        print("&#92r")
    k = n - 2
    x = n + 2
    for i in range(n, -1, -1):
        x -= 1
        for j in range(k, 0, -1):
            print(end=" ")
        k = k + 1
        for j in range(0, i + 1):
            print(x, end=" ")
        print("&#92r")

pattern(5)

Output:

 

Descending Order Pattern Program

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

        print("&#92r")

pattern(5)

Output:

 

Binary Numbers Pattern Program

def pattern(n):
    k = 2 * n - 2
    for i in range(0, n):
        for j in range(0, k):
            print(end=" ")
        k = k - 1
        for j in range(0, i + 1):
            print('10', end="")

        print("&#92r")

pattern(5)

Output:

 

Characters Pattern Programs

Here are a few pattern programs in python with characters.

Right Alphabetical Triangle

def pattern(n):
    x = 65
    for i in range(0, n):
        ch = chr(x)
        x += 1
        for j in range(0, i + 1):
            print(ch, end=" ")
        print("&#92r")

pattern(5)

Output:character pattern- python pattern programs-edureka

 

Character Pattern Program

def pattern(n):
    k = 2 * n - 2
    x = 65
    for i in range(0, n):
        for j in range(0, k):
            print(end=" ")
        k = k - 1
        for j in range(0, i + 1):
            ch = chr(x)
            print(ch, end=" ")
            x += 1
        print("&#92r")


pattern(7)

Output:

 

K Shape Character Program

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

Output:

 

Triangle Character Pattern Program

def pattern(n):
    k = 2 * n - 2
    x = 65
    for i in range(0, n):
        ch = chr(x)
        x += 1
        for j in range(0, k):
            print(end=" ")
        k = k - 1
        for j in range(0, i + 1):
            print(ch, end=" ")
        print("&#92r")


pattern(5)

Output:

 

Diamond Shaped Character Pattern Program

def pattern(n):
    k = 2 * n - 2
    for i in range(0, n):
        for j in range(0, k):
            print(end=" ")
        k = k - 1
        x = 65
        for j in range(0, i + 1):
            ch = chr(x)
            print(ch, end=" ")
            x += 1
        print("&#92r")
    k = n - 2
    x = 65
    for i in range(n, -1, -1):
        for j in range(k, 0, -1):
            print(end=" ")
        k = k + 1
        for j in range(0, i + 1):
            ch = chr(x)
            print(ch, end=" ")
            x += 1
        print("&#92r")


pattern(5)

Output:

 

This brings us to the end of this article where we have learned how we can implement different patterns in python using asterisk, numbers and characters with the help of loops in python. I hope you are clear with all that has been shared with you in this tutorial.

Find out our Python Training in Top Cities/Countries

IndiaUSAOther Cities/Countries
BangaloreNew YorkUK
HyderabadChicagoLondon
DelhiAtlantaCanada
ChennaiHoustonToronto
MumbaiLos AngelesAustralia
PuneBostonUAE
KolkataMiamiDubai
AhmedabadSan FranciscoPhilippines

If you found this article on “Python Pattern Programs” relevant, check out the Edureka’s Python Certification Training, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

We are here to help you with every step on your journey and come up with a curriculum that is designed for students and professionals who want to be a Python developer. The course is designed to give you a head start into Python programming and train you for both core and advanced Python concepts along with various Python frameworks like Django.

If you come across any questions, feel free to ask all your questions in the comments section of “Python Pattern Programs” and our team will be glad to answer or join our Master Python programming course.

If you’re trying to extend your business in this exciting field, check out our Artificial Intelligence Course. It is offered in collaboration with E&ICT Academy, National Institute of Technology, Warangal. This executive Masters’ Program equips students with information about the tools, techniques, and tools they require to advance their careers.

Upcoming Batches For Python Certification Training Course
Course NameDate
Python Certification Training Course

Class Starts on 28th January,2023

28th January

SAT&SUN (Weekend Batch)
View Details
Python Certification Training Course

Class Starts on 25th February,2023

25th February

SAT&SUN (Weekend Batch)
View Details
Python Certification Training Course

Class Starts on 25th March,2023

25th March

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Learn How To Make Python Pattern Programs With Examples

edureka.co