Table of contents
1.
Introduction
2.
Pyramid Patterns
2.1.
Half Pyramid Of Stars
2.2.
Inverted Pyramid Involving Numbers
2.3.
Full Pyramid
3.
Star Patterns
3.1.
Inverted Right-angled Triangle
3.2.
Diamond Pattern
3.3.
Rectangle Star Pattern
4.
Patterns Involving Number
4.1.
Even Number Pyramid Pattern
4.2.
Pyramid Of Horizontal Table
4.3.
Pyramid Pattern Of Alternate Numbers
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

Pattern in Python

Author ANKIT KUMAR
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Pattern problems are one of the most asked questions in technical interviews or college exams. Patterns are basically repeated series or sequences. There are various types of patterns like stars, pyramids, diamonds, rectangles, etc. In this article, we will try to cover most of them. Most importantly, we shall try to get the intuition behind solving any pattern problem.

Recommended topics, Floor Division in Python, and Convert String to List Python

Pyramid Patterns

There can be a variety of pyramid patterns. However, there are certain common things that we can keep in mind while solving pyramid pattern problems.

  1. We can use an outer loop for the number of rows.
  2. We can use the inner loop for the number of columns.
  3. Carefully observe the spacings, whether there is an odd number of gaps or even number of gaps.

Half Pyramid Of Stars

Pattern: 

*

**

***

****

*****

Code:

for i in range(1,6):
    for j in range(1,i+1):
        print("*",end="")     // to print in a single line
    print()                        // to change the line
You can also try this code with Online Python Compiler
Run Code

Explanation:

  • Firstly we should try to see if there is any kind of pattern or sequence in the given star pattern.
  • We see that there are five rows, and for ith row, there are i number of * in that row.
  • We, therefore, need an outer loop to iterate for five times, as we have five rows.
  • We have an inner loop that prints the *, i number of times. So for every ith iteration of the outer loop, we run an inner loop i number of times.

Read More, leap year program in python

Inverted Pyramid Involving Numbers

Pattern:

55555

4444

333

22

1

Code:
for i in range(5,0,-1):
    for j in range(0,i):
        print(i,end="")
    print()
You can also try this code with Online Python Compiler
Run Code

Explanation:

  • The outer loop runs five times, i.e., for i= 5, 4, 3,2,1.
  • For every i starting from 5 and going down to 1, we need an inner loop that runs exactly i number of times. Example: for i=5, the inner loop will have j values as 0,1,2,3,4. Hence it will iterate five times, which is the value of i.
  • In every iteration, we print the value of i.

Full Pyramid

Pattern:

        *

      * * *

    * * * * *

  * * * * * * *

* * * * * * * * *

Code:

rows = int(input("Enter number of rows: "))
k = 0
for i in range(1, rows+1):
    for space in range(1, (rows-i)+1):
        print(end="  ")
   
    while k!=(2*i-1):
        print("* ", end="")
        k += 1
   
    k = 0
    print()
You can also try this code with Online Python Compiler
Run Code

Also see, Fibonacci Series in Python

Star Patterns

Inverted Right-angled Triangle

Pattern:

    *

   **

  ***

 ****

*****

Code:

for i in range(0,5):
    for j in range(5,0,-1):
        if j>i+1:
            print(" ",end="")
        else:
            print("*",end="")
    print()
You can also try this code with Online Python Compiler
Run Code

Diamond Pattern

Pattern:

          * 

         * * 

        * * * 

       * * * * 

      * * * * * 

     * * * * * * 

    * * * * * * * 

     * * * * * * 

      * * * * * 

       * * * * 

        * * * 

         * * 

          * 

Code:

rows = int(input("Enter the number of rows: "))  
k = 2 * rows - 2  
 
for i in range(0, rows):  
  
    for j in range(0, k):  
        print(end=" ")  
    
    k = k - 1  
    
    for j in range(0, i + 1):  
        print("* ", end="")  
    print("")  
  
# Downward triangle Pyramid  


k = rows - 2  
 
for i in range(rows, -1, -1):  
     
    for j in range(k, 0, -1):  
        print(end=" ")  
    
    k = k + 1  
  
    for j in range(0, i + 1):  
        print("* ", end="")  
    print("")  
You can also try this code with Online Python Compiler
Run Code

Rectangle Star Pattern

Pattern:

*  *  *  *  *  

*  *  *  *  *  

*  *  *  *  *  

*  *  *  *  *  

*  *  *  *  * 

Code:

rows = int(input("Please Enter the Total Number of Rows  : "))
columns = int(input("Please Enter the Total Number of Columns  : "))
for i in range(rows):
    for j in range(columns):
        print('*', end = '  ')
    print()
You can also try this code with Online Python Compiler
Run Code

Click here to know about Python here. 

Patterns Involving Number

Even Number Pyramid Pattern

Pattern:

10  

10  8  

10  8  6  

10  8  6  4  

10  8  6  4  2 

Code:

for i in range (0,5):
    even=10
    for j in range(0,i+1):
        print(even," ",end="")
        even= even-2
    print()
You can also try this code with Online Python Compiler
Run Code

Pyramid Of Horizontal Table

Pattern:

0  

0 1  

0 2 4  

0 3 6 9  

0 4 8 12 16  

0 5 10 15 20 25  

0 6 12 18 24 30 36

Code:

rows = 7
for i in range(0, rows):
    for j in range(0, i + 1):
        print(i * j, end=’ ‘)
    print()
You can also try this code with Online Python Compiler
Run Code

Pyramid Pattern Of Alternate Numbers

Pattern:

3 3 

5 5 5 

7 7 7 7 

9 9 9 9 9

Code:

rows = 5
i = 1
while i <= rows:
    j = 1
    while j <= i:
        print(((i * 2) - 1), end="")
        j = j + 1
    i = i + 1
   print()
You can also try this code with Online Python Compiler
Run Code

 

You can try it on online python compiler.

FAQs

  1. What are the various types of patterns?
    The various types of patterns are pyramids, stars, patterns with numbers, etc.
     
  2. Why are pattern problems important?
    Pattern problems are a quick way to judge one's problem-solving skills along with the basic implementation skills.
     
  3. Where are the pattern problems asked?
    Pattern problems are asked in technical interview rounds and very often in college exams.
     
  4. Why do we need nested loops in most pattern problems?
    The outer loop represents the number of rows and hence is used to iterate for each row, and the inner loop is used to represent the column. 
     
  5. What should one do to ensure that the characters of the pattern are displayed in a single line?
    We should use end="" in the print statement to avoid a new line.

Key Takeaways

  • Pattern problems are one of the most asked questions in technical interviews or college exams.
  • We can use an outer loop for the number of rows. We can use the inner loop for the number of columns.
  • Pattern problems are a quick way to judge one's problem-solving skills along with the basic implementation skills.
  • We should use end=”” in the print statement to avoid a new line.

Never stop learning. Explore more here.

Happy learning!

Live masterclass