PYnative

Practice Coding

  • Python ▼
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Coding Exercises ▼
    • Python Exercises
    • C++ Exercises
    • C Programming Exercises
    • Java Exercises
    • C# Exercises
  • MCQs▼
    • Python MCQ Quizzes
    • Java MCQ Quizzes
    • C++ MCQ Quizzes
  • Compilers▼
    • Online Python Compiler
    • Online C Compiler
    • Online C++ Compiler
    • Online Java Compiler
    • Online C# Compiler
Home » Python » Python Programs to Print Patterns – Print Number, Pyramid, Star, Triangle, Diamond, and Alphabets Patterns

Python Programs to Print Patterns – Print Number, Pyramid, Star, Triangle, Diamond, and Alphabets Patterns

Updated on: December 4, 2026 | 458 Comments

This Python lesson includes 35+ programs for printing number, pyramid, star, triangle, diamond, and alphabet patterns, so you can gain hands-on experience and build confidence in your Python skills.

Printing numbers, stars (asterisk), or other characters in different shapes (patterns) is a frequently asked interview question for freshers. Creating these number and pyramid patterns tests your logical thinking and coding skills.

In this lesson, you’ll learn how to print patterns using the for loop, while loop, and the range() function. Most patterns use nested loops: an outer loop for rows and an inner loop for columns.

This article teaches you how to print the following patterns in Python.

  • Number pattern
  • Pyramid pattern
  • Inverted pyramid pattern
  • Half pyramid pattern
  • Triangle pattern
  • Star (*) or asterisk pattern
  • Diamond Shaped pattern
  • Characters or alphabet pattern
  • Square pattern
Print Pattern in Python
Print Pattern in Python

Table of contents

  • Steps to Print Pattern in Python
  • Number Patterns Programs In Python
    • Pyramid pattern of numbers
    • Inverted pyramid pattern of numbers
    • Inverted Pyramid pattern with the same digit
    • Another inverted half-pyramid pattern with a number
    • Alternate numbers pattern using a while loop
    • Reverse number pattern
    • Reverse Pyramid of Numbers
    • Another reverse number pattern
    • Print reverse number from 10 to 1
    • Number triangle pattern
    • Pascal’s triangle pattern using numbers
    • Square pattern with numbers
    • Multiplication table pattern
  • Pyramid pattern of stars in python
    • Right triangle pyramid of Stars
    • Downward half-Pyramid Pattern of Star
    • Downward full Pyramid Pattern of star
    • Right down mirror star Pattern
    • Equilateral triangle pattern of star
    • Print two pyramids of stars
    • Right star pattern
    • Left triangle pascal’s pattern
    • Sandglass pattern of star
    • Pant style pattern of stars
  • Diamond-shaped pattern of stars
    • Another diamond pattern of star
  • Alphabets and letters pattern
    • Pattern to display letter of the word
    • Equilateral triangle pattern of characters/alphabets
    • Pattern of same character
  • More miscellaneous Patterns
    • Pyramid of horizontal number tables
    • Double the number pattern
    • Random number pattern
    • Pyramid of numbers less than 10
    • Pyramid of numbers up to 10
    • Even number pattern
    • Unique pyramid pattern of digits
    • Pattern double number on each column
    • Number reduction pattern
    • Pant style pattern of numbers
    • Pattern with a combination of numbers and stars
  • Practice Problem
  • Next Steps

Steps to Print Pattern in Python

To print any pattern, you must first understand its logic and technique. Use the below steps to print any pattern in Python.

  1. Decide the number of rows and columns

    The number of rows and columns is crucial when printing a pattern. To achieve this, we utilize two loops: outer loops and nested loops. The outer loop is the number of rows, while the inner loop tells us the column needed to print the pattern.

    The input () function accepts the number of rows from a user to decide the size of a pattern.

  2. Iterate rows

    Next, write an outer loop to Iterate the number of rows using a for loop and range() function.

  3. Iterate columns

    Next, write the inner or nested loop to handle the number of columns. The internal loop iteration depends on the values of the outer loop.

  4. Print star or number

    Use the print() function in each iteration of nested for loop to display the symbol or number of a pattern (like a star (asterisk *) or number).

  5. Add a new line after each iteration of the outer loop

    Add a new line using the print() function after each iteration of the outer loop so that the pattern displays appropriately

