Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconData Science USbreadcumb forward arrow iconTop Python Programs to Create Pattern

Top Python Programs to Create Pattern

Last updated:
1st Dec, 2021
Views
Read Time
8 Mins
share image icon
In this article
Chevron in toc
View All
Top Python Programs to Create Pattern

Python is currently the top most popular programming language globally after having dethroned C on TIOBE Index. Given its easy readability and simple syntax, which is similar to English Language, Python is extensively used on different platforms like Windows, Mac, Linux, Raspberry Pi, etc. 

Part of the huge demand for Python is that it has an easy learning curve due to its close to human language nature. Also, writing Python programs takes a fraction of the time compared to other programming languages, thereby speeding up the development process. 

Using Python, developers can develop efficient applications quickly, whether it is a complex machine learning program or simple pattern-creating codes.

In this article, we will list out some top programs to create star patterns using Python. 

Ads of upGrad blog

Why should you learn how to create patterns? 

Python finds extensive use in task automation, data analysis, machine learning, and artificial intelligence, where a huge part of programming involves identifying and creating patterns to make accurate predictions. This allows developers to recognize relationships in data and build generalizations to solve complex mathematical problems. 

Luckily Python design patterns codes are very easy to create and implement and offer the ability to create and print a wide variety of patterns. 

Star Patterns are among the most common patterns in most programming languages. It consists of a series of stars, and for printing star patterns, you only need to have a basic understanding of Python programming concepts like the use of ‘for loop’, ‘if loop’, and input and print functions. 

A basic pattern matching Python program primarily consists of two ‘for’ loops, where the outer loop is for rows, and the inner loop is for columns in the pattern. We have the print function to print the output and the input function to receive the user input. 

Let’s now look at the various types of star pattern Python programs below:

Square Pattern

The square pattern is one of the easiest patterns to create in Python. The idea is to print stars or characters in the form of a square. Two nested loops are used to print a square pattern; the internal for loop prints stars (*) and creates a new line after every row, the outer loop creates the rows, and the inner loop creates columns.

Code:

for i in range(0, 5):

    for j in range(0, 5):

        print(“*”, end=””)

    print()

Output:

*****

*****

*****

*****

*****

Right Angle Triangle Pyramid Pattern

It is to print the required number or character (in this case, stars) in the shape of a right-angle triangle pyramid.

Code:

l = 5

for i in range(0, l):

    # nested loop for each column

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

        # print star

        print(“*”, end=’ ‘)

    # new line after each row

    print(“\r”)

Output:

* * 

* * * 

* * * * 

* * * * * 

Mirrored Right Triangle

It is a star pattern in an inverted right triangle pyramid, also known as a mirrored right triangle.

Code:

# number of rows

rows = 5

k = 2 * rows – 2

for i in range(0, rows):

    # process each column

    for j in range(0, k):

        # print space in pyramid

        print(end=” “)

    k = k – 2

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

        # display star

        print(“* “, end=””)

    print(“”)

Output:

        * 

      * * 

    * * * 

  * * * * 

* * * * * 

Downward half-Pyramid Star Pattern

We use the reverse nested for loop to print the downward pyramid pattern of stars.

Code:

rows = 5

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

    # nested reverse loop

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

        # display star

        print(“*”, end=’ ‘)

    print(” “)

Output:

* * * * *  

* * * *  

* * *  

* *  

*

Equilateral Triangle or Pyramid Star Pattern Python

This star pattern will be in the form of an equilateral triangle.

Code:

n = 5

for i in range(n):

    for j in range(n – i – 1):

        print(‘ ‘, end=”)

    for k in range(2 * i + 1):

        print(‘*’, end=”)

    print()

Output:

    *

   ***

  *****

 *******

*********

Reverse Pyramid Pattern

Next, we will print the pyramid pattern in reverse.

Code:

rows = 5

k = 2 * 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(“”)

Output:

        * * * * * * 

         * * * * * 

          * * * * 

           * * * 

            * * 

             * 

Diamond-Shaped Star Pattern

This star pattern is in the shape of a diamond. We use the nested loops again. The program is a combination of pyramid and reverse-pyramid patterns.

Code:

rows = 5

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(“”)

    

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(“”)

Output:

        * 

       * * 

      * * * 

     * * * * 

    * * * * * 

   * * * * * * 

    * * * * * 

     * * * * 

      * * * 

       * * 

        * 

Hollow Diamond Pattern

The hollow diamond star pattern is the same as the diamond star pattern, but it is hollow. This pattern Python program is a little complex as we need to deal with multiple points. The pattern is divided into two parts – the upper pyramid and the lower pyramid.

Code:

rows = 5

i = 1

