Pattern Programs in Python

Pattern Programs in Python

Python pattern programs are a great way to improve your knowledge of conditionals, loops, and logic construction. You can improve your problem-solving skills and get ready for technical interviews by making different shapes, such as triangles, pyramids, diamonds, and more. This article will cover a variety of Python patterns, ranging from Pascal’s triangle to a simple star pattern.

Table of Contents:

What are Pattern Programs?

Pattern programs are programs that are used to print different shapes or some specifically designed shapes in characters such as asterisks, numbers, or alphabets. Pattern programs are written using nested loops using for or while loops.

Pattern programs help to understand nested loops, conditional statements, string formatting, and functions like print(), range(), chr(), and ord().

Pattern Programs in Python 

Below are a few pattern programs in Python.

1. Right-Angled Triangle Pattern in Python

The right-angled triangle pattern is the simplest pattern and one of the widely used patterns in programming. It consists of one asterisk(*) in the first row, and each row adds an additional asterisk(*) to the next row, forming a right-angled triangle.

Logic:

  • The number of rows is used to define the length of the triangle, which means how tall the triangle will be.
  • Each row must contain a certain number of stars.
  • The number of stars in a row is equal to the row number. For example, Row 1 -> 1 star, Row 2-> 2 star, and so on.
  • Use nested for loops, where the outer loop controls rows, and the inner loops control columns. 

Code: 

Python

Output:

1. Right-Angled Triangle Pattern

The program above uses a nested loop to print a right-angled triangle. The outer loop loops from 1 to rows, while for each iteration of the outer loop, the inner loop will run i times, to print * horizontally in a new line for each row.

2. Left-Aligned Triangle Pattern in Python

A left-aligned triangle pattern is the pattern in which the triangle is shifted to the left by adding spaces before the stars (*) on each line. This creates a triangle that looks left-slanted.

Logic: 

  • Each row should contain a certain number of stars (*).
  • The number of stars in a row is equal to the row number.
  • Before printing the stars, add spaces to shift the stars to the right.
  • The number of spaces a row is equal to (total rows – current row number).

Code:

Python

Output:

2. Left Aligned Triangle Pattern

The above program prints a left-aligned triangle by adding spaces (rows – i) before incrementing stars in each row.

3. Pyramid Pattern in Python

A pyramid pattern is a symmetrical triangle with a centered triangular shape on each line. The width increases as it goes downward, forming a perfect pyramid shape.

Logic:

  • The number of stars to print in each row will increase with the current row number.
  • Before printing stars, add spaces for the center-aligned pyramid.
  • The number of leading spaces is equal to (total rows – current row number).

Code:

Python

Output:

3. Pyramid Pattern

The program above prints a pyramid pattern. The first inner loop will print spaces to center the stars, and the second inner loop will print (2*i-1) stars, one time for each row, to give it the width. Then, the print() method moves down to the next line.

4. Inverted Right-Angled Triangle in Python

An inverted right-angled triangle is the reverse of a right-angled triangle. It starts with having a maximum number of stars in its first row and then decreases by 1 in each row.

Logic:

  • The outer loop runs in reverse, from the total number of rows to 1.
  • Print the number of stars equal to the row number, for each row.
  • Each row will have one less star than the previous row.

Code:

Python

Output:

4. Inverted Right-Angled Triangle

The above code gives an inverted right triangle, where the number of stars in each row is decreasing as it goes from “rows” to 1, with a reverse outer loop.

Learn Python. Get Certified. Get Ahead
Enroll now!
quiz-icon

5. Inverted Pyramid Pattern in Python

An inverted pyramid pattern is simply a pyramid upside-down. The row with the maximum stars is row 1, and each subsequent row decreases the number of stars while increasing the leading spaces.

Logic: 

  • The outer for loop is counted down starting with the total amount of rows to 1.
  • The first inner for loop prints increasing spaces and shifts the stars to the right.
  • The second inner for loop prints the stars equally and consistently in an odd amount, decreased by each row (2*i-1).

Code:

Python

Output:

5. Inverted Pyramid Pattern

The above program prints a pattern of an inverted pyramid. It does this by simply using a reverse loop to decrease the stars (2 * i – 1) and increase space (rows – i) in each row, while also being centered.

6. Diamond Pattern in Python

A Diamond pattern is a combination of a pyramid and an inverted pyramid. First, build a pyramid (the top half), and then an inverted pyramid after it (the bottom half), to create a diamond shape. 

Logic:

1. The upper half (regular pyramid):

  • The stars increase from 1 to rows.
  • Spaces before the stars to center the pattern.

2. The lower half (inverted pyramid):

  • The stars decrease from (rows – 1) to 1.
  • Also center-aligned with spaces before the stars. 

Code:

Python

Output:

6. Diamond Pattern

The above program prints a diamond pattern by printing a regular pyramid followed by an inverted pyramid, using spaces to center-align stars in both halves.

7. Hollow Square in Python

A hollow square pattern is a square made of stars (*) where only the border is filled and the inside is empty. 