Algorithm for printing patterns in Python
Algorithm for printing patterns in Python

Also, Solve:

  • Python loop exercise
  • Python Basic Exercise for Beginners

Number Patterns Programs In Python

I have created various programs that print different styles of number patterns. Let’s see them one by one. Let’s print the following number pattern using a for loop.

1  
2 2  
3 3 3  
4 4 4 4  
5 5 5 5 5

Program:

rows = 6
# if you want user to enter a number, uncomment the below line
# rows = int(input('Enter the number of rows'))
# outer loop
for i in range(rows):
    # nested loop
    for j in range(i):
        # display number
        print(i, end=' ')
    # new line after each row
    print('')Code language: Python (python)

How it works: The outer loop picks the row number i. The inner loop range(i) runs exactly i times, so row i prints the number i, i times. Row 0 prints nothing, which is why the pattern starts with 1. After each row, print('') moves to a new line.

In this number pattern, we display a single digit on the first row, two digits on the second row, and three digits on the third row. This process will repeat until the number of rows is reached.

Note:

  • The count of numbers on each row is equal to the current row number.
  • Also, each number is separated by space.
  • We used a nested loop to print the pattern

Pyramid pattern of numbers

Let’s see how to print the following half-pyramid pattern of numbers

1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5

Note: In each row, every next number is incremented by 1.

Program:

rows = 5
for i in range(1, rows + 1):
    for j in range(1, i + 1):
        print(j, end=' ')
    print('')Code language: Python (python)

How it works: The outer loop counts rows from 1 to 5. For each row i, the inner loop prints the numbers 1 up to i, so every row starts again at 1 and is one number longer than the row above it.

Inverted pyramid pattern of numbers

An inverted pyramid is a downward pattern where numbers get reduced in each iteration, and the last row shows only one number. Use a reverse for loop to print this pattern.

Pattern

1 1 1 1 1 
2 2 2 2 
3 3 3 
4 4 
5

Program

rows = 5
b = 0
# reverse for loop from 5 to 0
for i in range(rows, 0, -1):
    b += 1
    for j in range(1, i + 1):
        print(b, end=' ')
    print('\r')Code language: Python (python)

How it works: The outer loop counts i down from 5 to 1, which controls how many numbers each row has. The variable b goes up by 1 per row and is the number that gets printed. So the first row prints 1 five times, the second prints 2 four times, and so on.

Inverted Pyramid pattern with the same digit

Pattern:

5 5 5 5 5 
5 5 5 5 
5 5 5 
5 5 
5

Program:

rows = 5
num = rows
# reverse for loop
for i in range(rows, 0, -1):
    for j in range(0, i):
        print(num, end=' ')
    print("\r")
Code language: Python (python)

How it works: The outer loop counts down from 5 to 1, so each row is one number shorter than the previous one. The inner loop prints the same value, num (which stays 5), i times.

Another inverted half-pyramid pattern with a number

Pattern:

0 1 2 3 4 5 
0 1 2 3 4 
0 1 2 3 
0 1 2 
0 1

Program

rows = 5
for i in range(rows, 0, -1):
    for j in range(0, i + 1):
        print(j, end=' ')
    print("\r")
Code language: Python (python)

How it works: The outer loop counts i down from 5 to 1. The inner loop prints the numbers 0 up to i, so the first row is 0 to 5, the next is 0 to 4, and the row shrinks by one number each time.

Alternate numbers pattern using a while loop

Let’s see how to use the while loop to print the number pattern.

Pattern:

1 
3 3 
5 5 5 
7 7 7 7 
9 9 9 9 9

Program:

rows = 5
i = 1
while i <= rows:
    j = 1
    while j <= i:
        print((i * 2 - 1), end=" ")
        j = j + 1
    i = i + 1
    print('')
Code language: Python (python)

How it works: This is the same row/column logic written with while loops. The outer loop runs once per row, and the inner loop prints i values in row i. The value printed is 2 * i - 1, which produces the odd numbers 1, 3, 5, 7, 9.

Reverse number pattern

