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. 

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.

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!

How 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.

What 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.

What 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.

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.

Prepare for a Career of the Future

0 replies on “Top Python Programs to Create Pattern”

×