Logic:

  • Use two loops, an outer loop for rows and an inner loop for columns.
  • Print a star (*) only if you are in the first or last row and column.
  • Print a space for all the inner positions.

Code:

Python

Output:

7. Hollow Square

The above program uses conditional checks inside nested loops to place stars on the border positions and spaces inside to print a hollow square.

8. Repeating Number Rows Pattern in Python

The repeating number rows pattern has the same number, which is equal to the row number, repeated in each row.

Logic:

  • Each row number is printed i times.
  • Nested loops are used, in which the outer loop controls the row number, and the inner loop repeats the row number.

Code:

Python

Output:

8. Repeating Number Rows Pattern

The above program prints a pattern that for each row prints number the same number of times as its row number.

9. Floyd’s Triangle in Python

Floyd’s Triangle is a numerical pattern formed by printing numbers in a way that forms a right-angle triangle. The i-th row has one more number than the previous row (i-1).

Logic:

  • The first number is 1, having increased num by 1 every time. 
  • There are i number of values printed on each row i.

Code:

Python

Output:

9. Floyd’s Triangle

The above program prints Floyd’s Triangle by using two nested loops that use a counter variable and increase that variable sequentially for each row.

10. Pascal’s Triangle in Python

A triangular array of binomial coefficients is known as Pascal’s Triangle. Every number in the preceding row is the sum of the two numbers that are immediately above it.

Logic:

  • Apply the formula for binomial coefficients: C(n, k) = n!/k!(n-k)!
  • There are i + 1 elements in every row.
  • The triangle is center-aligned using spaces.

Code:

Python

Output:

10. Pascal’s Triangle

This code prints Pascal’s Triangle by calculating binomial coefficients using factorials and formatting the output into a centered triangle.

Get 100% Hike!

Master Most in Demand Skills Now!

11. Mirror Number Pattern in Python

The Mirror Number Pattern prints numbers in increasing order per row, but aligned to the right using spaces, like a right-angled triangle reflected across a vertical axis.

Logic:

  • Leading spaces decrease with each row to shift numbers right.
  • Numbers start from 1 and increase up to the current row index.

Code:

Python

Output:

11. Mirror Number Pattern

This code prints a right-aligned number triangle by combining decreasing spaces and increasing numbers per row.

12.  Number Pyramid Pattern in Python

The number pyramid pattern consists of a pyramid-shaped arrangement of numbers with spaces in the middle of each row displaying an increasing sequence of numbers.

Logic: 

  • Each row has an increasing number of elements, (2 * i – 1).
  • Also, (rows-i) will be the leading spaces.
  • Each row starts from 1 and increases by 1.

Code:

Python

Output:

12. Number Pyramid Pattern

This code prints a center-aligned number pyramid by printing each row with an increasing count of numbers, specifically (2*i-1) numbers on the ith row. In each row, leading spaces are printed before the numbers to ensure the pyramid is centered and symmetrical.

13. Inverted Number Pyramid Pattern in Python

This is an inverted number pyramid pattern. It also begins with the largest row and ends with the smallest row. The rows will maintain symmetry and be centered.

Logic:

  • Each row will print (2*i-1) numbers starting from 1, where i is the row number.
  • The leading space will increase for each row so that the numbers will be centered.
  • The width of the pattern decreases as the row number decreases.

Code:

Python

Output:

13. Inverted Number Pyramid Pattern

The code prints an inverted number pyramid by starting with the maximum count of numbers in the first row and decreasing the count by one, row by row. While it decreases the count of numbers in each row, it also increases the count of leading spaces prior to the numbers in each row. This is done so that the pattern remains center aligned and the pyramid shape is properly maintained as the width decreases at each row moving downward.

14. Number Diamond Pattern in Python

The number diamond pattern is a symmetrical pattern of a diamond shape that is created by using increasing and decreasing sequences of numbers.

Logic:

  • The top half is a number pyramid with increasing rows numbered from 1 to rows.
  • The bottom half is an inverted number pyramid, decreasing down to 1.
  • Each row will print numbers from 1 to (2*i-1), as well as leading spaces for centering.

Code:

Python

Output:

14. Number Diamond Pattern

This code prints a diamond shape by centering the sequential numbers in each row, forming a pyramid, followed by a mirrored pyramid. In this program, spaces are used to center the sequential numbers to form the diamond shape.

15. Butterfly Pattern in Python

The butterfly pattern consists of two symmetrical right triangles, one in the top half and the other in the bottom half. In the center, there are some gaps between the two triangles to create the look of a butterfly. 

Logic:

  • The top half will expand the stars from left to right and shrink the gap center for a symmetrical look.
  • The bottom half will do the reverse of the top half in reverse order.
  • The middle spaces are 2 * (rows – i) for symmetry.

Code:

Python

Output:

15. Butterfly Pattern

This code prints a butterfly pattern by printing stars (*) on both the left side and right side of each line, but changing the number of spaces in the middle. The spaces in between the stars get smaller in the upper half of the pattern and larger in the lower half of the pattern, so you have two symmetrical wings that come together to make a butterfly shape.

Enroll for Free and earn your Python certification from anywhere, anytime!
Enroll now!
quiz-icon