Let’s see how to display the pattern of descending order of numbers

Pattern 1: –

5 5 5 5 5 
4 4 4 4 
3 3 3 
2 2 
1

This pattern is also called an inverted pyramid of descending numbers.

Program:

rows = 5
# reverse loop
for i in range(rows, 0, -1):
    num = i
    for j in range(0, i):
        print(num, end=' ')
    print("\r")
Code language: Python (python)

How it works: The outer loop counts i down from 5 to 1. In each row, the inner loop prints the row number i itself, i times. So the rows shrink in both value and length.

Reverse Pyramid of Numbers

Pattern 2:

1 
2 1 
3 2 1 
4 3 2 1 
5 4 3 2 1

Note: It is a downward increasing pattern, where the numbers increase in each iteration. The count of numbers in each row equals the current row number.

Program

rows = 6
for i in range(1, rows):
    for j in range(i, 0, -1):
        print(j, end=' ')
    print("")
Code language: Python (python)

How it works: The outer loop picks the row number i from 1 to 5. The inner loop counts down from i to 1 with range(i, 0, -1), so each row lists the numbers in reverse order and is one number longer than the row above.

Another reverse number pattern

Pattern:

5 4 3 2 1 
4 3 2 1 
3 2 1 
2 1 
1

Program:

rows = 5
for i in range(0, rows + 1):
    for j in range(rows - i, 0, -1):
        print(j, end=' ')
    print()Code language: Python (python)

How it works: Each row counts down from rows - i to 1. As i increases, the starting number becomes smaller, so the rows go 5 4 3 2 1, then 4 3 2 1, and so on.

Print reverse number from 10 to 1

Pattern:

1
3 2
6 5 4
10 9 8 7

Program:

start = 1
stop = 2
current_num = stop
for row in range(2, 6):
    for col in range(start, stop):
        current_num -= 1
        print(current_num, end=' ')
    print("")
    start = stop
    stop += row
    current_num = stopCode language: Python (python)

How it works: Each row holds one more number than the previous row: 1, then 2, then 3, then 4 numbers. start and stop track which numbers belong to the current row, and current_num starts at the row’s last number and counts down, which is why each row appears in reverse.

Number triangle pattern

Let’s see how to print the right-angled triangle pattern of numbers

Pattern:

          1 
        1 2 
      1 2 3 
    1 2 3 4 
  1 2 3 4 5 

Program:

rows = 6
for i in range(1, rows):
    num = 1
    for j in range(rows, 0, -1):
        if j > i:
            print(" ", end=' ')
        else:
            print(num, end=' ')
            num += 1
    print("")
Code language: Python (python)

How it works: Each row has the same width. The inner loop counts columns from rows down to 1: while the column number is greater than the row number, it prints a space. Otherwise, it prints the next number, starting from 1. This pushes the numbers to the right and forms a triangle.

Pascal’s triangle pattern using numbers

To build the pascal triangle, start with “1” at the top, then continue placing numbers below it in a triangular pattern.

Each number is the sum of the two numbers directly above it.

Pattern:

1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 
1 5 10 10 5 1 
1 6 15 20 15 6 1

Program:

def print_pascal_triangle(size):
    for i in range(0, size):
        for j in range(0, i + 1):
            print(decide_number(i, j), end=" ")
        print()


def decide_number(n, k):
    num = 1
    if k > n - k:
        k = n - k
    for i in range(0, k):
        num = num * (n - i)
        num = num // (i + 1)
    return num

# set rows
rows = 7
print_pascal_triangle(rows)
Code language: Python (python)

How it works: Every number in Pascal’s triangle is a binomial coefficient “n choose k”, where n is the row and k is the position in the row. decide_number() calculates that value with a short multiply-and-divide loop, and print_pascal_triangle() prints i + 1 values in row i.

Square pattern with numbers

Pattern:

1 2 3 4 5 
2 2 3 4 5 
3 3 3 4 5 
4 4 4 4 5 
5 5 5 5 5

Program:

rows = 5
for i in range(1, rows + 1):
    for j in range(1, rows + 1):
        if j <= i:
            print(i, end=' ')
        else:
            print(j, end=' ')
    print()
