Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconData Science USbreadcumb forward arrow iconTop 10 Python Pattern Programs You Should Know

Top 10 Python Pattern Programs You Should Know

Last updated:
24th Oct, 2022
Views
Read Time
7 Mins
share image icon
In this article
Chevron in toc
View All
Top 10 Python Pattern Programs You Should Know

When you first start learning to code, there are some basic challenges that you must overcome to get accustomed to the basics of programming. One such challenge is understanding looping and iterations. This concept is taught very early on during your programming journey, but it stays with you till the very advanced stages and is helpful in all programming projects you undertake. 

The idea behind looping is to create easily understandable pieces of repeatable codes that work based on some predefined conditions. Loops ensure that you can run some functions several times, depending on your needs, without hard coding them multiple times. This idea is extremely useful in the programming world but can seem challenging to beginners. 

Don’t worry, we’ll help you with that. 

When it comes to iterations and loops, the best way to understand how they work is by going through various pattern printing programs and trying to dry-run how the code is producing the required article. 

Ads of upGrad blog

Get data science certification from the World’s top Universities. Learn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

So, in this article, we will walk you through ten important python design patterns you should know. You will have a comfortable grasp of the concept of loops in Python programming by the end of this article. 

However, before we dive into the python design patterns, let’s first look at the basic steps involved in pattern printing in Python. 

Basic steps for printing different patterns using Python

Here is a series of steps that you can take to print different patterns:

  • Determine the number of rows and columns needed. This will help you decide how to program the outer and inner loops. The outer loop takes care of the number of rows, whereas the inner loop is all about the current column.  
  • Write an outer loop to iterate in the range of the number of rows. You can do this using for or while loops. 
  • Write an inner loop for handling the various columns of your pattern. The iteration of this loop is directly linked to the outer loop. 
  • Use the print() function to display whatever you need to display according to the pattern. 
  • Don’t forget to add a new line once every iteration of the outer loop completes, ensuring the next printing will occur in the following line instead of the same line itself. 

Important pattern programs using Python

Pattern 1: 

Here is the pattern that we need to print: 

1  

2 2  

3 3 3  

4 4 4 4  

5 5 5 5 5

Here is the required Python program that will help you get the following pattern matching python on your screen: 

rows_number = int(input(‘How many rows do you want in the pattern?”))

for i in range(rows_number):    

    for j in range(i):

        print(i, end=’ ‘)

    print(”)

In the above program, we will display a single digit on the first row, two digits in the second row, and so on. For this reason, we ran the outer loop in the range of the number of rows, whereas each iteration of the outer loop fixed the column values for inner rows. 

Pattern 2: 

Here is the second pattern that we need to learn how to print: 

1 2 

1 2 3 

1 2 3 4 

1 2 3 4 5

As you can see, in this pattern, every second number is an increment by 1 of the last number in each row. 

Here’s the required Python program for this: 

rows_number = int(input(‘How many rows do you want in the pattern?”))

for i in range(1, rows_number + 1):

    for j in range(1, i + 1):

print(j, end=’ ‘)

    print(”)

Pattern 3: 

So far, we have printed upright pyramids. Now, we see how inverted pyramids are printed using Python. For that, we will begin with the following pattern printing: 

1 1 1 1 1 

2 2 2 2 

3 3 3 

4 4 

5

For this, we can use the following Python program:

number_rows = 5

b = 0

for i in range(number_rows, 0, -1):

    b += 1for j in range(1, i + 1):

        print(b, end=’ ‘)

    print(‘\r’)

Pattern 4: 

Now, we need to print the following pattern using Python programming language: 

5 5 5 5 5 

5 5 5 5 

5 5 5 

5 5 

5

Here’s how it can be done: 

number_rows = 5

n = number_rows

for i in range(number_rows, 0, -1):

    for j in range(0, i):

        print(n, end=’ ‘)

    print(“\r”)

Pattern 5: 

The next pattern that we want to print is: 

0 1 2 3 4 5 

0 1 2 3 4 

0 1 2 3 

0 1 2 

0 1

For that, we can use the following Python program: 

number_rows = 5

for i in range(number_rows, 0, -1):

    for j in range(0, i + 1):

        print(j, end=’ ‘)

    print(“\r”)

Pattern 6: 

This pattern focuses on printing odd numbers in a pyramid structure, as follows: 

3 3 

5 5 5 

7 7 7 7 

9 9 9 9 9

Here is how this pattern matching python can be achieved using a Python program: 

number_rows = 5

itr= 1

while itr <= number_rows:

    j = 1

    while j <= itr:

        print((itr * 2 – 1), end=” “)

        j = j + 1

    itr = itr + 1

    print(”)

Pattern 7:

In the next pattern, we want to look at printing an equilateral triangle consisting of stars or any other special symbols, as follows: 

            *   

           *  *   

          *  *  *   

         *  *  *  *   

        *  *  *  *  *   

       *  *  *  *  *  *   

      *  *  *  *  *  *  *  

This is how it can be achieved using a Python program: 

size = 7

m = (2 * size) – 2

for i in range(0, size):

    for j in range(0, m):

        print(end=” “)

    # decrementing m after each loop

    m = m – 1

    for j in range(0, i + 1):

        print(“* “, end=’ ‘)

    print(” “)

Pattern 8:

This is about Pascal’s pattern and how we can print it using Python: 

       * 

      * * 

    * * * 

  * * * * 

* * * * * 

  * * * * 

    * * * 

      * * 

        *

Here is a Python program that can get that done: 

number_rows = 5

itr = 1