16. Hollow Butterfly Pattern in Python

A Hollow Butterfly Pattern is a type of butterfly pattern that only prints the edges of the wings (stars), so the interior is hollow with spaces.

Logic:

  • This pattern can be broken up into two halves, top and bottom.
  • Each “wing” or side of the butterfly prints stars for the wing borders (j == 1 or j == i), and the other elements are hollow with spaces.
  • There is a center gap between the wings, which expands or shrinks as you iterate through the rows to complete the full shape of the butterfly.

Code:

Python

Output:

16. Hollow Butterfly Pattern

This code prints a symmetrical hollow butterfly structure by printing two triangular wings side by side. It draws stars (*) only along the outside edges of each wing, the left and right boundaries, while filling the inside spaces with blank spaces to keep the wings hollow. 

17. Zig-Zag Number Pattern in Python

The zigzag number pattern alternates between rows of numbers that are arranged from left to right and then right to left.

Logic: 

  • Determine which row the number will be in by iterating the columns from left to right and dividing them by using the modulus operator.
  • Put the numbers in a zigzag pattern that repeats from top to middle to bottom.
  • A two-dimensional list containing the pattern will be saved and printed row by row.

Code:

Python

Output:

17. Zig-Zag Number Pattern

This code uses the index number of each column to determine the row location, printing a zigzag pattern of numbers, and in order to align numbers diagonally for a zigzag appearance.

18. Alternating Binary Pattern in Python

The alternating binary pattern is a pattern that will print a square grid of alternating 1s and 0s, starting with 1, with the starting digit alternating with each row.

Logic:

  • To switch between 1s and 0s, use (i + j) % 2.
  • If the indices are even, row + column = 0, or odd row + column = 1.
  • With this approach, the alternation of each starting digit for a row is taken care of automatically.

Code:

Python

Output:

18. Alternating Binary Pattern

This code gives a checkerboard-style binary pattern by alternating between using 1 and 0 for each cell in the square grid. It then decides whether each cell in the grid will print a 1 or print a 0 by adding the row index and the column index together to get a value. If the value is even, it prints a 1, and if the value is odd, it prints a 0. 

19. Cross Pattern in Python

The cross pattern is a pattern that prints stars (*) or another symbol in a cross shape using two diagonals of a square grid. 

Logic: 

  • The first diagonal is where i == j. 
  • The second diagonal is where i + j == size – 1.
  • These two conditions cover each arm of the “X”.

Code:

Python

Output:

19. Cross Pattern in Python

This code gives a cross in the shape of an X by filling in positions with stars (*) where the row and column indices are equal (on the main diagonal) and positions where the row index and column index sum to n + 1 (on the anti-diagonal). The remaining positions are filled with spaces.

20. Plus Pattern in Python

The plus pattern is an even plane symmetrical “+” shape with stars (*) by creating stars on the middle row and column of a square matrix.

Logic:

  • The middle row (i == mid row) makes the horizontal line
  • The middle column (j == mid col) makes the vertical line
  • Odd value sizes provide the best symmetric view.

Code:

Python

Output:

20. Plus Pattern

This program generates a plus (+) pattern through a combination of stars (*) being in the center row and center column of a square grid. The program calculates the index that corresponds to the center of the grid based on the size of the grid, and it will only print a star when either the row or column being printed is set to the center of the grid. The rest of the row or column will be set to spaces, which forms a symmetrical plus (+) shape.

Conclusion

Pattern programs are a basic way to use and understand key concepts in programming, such as nested loops, conditional statements, and formatting output. By solving small and large problems and creating shapes including triangles, pyramids, diamonds, butterflies, and zig-zags, programmers increase their ability to think logically and gain coding proficiency. These patterns provide an excellent way to practice coding and could be more helpful as a beginner to develop a foundation for learning more advanced programming problems. Understanding these patterns of programming, therefore, improves fluency of coding, which in turn strengthens understanding of the inner workings of loops and conditionals to produce organized output.

Pattern Programs in Python – FAQs

Q1. In what way do nested loops assist in pattern printing?

Nested loops help you print rows and columns; the outer loop that iterates over the rows and the inner loop that iterates over the columns or the specific character(s) for each row.

Q2. What is the logic for creating a right-angled triangle pattern?

The number of stars in a row is equal to the row number, and nested for loops are used, where the outer loop controls rows, and the inner loop controls columns.

Q3. How do you make a pyramid pattern using Python's loops?

By using spaces and stars, you would print fewer spaces, decreasing from a certain number (e.g., 4 spaces) and increasing stars (e.g., 1 star) in each row until the pattern is centered.

Q4. What is the logic for creating a regular pyramid vs an inverted pyramid pattern?

A regular pyramid starts with fewer stars at the top and increases, whereas an inverted pyramid starts with the most stars at the top and decreases.

Q5. What are pattern programs in Python?

Pattern programs are scripts that output a shape or design of any kind (of stars, numbers, or letters) based on the prepared logic of the loops.

About the Author

Senior Consultant Analytics & Data Science, Eli Lilly and Company

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration. 

Full Stack Developer Course Banner