Code language: Python (python)

How it works: Every row has 5 columns. For each column j in row i, the program prints the larger of the two numbers: i while j <= i, and j after that. That’s why the row number repeats at the start of each row and the column numbers continue after it.

Multiplication table pattern

Pattern:

1  
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  

Program:

rows = 8
# rows = int(input("Enter the number of rows "))
for i in range(1, rows + 1):
    for j in range(1, i + 1):
        # multiplication current column and row
        square = i * j
        print(i * j, end='  ')
    print()
Code language: Python (python)

How it works: This is a triangle where each value is row × column. Row i has i columns, and the inner loop prints i * j for j from 1 to i, which gives the multiplication table of i up to i × i.

Pyramid pattern of stars in python

In this section, you’ll see how to print pyramid and star (asterisk) patterns in Python. Here we will print the following pyramid pattern with Star (asterisk).

  • Half pyramid pattern with stars(*)
  • Full pyramid pattern with stars
  • Inverted pyramid Pattern with stars
  • Triangle pattern with stars
  • Right-angled triangle pattern with stars

Simple half pyramid pattern:

* 
* * 
* * * 
* * * * 
* * * * * 

This pattern is also known as a right angle triangle pyramid.

Program:

# number of rows
rows = 5
for i in range(0, rows):
    # nested loop for each column
    for j in range(0, i + 1):
        # print star
        print("*", end=' ')
    # new line after each row
    print("\r")
Code language: Python (python)

How it works: The outer loop runs once per row. In row i, the inner loop runs i + 1 times and prints one star each time, so every row has one more star than the previous one. end=' ' keeps the stars on the same line.

Right triangle pyramid of Stars

Pattern:

        * 
      * * 
    * * * 
  * * * * 
* * * * * 

This pattern is also called a mirrored right triangle.

Program:

# 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("")
Code language: Python (python)

Alternative Solution:

rows = 5
for j in range(1, rows + 1):
    # print leading spaces, then the stars
    print("  " * (rows - j) + "* " * j)Code language: Python (python)

How it works: Each row needs two parts: leading spaces and stars. In row i, the first solution prints k spaces (k drops by 2 per row) and then i + 1 stars. The alternative builds the same row in one line: rows - j blank pairs, followed by j stars.

Downward half-Pyramid Pattern of Star

Pattern:

* * * * *  
* * * *  
* * *  
* *  
*

Note: Use a reverse nested loop to print the downward pyramid pattern of stars.

Program:

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(" ")
Code language: Python (python)

How it works: This is the half pyramid in reverse. The outer loop counts down, so the inner loop prints 5 stars in the first row, then 4, and so on until a single star is left.

Downward full Pyramid Pattern of star

Let’s see how to print a reversed pyramid pattern in Python.

Pattern:

        * * * * * * 
         * * * * * 
          * * * * 
           * * * 
            * * 
             * 

Program:

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("")
Code language: Python (python)

How it works: Each row prints k leading spaces and then i + 1 stars. The outer loop counts i down, so the star count shrinks, while k increases by 1 per row. The growing indent on the left creates the upside-down pyramid shape.

Right down mirror star Pattern

Pattern:

* * * * * 
  * * * * 
    * * * 
      * * 
        * 

In this pattern, we need to use two nested while loops.

Program:

rows = 5
i = rows
while i >= 1:
    j = rows
    while j > i:
        # display space
        print(' ', end=' ')
        j -= 1
    k = 1
    while k <= i:
        print('*', end=' ')
        k += 1
    print()
    i -= 1
Code language: Python (python)

How it works: Row i (counting down from 5) first prints rows - i blank spaces, then i stars. Each row has one more leading space and one fewer star, so the stars stay right-aligned and the triangle points downward.

Equilateral triangle pattern of star

Pattern:

            *   
           *  *   
          *  *  *   
         *  *  *  *   
        *  *  *  *  *   
       *  *  *  *  *  *   
      *  *  *  *  *  *  *   

Program:

# printing full Triangle pyramid using stars
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(" ")
Code language: Python (python)

How it works: Each row prints m leading spaces and then i + 1 stars. m starts large and drops by 1 per row, so every new row starts one position further left and holds one more star. The left edge moves left while the right edge moves right, which forms a symmetrical triangle.