while itr <= number_rows:

    j = itr

    while j < number_rows:

        # display space

        print(‘ ‘, end=’ ‘)

        j += 1

    k = 1

    while k <= itr:

        print(‘*’, end=’ ‘)

        k += 1

    print()

    itr += 1

itr = number_rows

while itr >= 1:

    j = i

    while j <= rows:

        print(‘ ‘, end=’ ‘)

        j += 1

    k = 1

    while k < itr:

        print(‘*’, end=’ ‘)

        k += 1

    print(”)

    itr -= 1

Pattern 9:

This pattern is also referred to as a sandglass pattern, and is essential to understand how nested loops work together! 

* * * * * 

 * * * * 

  * * * 

   * * 

    * 

    * 

   * * 

  * * * 

 * * * * 

* * * * *

Here is how we can print this pattern using the Python programming language: 

number_rows = 5

itr = 0

while itr <= number_rows – 1:

    j = 0

    while j < itr:

        # display space

        print(”, end=’ ‘)

        j += 1

    k = itr

    while k <= rows – 1:

        print(‘*’, end=’ ‘)

        k += 1

    print()

    itr += 1

i = rows – 1

while itr >= 0:

    j = 0

    while j < itr:

        print(”, end=’ ‘)

        j += 1

    k = itr

    while k <= number_rows – 1:

        print(‘*’, end=’ ‘)

        k += 1

    print(”)

    itr -= 1

Pattern 10:

This pattern is about printing horizontal tables of numbers. Here is how it looks: 

2 4 

3 6 9 

4 8 12 16 

5 10 15 20 25 

6 12 18 24 30 36 

7 14 21 28 35 42 49 

8 16 24 32 40 48 56 64 

9 18 27 36 45 54 63 72 81 

10 20 30 40 50 60 70 80 90 100

That can be done using the following Python program: 

number_rows = 10

for i in range(1, number_rows + 1):

    for j in range(1, i + 1):

        print(i * j, end=’ ‘)

    print()

In conclusion

Looping is a fundamental programming concept that every beginner should learn. 

Ads of upGrad blog

We hope this article, and the examples discussed, gives you a better understanding of how loops and nested loops work together to help with different pattern Python printing.

Remember, programming is all about practice. So, even if you don’t get it at once, please keep retrying and practicing. Eventually, it will start falling into place. At upGrad, we have mentored students worldwide from various educational backgrounds and helped them grow professionally. We understand the challenges students face and are always one step ahead when it comes to solving those challenges. 

Our Professional Certification in Data Science, offered along with the University of Maryland, is designed for graduates looking to grow in Data Science. Check out our course and get yourself enrolled soon! 

Profile

Pavan Vadapalli

Blog Author
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working on solving problems of scale and long term technology strategy.
Get Free Consultation

Select Coursecaret down icon
Selectcaret down icon
By clicking 'Submit' you Agree to  
UpGrad's Terms & Conditions

Our Best Data Science Courses

Frequently Asked Questions (FAQs)

1How are patterns printed in Python?

Printing patterns in Python is all about working with different loops.

2Can we print pattern Python using only one loop?

More often than not, you will require two or more loops to print complex patterns.

3Which is the best method to print patterns in Python?

Looping is the best solution for printing patterns. Depending on your expertise and comfort, you can go with either ‘for’ or ‘while’ loops.

Explore Free Courses

Suggested Blogs

Top 10 Real-Time SQL Project Ideas: For Beginners &#038; Advanced
13265
Thanks to the big data revolution, the modern business world collects and analyzes millions of bytes of data every day. However, regardless of the bus
Read More

by Pavan Vadapalli

28 Aug 2023

Python Free Online Course with Certification [US 2023]
5429
Data Science is now considered to be the future of technology. With its rapid emergence and innovation, the career prospects of this course are increa
Read More

by Pavan Vadapalli

14 Apr 2023

13 Exciting Data Science Project Ideas &#038; Topics for Beginners in US [2023]
5351
Data Science projects are great for practicing and inheriting new data analysis skills to stay ahead of the competition and gain valuable experience.
Read More

by Rohit Sharma

07 Apr 2023

4 Types of Data: Nominal, Ordinal, Discrete, Continuous
5498
Data refers to the collection of information that is gathered and translated for specific purposes. With over 2.5 quintillion data being produced ever
Read More

by Rohit Sharma

06 Apr 2023

Best Python Free Online Course with Certification You Should Check Out [2023]
5438
Data Science is now considered to be the future of technology. With its rapid emergence and innovation, the career prospects of this course are increa
Read More

by Rohit Sharma

05 Apr 2023

5 Types of Binary Tree in Data Structure Explained
5298
A binary tree is a non-linear tree data structure that contains each node with a maximum of 2 children. The binary name suggests the number 2, so any
Read More

by Rohit Sharma

03 Apr 2023

42 Exciting Python Project Ideas &#038; Topics for Beginners [2023]
5363
Python is an interpreted, high-level, object-oriented programming language and is prominently ranked as one of the top 5 most famous programming langu
Read More

by Rohit Sharma

02 Apr 2023

5 Reasons Why Python Continues To Be The Top Programming Language
5268
Introduction Python is an all-purpose high-end scripting language for programmers, which is easy to understand and replicate. It has a massive base o
Read More

by Rohit Sharma

01 Apr 2023

Why Should One Start Python Coding in Today’s World?
5158
Python is the world’s most popular programming language, used by both professional engineer developers and non-designers. It is a highly demanded lang
Read More

by Rohit Sharma

16 Feb 2023

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon