Pattern Programs in Python

Pattern-Programs-in-Python-Featured-image.jpg

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

Table of Contents:

What are Pattern Programs?

Pattern programs are programs that print shapes or designs made of characters such as asterisks, numbers, or alphabets. Pattern programs are written using nested 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 most widely used patterns in programming. It consists of one asterisk(*) in the first row, and each following 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 stars, 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, and 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 in 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 in which each row of stars is centered. The width increases by two stars each row going downward.

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, once per row, to give the pyramid its 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 the maximum number of stars in the first row and decreases by one star in each following 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 a pyramid printed 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 counts down from the total number 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 an odd number of stars, (2*i-1), which decreases with each row

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.
  • Add 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 or the first or last 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 prints the row number repeated across 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 each row’s number as many times as the 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-angled triangle. The i-th row contains i numbers, one more than the previous row (i-1).

Logic:

  • The first number is 1, and the counter num increases by 1 after each number is printed.
  • Row i prints i values.

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

Pascal’s Triangle is a triangular array of binomial coefficients. Every number is the sum of the two numbers immediately above it in the preceding row.

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, forming 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 is a pyramid-shaped arrangement of numbers in which each row is centered with leading spaces and shows an increasing sequence of numbers.

Logic: 

  • Row i has (2 * i – 1) elements.
  • Each row has (rows – i) 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

The inverted number pyramid pattern begins with the largest row and ends with the smallest row, with every row symmetrical and centered.

Logic:

  • Each row will print (2*i-1) numbers starting from 1, where i is the row number.
  • The leading spaces increase with 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. As the count of numbers falls in each row, the count of leading spaces rises, keeping the pattern center-aligned as the width decreases downward.

14. Number Diamond Pattern in Python

The number diamond pattern is a symmetrical diamond-shaped pattern created from increasing and then decreasing rows 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), preceded by 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 is made of two symmetrical halves, each with a left and right right-angled triangle of stars, one in the top half and a mirrored one in the bottom half. In the center, there is a gap 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 center gap for a symmetrical look.
  • The bottom half mirrors 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, forming two symmetrical wings that 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 prints only the edges of the wings (stars) and leaves the interior 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 by printing two triangular wings side by side. It draws stars (*) only along the left and right boundaries of each wing and fills the inside with spaces to keep the wings hollow. 

17. Zig-Zag Number Pattern in Python

The zig-zag number pattern in Python is a pattern in which numbers are arranged in a wave-like path that moves from the top row to the middle and bottom rows and repeats.

Logic: 

  • Iterate through the columns from left to right and use the modulus operator to determine which row each number belongs in.
  • 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 zig-zag pattern of numbers that align diagonally.

18. Alternating Binary Pattern in Python

The alternating binary pattern is a square grid of alternating 1s and 0s that starts with 1, with the first digit switching on each row.

Logic:

  • To switch between 1s and 0s, use (i + j) % 2.
  • If the sum of the row and column indices is even, the result is 0; if it is odd, the result is 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 prints stars (*) or another symbol along the two diagonals of a square grid to form an X shape.

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 a symmetrical “+” shape made of stars (*) placed on the middle row and middle column of a square matrix.

Logic:

  • The middle row (i == mid row) draws the horizontal line
  • The middle column (j == mid col) draws the vertical line
  • Odd sizes give the most symmetrical result.

Code:

Python

Output:

20. Plus Pattern

This program generates a plus (+) pattern by placing stars (*) in the center row and center column of a square grid. The program calculates the center index from the grid size and prints a star only when the current row or column is the center. All other positions are filled with spaces, forming 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 are an excellent way for beginners to practice coding and build a foundation for more advanced programming problems. Mastering them improves coding fluency and strengthens understanding of how loops and conditionals produce organized output.

Pattern Programs in Python – FAQs

Q1. How do nested loops help in printing patterns?

Nested loops print patterns row by row: the outer loop iterates over the rows, and the inner loop iterates over the columns or characters in 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 print decreasing spaces (for example, starting at 4) and increasing stars (starting at 1) in each row, which centers the pattern.

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

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

Q5. What are pattern programs in Python?

Pattern programs in Python are scripts that use loops and conditions to print shapes or designs made of stars, numbers, or letters.

About the Author

Technical Content Writer | Software Developer

Nirnayika Rai is a Software Engineer and Technical Content Writer with experience in full-stack development, cloud platforms, and software systems. She creates clear, well-structured content that helps developers understand and apply technical concepts with confidence. Her work combines coding knowledge with a strong focus on clarity, accuracy, and practical relevance.

EPGC Data Science Artificial Intelligence