Print two pyramids of stars

Pattern:

*  
* *  
* * *  
* * * *  
* * * * *  
* * * * * *  
 
* * * * * *  
* * * * *  
* * * *  
* * *  
* *  
*  

Program:

rows = 6
for i in range(0, rows):
    for j in range(0, i + 1):
        print("*", end=' ')
    print(" ")

print(" ")

for i in range(rows + 1, 0, -1):
    for j in range(0, i - 1):
        print("*", end=' ')
    print(" ")
Code language: Python (python)

How it works: This combines two programs. The first loop prints an increasing half pyramid (1 to 6 stars), and the second loop counts down to print a decreasing one (6 to 1 stars), with an empty line in between.

Right star pattern

Pattern:

* 
* * 
* * * 
* * * * 
* * * * * 
* * * * 
* * * 
* * 
* 

This pattern is also called a right Pascal’s triangle.

Program:

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")
Code language: Python (python)

How it works: The first loop prints an increasing half pyramid with 1 to 5 stars. The second loop then counts down from 5 and prints i - 1 stars, which gives 4, 3, 2, and 1 stars. Together, they form an arrow pointing to the right.

Left triangle pascal’s pattern

Pattern:

        * 
      * * 
    * * * 
  * * * * 
* * * * * 
  * * * * 
    * * * 
      * * 
        * 

Program:

rows = 5
i = 1
while i <= rows:
    j = i
    while j < rows:
        # display space
        print(' ', end=' ')
        j += 1
    k = 1
    while k <= i:
        print('*', end=' ')
        k += 1
    print()
    i += 1

i = rows
while i >= 1:
    j = i
    while j <= rows:
        print(' ', end=' ')
        j += 1
    k = 1
    while k < i:
        print('*', end=' ')
        k += 1
    print('')
    i -= 1
Code language: Python (python)

How it works: This is a mirrored version of the previous pattern. In the upper half, each row prints rows - i blank spaces followed by i stars, so the stars are right-aligned. The lower half repeats the logic in reverse, adding one space and removing one star per row.

Sandglass pattern of star

Pattern:

* * * * * 
 * * * * 
  * * * 
   * * 
    * 
    * 
   * * 
  * * * 
 * * * * 
* * * * * 

To print this pattern, we use two sets of three while loops: one set for the upper half and one for the lower half.

Program:

rows = 5
i = 0
while i <= rows - 1:
    j = 0
    while j < i:
        # display space
        print('', end=' ')
        j += 1
    k = i
    while k <= rows - 1:
        print('*', end=' ')
        k += 1
    print()
    i += 1

i = rows - 1
while i >= 0:
    j = 0
    while j < i:
        print('', end=' ')
        j += 1
    k = i
    while k <= rows - 1:
        print('*', end=' ')
        k += 1
    print('')
    i -= 1
Code language: Python (python)

How it works: In the top half, row i prints i single spaces and then rows - i stars, so each row gets shorter and shifts right. The bottom half runs the same loops with i counting down, so the rows grow back out, forming an hourglass.

Pant style pattern of stars

Pattern:

****************
*******__*******
******____******
*****______*****
****________****
***__________***
**____________**
*______________*

Program:

rows = 16
print("*" * rows, end="\n")
i = (rows // 2) - 1
j = 2
while i != 0:
    while j <= (rows - 2):
        print("*" * i, end="")
        print("_" * j, end="")
        print("*" * i, end="\n")
        i = i - 1
        j = j + 2Code language: Python (python)

How it works: The first line is a full row of 16 stars. In each following row, i (the stars on each side) shrinks by 1, while j (the underscores in the middle) grows by 2. The total width stays 16, so the gap in the middle opens up like a pair of pants.

Diamond-shaped pattern of stars

Pattern:

        * 
       * * 
      * * * 
     * * * * 
    * * * * * 
   * * * * * * 
    * * * * * 
     * * * * 
      * * * 
       * * 
        * 

Program:

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("")Code language: Python (python)

How it works: The diamond is two pyramids. The first loop prints a centered pyramid that grows from 1 to 5 stars, using k leading spaces that decrease each row. The second loop counts down and increases k, which prints the upside-down half underneath.

Another diamond pattern of star

Pattern:

        * 
      *   *
    *       *
  *           *
*               *
  *             *
    *         *
      *     *
        * 

Program:

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
Code language: Python (python)

How it works: Each row prints leading spaces, a star, a gap, and a second star. The leading spaces shrink as i grows, and the gap between the two stars is 2 * (i - 1) wide, so the outline widens. The first row has only one star. The second half repeats the steps with i counting down, so the outline closes again.

Alphabets and letters pattern

Every letter has a numeric ASCII (Unicode) value. For example, ‘A’ is 65. To print letter patterns, we loop over these numbers and convert each one back to a letter with the built-in chr() function. (ord() does the reverse.)

  • Decide the number of rows
  • Start with ASCII number 65 ( ‘A’)
  • Iterate a loop and in nested for loop use the char function to convert ASCII number to its equivalent letter.

Now let’s see how to print alphabet and letter patterns in Python.

Pattern:

A  
B C  
D E F  
G H I J  
K L M N O  
P Q R S T U  
V W X Y Z [ \ 

This pattern is known as a right-angled pattern with characters.

Program:

# ASCII number of 'A'
ascii_number = 65
rows = 7
for i in range(0, rows):
    for j in range(0, i + 1):
        character = chr(ascii_number)
        print(character, end=' ')
        ascii_number += 1
    print(" ")Code language: Python (python)

How it works: Letters are printed using their character codes: 65 is ‘A’. Row i prints i + 1 characters, and after each one, ascii_number increases by 1. chr() converts each code to the next letter. With 7 rows, the codes go past ‘Z’ (90), which is why ‘[‘ and ‘\’ appear at the end.

Pattern to display letter of the word

Let’s see how to print the word ‘Python’ as a pattern:

P
Py
Pyt
Pyth
Pytho
Python

Program:

word = "Python"
x = ""
for i in word:
    x += i
    print(x)
Code language: Python (python)

How it works: x starts as an empty string. The loop adds one letter of the word to x in each iteration and prints it, so every line shows one more letter than the previous line.

Equilateral triangle pattern of characters/alphabets

Pattern:

            A  
           B C  
          D E F  
         G H I J  
        K L M N O  
       P Q R S T U  
      V W X Y Z A B  

Program:

size = 7
ascii_number = 65
m = (2 * size) - 2
for i in range(0, size):
    for j in range(0, m):
        print(end=" ")
    m = m - 1
    for j in range(0, i + 1):
        character = chr(ascii_number)
        print(character, end=' ')
        ascii_number += 1
        # start again at 'A' after 'Z'
        if ascii_number > 90:
            ascii_number = 65
    print(" ")Code language: Python (python)

How it works: This uses the same spacing logic as the equilateral star triangle. m leading spaces decrease by 1 per row to keep the triangle centered. Instead of stars, each position prints the next letter, using chr() on an ASCII code that increases by 1. After Z (ASCII 90), the code resets to 65 so the letters start again at A instead of printing symbols such as [ and \.

Pattern of same character

Pattern:

V 
V V 
V V V 
V V V V 
V V V V V 

Program:

# Same character pattern
character = 'V'
# convert char to ASCII
char_ascii_no = ord(character)
for i in range(0, 5):
    for j in range(0, i + 1):
        # Convert the ASCII value to the character
        user_char = chr(char_ascii_no)
        print(user_char, end=' ')
    print()Code language: Python (python)

How it works: This is the star half pyramid with a letter instead of a star. ord('V') gets the character code of ‘V’, and chr() turns it back into ‘V’. Row i prints that letter i + 1 times.

Let’s see some more miscellaneous patterns

More miscellaneous Patterns

Pyramid of horizontal number tables

Pattern:

1 
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 

Program:

# Pyramid of horizontal tables of numbers
rows = 10
for i in range(1, rows + 1):
    for j in range(1, i + 1):
        print(i * j, end=' ')
    print()
Code language: Python (python)

How it works: Row i is the multiplication table of i, cut off after i terms. The inner loop prints i * j for j from 1 to i, so each row is one value longer than the previous row.

Double the number pattern

Pattern:

   1 
   2    1 
   4    2    1 
   8    4    2    1 
  16    8    4    2    1 
  32   16    8    4    2    1 
  64   32   16    8    4    2    1 
 128   64   32   16    8    4    2    1 

Note: In each row, every number is double the preceding number.

Program:

rows = 9
for i in range(1, rows):
    for j in range(-1 + i, -1, -1):
        print(format(2 ** j, "4d"), end=' ')
    print("")Code language: Python (python)

How it works: Each value is a power of 2. In row i, the inner loop counts the exponent j down from i - 1 to 0 and prints 2 ** j, which gives a descending series such as 8 4 2 1. format(..., "4d") pads each number to 4 characters so the columns line up.

Random number pattern

   1 
   1    2    1 
   1    2    4    2    1 
   1    2    4    8    4    2    1 
   1    2    4    8   16    8    4    2    1 
   1    2    4    8   16   32   16    8    4    2    1 
   1    2    4    8   16   32   64   32   16    8    4    2    1 
   1    2    4    8   16   32   64  128   64   32   16    8    4    2    1 

Program:

rows = 9
for i in range(1, rows):
    for i in range(0, i, 1):
        print(format(2 ** i, "4d"), end=' ')
    for i in range(-1 + i, -1, -1):
        print(format(2 ** i, "4d"), end=' ')
    print("")Code language: Python (python)

How it works: Each row is a sequence of powers of 2 that rises and then falls. The first inner loop prints 2 ** 0 up to 2 ** (i - 1). The second loop counts the exponent back down, which mirrors the row around its largest value.

Pyramid of numbers less than 10

Pattern:

1 
2 3 4 
5 6 7 8 9

Program:

current_num = 1
stop = 2
rows = 3

for i in range(rows):
    for column in range(1, stop):
        print(current_num, end=' ')
        current_num += 1
    print("")
    stop += 2
Code language: Python (python)

How it works: A single counter, current_num, keeps increasing across all rows. stop controls how many numbers each row gets: 1, 3, then 5 (it grows by 2), so the numbers 1 to 9 fill a pyramid.

Pyramid of numbers up to 10

Pattern:

1 
2 3 
4 5 6 
7 8 9 10

Program:

current_num = 1
rows = 4
stop = 2
for i in range(rows):
    for column in range(1, stop):
        print(current_num, end=' ')
        current_num += 1
    print("")
    stop += 1Code language: Python (python)

How it works: This is the same idea as the previous pattern, but stop grows by 1 per row. So the rows contain 1, 2, 3, and 4 numbers, which uses up exactly the numbers 1 to 10.

Even number pattern

Pattern:

10 
10 8 
10 8 6 
10 8 6 4 
10 8 6 4 2

Programs:

rows = 5
last_num = 2 * rows
even_num = last_num
for i in range(1, rows + 1):
    even_num = last_num
    for j in range(i):
        print(even_num, end=' ')
        even_num -= 2
    print("\r")
Code language: Python (python)

How it works: Every row starts again at the largest even number, 2 * rows (10). Row i prints i numbers, subtracting 2 each time, so the rows grow longer while counting down 10, 8, 6, and so on.

Unique pyramid pattern of digits

Pattern:

1 
1 2 1 
1 2 3 2 1 
1 2 3 4 3 2 1 
1 2 3 4 5 4 3 2 1

Program:

rows = 6
for i in range(1, rows + 1):
    for j in range(1, i - 1):
        print(j, end=" ")
    for j in range(i - 1, 0, -1):
        print(j, end=" ")
    print()
Code language: Python (python)

How it works: Each row has two halves. The first inner loop counts up from 1 to i - 2, and the second loop counts down from i - 1 to 1. Together, they create a palindrome such as 1 2 3 2 1, with the largest number in the middle.

Pattern double number on each column

Pattern:

0  
0  1  
0  2  4  
0  3  6  9  
0  4  8  12  16  
0  5  10  15  20  25  
0  6  12  18  24  30  36

Program:

rows = 7
for i in range(0, rows):
    for j in range(0, i + 1):
        print(i * j, end='  ')
    print()Code language: Python (python)

How it works: Each value is row × column, with both counted from 0. So the first column is always 0, and row i holds the multiples of i (0, i, 2i, …), one more value per row.

Number reduction pattern

Pattern:

1 2 3 4 5 
2 3 4 5 
3 4 5 
4 5 
5

Program:

rows = 5
for i in range(0, rows + 1, 1):
    for j in range(i + 1, rows + 1, 1):
        print(j, end=' ')
    print()Code language: Python (python)

How it works: Row i starts at i + 1 and counts up to 5. As the starting number rises with each row, one number disappears from the front of the row.

Pant style pattern of numbers

Pattern:

5 4 3 2 1 1 2 3 4 5 

5 4 3 2     2 3 4 5 

5 4 3         3 4 5 

5 4             4 5 

5                 5

Program:

rows = 6
for i in range(0, rows):
    for j in range(rows - 1, i, -1):
        print(j, '', end='')
    for l in range(i):
        print('    ', end='')
    for k in range(i + 1, rows):
        print(k, '', end='')
    print('\n')Code language: Python (python)

How it works: Each row has three parts. The first loop counts down from 5 to just above i, the middle loop prints a 4-character gap i times, and the last loop counts back up. The gap widens each row, so the two sides separate like the legs of a pair of pants.

Pattern with a combination of numbers and stars

Pattern:

1 * 2 * 3 * 4 

1 * 2 * 3 

1 * 2 

1

Program:

row = 4
for i in range(0, row):
    c = 1
    print(c, end=' ')
    for j in range(row - i - 1, 0, -1):
        print('*', end=' ')
        c = c + 1
        print(c, end=' ')
    print('\n')
Code language: Python (python)

How it works: Row i starts with 1 and then repeats row - i - 1 times: print a star, increase the counter c, and print it. So the first row is 1 * 2 * 3 * 4, and every following row has one star and one number fewer.

Also, see how to calculate the sum and average in Python.

Practice Problem

Pattern:

0 
2 4 
4 8 8 
8 16 16 16

Solution:

num = 4
counter = 0
for x in range(0, num):
    for y in range(0, x + 1):
        print(counter, end=" ")
        counter = 2 ** (x + 1)
    print()Code language: Python (python)

Next Steps

Solve:

  • Python Basic Exercise for Beginners
  • Python if-else and for loop exercise
  • Python Quiz for beginners

If you can’t find the pattern you’re looking for, let me know by leaving a comment below.

Filed Under: Python, Python Basics

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

Python Python Basics

All Coding Exercises:

C Exercises
C++ Exercises
Python Exercises
Java Exercises
C# Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 25+ questions
  • Each Quiz contains 25 MCQ
Exercises
Quizzes
In: Python Python Basics
TweetF  sharein  shareP  Pin

  Python Tutorials

  • Get Started with Python
  • Python Statements
  • Python Comments
  • Python Keywords
  • Python Variables
  • Python Operators
  • Python Data Types
  • Python Casting
  • Python Control Flow statements
  • Python For Loop
  • Python While Loop
  • Python Break and Continue
  • Python Nested Loops
  • Python Input and Output
  • Python range function
  • Check user input is String or Number
  • Accept List as a input from user
  • Python Numbers
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python Functions
  • Python Modules
  • Python isinstance()
  • Python OOP
  • Python Inheritance
  • Python Exceptions
  • Python Exercise for Beginners
  • Python Quiz for Beginners

 Explore Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Python Interview Q&A
  • Python Programs

All Python Topics

  • Python Basics
  • Python Exercises
  • Python Quizzes
  • Python File Handling
  • Python Date and Time
  • Python OOP
  • Python Random
  • Python Regex
  • Python Pandas
  • Python Databases
  • Python MySQL
  • Python PostgreSQL
  • Python SQLite
  • Python JSON

About PYnative

PYnative.com is a platform built to help developers learn by doing. Solve coding exercises, take MCQs, and run code directly in built-in online compilers.

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Coding Exercises

  • C Exercises
  • C++ Exercises
  • Python Exercises
  • Java Exercises
  • C# Exercises

MCQ Quizzes

  • Python MCQs
  • Java MCQs
  • C++ MCQs

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2026 pynative.com