while i <= rows:

    j = rows

    while j > i:

        # display space

        print(‘ ‘, end=’ ‘)

        j -= 1

    print(‘*’, end=’ ‘)

    k = 1

    while k < 2 * (i – 1):

        print(‘ ‘, end=’ ‘)

        k += 1

    if i == 1:

        print()

    else:

        print(‘*’)

    i += 1

i = rows – 1

while i >= 1:

    j = rows

    while j > i:

        print(‘ ‘, end=’ ‘)

        j -= 1

    print(‘*’, end=’ ‘)

    k = 1

    while k <= 2 * (i – 1):

        print(‘ ‘, end=’ ‘)

        k += 1

    if i == 1:

        print()

    else:

        print(‘*’)

    i -= 1

Output:

    *

   * *

  * *

 * *

* *

 * *

  * *

   * *

    *

Sand Glass Star Pattern

The hourglass or sandglass pattern is a pattern shaped like an hourglass. As you observe the pattern, you will see that the pattern is made up of a downward pyramid pattern and an upward triangle pattern.

Code:

n = 5

# downward pyramid

for i in range(n-1):

    for j in range(i):

        print(‘ ‘, end=”)

    for k in range(2*(n-i)-1):

        print(‘*’, end=”)

    print()

# upward pyramid

for i in range(n):

    for j in range(n-i-1):

        print(‘ ‘, end=”)

    for k in range(2*i+1):

        print(‘*’, end=”)

    print()

Output:

*********

 *******

  *****

   ***

    *

   ***

  *****

 *******

*********

Right Pascal Triangle Star Pattern

The Right Pascal Triangle Star pattern, consists of an upper triangle and a lower triangle.

Code:

rows = 5

for i in range(0, rows):

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

        print(“*”, end=’ ‘)

    print(“\r”)

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

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

        print(“*”, end=’ ‘)

    print(“\r”)

Output:

* * 

* * * 

* * * * 

* * * * * 

* * * * 

* * * 

* * 

Left Pascal Triangle Star Pattern

This pattern is a mirror image of the right pascal triangle pattern. Compared to the right pascal triangle pattern, the left pascal triangle is a little bit complicated as you need to deal with both spaces and stars.

Code:

n = 5

# upper triangle

for i in range(n):

    # print spaces

    for j in range(n – i – 1):

        print(‘ ‘, end=”)

    # print stars

    for k in range(i + 1):

        print(‘*’, end=”)

    print()

# lower triangle

for i in range(n – 1):

    # print spaces

    for j in range(i + 1):

        print(‘ ‘, end=”)

    # print stars

    for k in range(n – i – 1):

        print(‘*’, end=”)

    print()

Output:

        * 

      * * 

    * * * 

  * * * * 

* * * * * 

  * * * * 

    * * * 

      * * 

        * 

Those were some top Python programs to create star patterns. Many other star pattern programs are equally easy to create using Python, whether alphabets or numbers.

Since Python developers can write efficient programs using just a few lines, enhancing the language’s appeal and usage.

As per the research and analysis conducted in 2019 under the ‘Technical Skills With Huge Demand’ by Forbes, Python ranked seventh, having registered a 456% growth rate. In 2021, the monthly TIOBE Index of language popularity declared that Python had secured the top position, having left C and Java behind; the languages secured the second and third positions, respectively, in November 2021. 

Being skilled at Python in the current competitive market can give you the much-needed edge to progress in your career. If you’d like to master Python and foray into the field of Data Science, we recommend joining upGrad’s Advanced Certificate Program in Data Science offered by IIIT Bangalore. The 8-month program has industry experts from top companies like Flipkart, Walmart, and Gramener to help you build essential hard and soft skills to succeed as a data science professional.

Ads of upGrad blog

The program also includes a complimentary Python programming Bootcamp, 7+ case studies and projects, and access to Kaggle competitions to help students build hands-on experience in the field. 

An advanced certificate program can increase your chances of success in data science. So, go ahead and book your seat in the program today!

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

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 do pattern programs in Python work?

Every pattern program in Python employs at least two for loops, one denoting the number of rows, and the other, denoting columns. The number of loops that will be used depends on the complexity of the pattern program.

2What are pattern programs?

Pattern programs are codes written to print numbers, alphabets, or special characters in your desired form, be it as a triangle, pyramid, inverted triangle, diamond, etc.

3What are some commonly asked pattern programs at interviews?

Some of the most commonly asked pattern programs include pyramid, rhombus, pascal triangle, diamond, mirrored right-angled triangle, downward triangle — composed using stars, alphabets, or numbers.

Explore Free Courses

Suggested Blogs

Top 10 Real-Time SQL Project Ideas: For Beginners &#038; Advanced
14864
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 2024]
5519
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 [2024]
5474
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
6068
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 [2024]
5628
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
5385
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 [2024]
5857
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
5339
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?
5220
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