Understanding Pattern Printing

Pattern printing is a common programming exercise that helps you understand nested loops, control flow, and logic building. Every pattern uses the same fundamental concepts:

Key Concepts

  • Outer Loop: Controls the number of rows
  • Inner Loop: Controls what gets printed in each column
  • end parameter: print("*", end=" ") prevents newline
  • Empty print(): Moves to the next line after each row
pattern_basics.py
# Basic Pattern Structure
n = 5  # Number of rows

for i in range(1, n + 1):          # Outer loop: rows (1 to 5)
    for j in range(1, i + 1):      # Inner loop: columns (1 to i)
        print("*", end=" ")        # Print star, stay on same line
    print()                        # Move to next line

# Output:
# * 
# * * 
# * * * 
# * * * * 
# * * * * * 

Pro Tips

  • Use range(n, 0, -1) for decreasing patterns
  • Use " " * n to create spaces for alignment
  • Use chr(65 + i) to print alphabets (A=65 in ASCII)

Pattern 1: Right Triangle Star Pattern

The most basic pattern - a right-angled triangle made of stars.

right_triangle.py
# Right Triangle Star Pattern
n = 5

for i in range(1, n + 1):
    for j in range(1, i + 1):
        print("*", end=" ")
    print()

Output:

Output
* 
* * 
* * * 
* * * * 
* * * * * 

How it works

  • Row 1: inner loop runs 1 time → prints 1 star
  • Row 2: inner loop runs 2 times → prints 2 stars
  • Row i: inner loop runs i times → prints i stars

Pattern 2: Inverted Right Triangle

An upside-down right triangle - starts with maximum stars and decreases.

inverted_triangle.py
# Inverted Right Triangle
n = 5

for i in range(n, 0, -1):
    for j in range(1, i + 1):
        print("*", end=" ")
    print()

Output:

Output
* * * * * 
* * * * 
* * * 
* * 
* 

How it works

range(n, 0, -1) goes from n down to 1. So row 1 has 5 stars, row 2 has 4 stars, etc.

Pattern 3: Pyramid Pattern

A centered pyramid shape - requires spaces for alignment.

pyramid.py
# Pyramid Pattern
n = 5

for i in range(1, n + 1):
    # Print leading spaces
    print(" " * (n - i), end="")
    # Print stars
    print("* " * i)

Output:

Output
    * 
   * * 
  * * * 
 * * * * 
* * * * * 

How it works

  • Row 1: (5-1)=4 spaces, then 1 star
  • Row 2: (5-2)=3 spaces, then 2 stars
  • Row 5: (5-5)=0 spaces, then 5 stars

Pattern 4: Diamond Pattern

A diamond shape - combination of pyramid and inverted pyramid.

diamond.py
# Diamond Pattern
n = 5

# Upper half (pyramid)
for i in range(1, n + 1):
    print(" " * (n - i), end="")
    print("* " * i)

# Lower half (inverted pyramid)
for i in range(n - 1, 0, -1):
    print(" " * (n - i), end="")
    print("* " * i)

Output:

Output
    * 
   * * 
  * * * 
 * * * * 
* * * * * 
 * * * * 
  * * * 
   * * 
    * 

How it works

  • Upper half: Uses pyramid logic (spaces decrease, stars increase)
  • Lower half: Uses inverted pyramid (spaces increase, stars decrease)
  • Lower loop starts from n-1 to avoid repeating the middle row

Pattern 5: Hollow Square

A square with stars only on the borders.

hollow_square.py
# Hollow Square Pattern
n = 5

for i in range(1, n + 1):
    for j in range(1, n + 1):
        # Print star if on border (first/last row or first/last column)
        if i == 1 or i == n or j == 1 or j == n:
            print("*", end=" ")
        else:
            print(" ", end=" ")
    print()

Output:

Output
* * * * * 
*       * 
*       * 
*       * 
* * * * * 

How it works

Stars are printed only when we're on row 1, row n, column 1, or column n. Otherwise, print a space.

Pattern 6: Number Triangle

A triangle pattern with numbers instead of stars.

number_triangle.py
# Number Triangle Pattern
n = 5

for i in range(1, n + 1):
    for j in range(1, i + 1):
        print(j, end=" ")
    print()

Output:

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

How it works

  • Outer loop controls rows (1 to n)
  • Inner loop prints column numbers from 1 to current row number
  • Each row resets: j always starts from 1

Pattern 7: Floyd's Triangle

Numbers arranged in a triangle with continuous counting.

floyds_triangle.py
# Floyd's Triangle
n = 5
num = 1

for i in range(1, n + 1):
    for j in range(1, i + 1):
        print(num, end=" ")
        num += 1
    print()

Output:

Output
1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 

How it works

  • Uses a counter variable num that starts at 1
  • Counter increments after each print (never resets)
  • Named after Robert Floyd, computer scientist

Pattern 8: Pascal's Triangle

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

pascals_triangle.py
# Pascal's Triangle
n = 5

for i in range(n):
    # Print leading spaces for alignment
    print(" " * (n - i - 1), end="")
    
    num = 1
    for j in range(i + 1):
        print(num, end=" ")
        num = num * (i - j) // (j + 1)
    print()

Output:

Output
    1 
   1 1 
  1 2 1 
 1 3 3 1 
1 4 6 4 1 

Formula

Each element = C(row, col) = row! / (col! × (row-col)!). The code uses an optimized calculation.

Pattern 9: Multiplication Table

A grid showing multiplication results.

multiplication_table.py
# Multiplication Table (5x5)
n = 5

for i in range(1, n + 1):
    for j in range(1, n + 1):
        print(f"{i*j:3}", end=" ")
    print()

Output:

Output
  1   2   3   4   5 
  2   4   6   8  10 
  3   6   9  12  15 
  4   8  12  16  20 
  5  10  15  20  25 

How it works

  • i represents row number (1-5)
  • j represents column number (1-5)
  • Each cell = i × j (row times column)
  • f"{i*j:3}" formats with 3-character width for alignment

Pattern 10: Number Pyramid

A centered pyramid with repeating row numbers.

number_pyramid.py
# Number Pyramid
n = 5

for i in range(1, n + 1):
    # Print leading spaces
    print(" " * (n - i), end="")
    # Print numbers
    for j in range(1, i + 1):
        print(i, end=" ")
    print()

Output:

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

How it works

  • Leading spaces: (n - i) spaces to center the pyramid
  • Inner loop prints the row number i repeatedly
  • Row 1 prints "1" once, Row 2 prints "2" twice, etc.

Pattern 11: Alphabet Triangle

A triangle pattern using letters A, B, C...

alphabet_triangle.py
# Alphabet Triangle Pattern
n = 5

for i in range(1, n + 1):
    for j in range(i):
        print(chr(65 + j), end=" ")  # 65 is ASCII for 'A'
    print()

Output:

Output
A 
A B 
A B C 
A B C D 
A B C D E 

ASCII Reference

chr(65) = 'A', chr(66) = 'B', ... chr(90) = 'Z'. Use ord('A') to get 65.

Pattern 12: Hollow Pyramid

A pyramid with hollow inside - only borders are printed.

hollow_pyramid.py
# Hollow Pyramid Pattern
n = 5

for i in range(1, n + 1):
    for j in range(n - i):
        print(" ", end="")
    for j in range(2 * i - 1):
        if j == 0 or j == 2 * i - 2 or i == n:
            print("*", end="")
        else:
            print(" ", end="")
    print()

Output:

Output
    *
   * *
  *   *
 *     *
*********

How it works

  • Print star only at first position (j==0), last position (j==2i-2), or last row
  • Leading spaces = (n - i) for centering
  • Width of each row = (2 * i - 1) characters

Pattern 13: Sandglass Pattern

An hourglass shape - inverted pyramid followed by pyramid.

sandglass.py
# Sandglass Pattern
n = 5

# Upper half (inverted pyramid)
for i in range(n, 0, -1):
    print(" " * (n - i), end="")
    print("* " * i)

# Lower half (pyramid without first row)
for i in range(2, n + 1):
    print(" " * (n - i), end="")
    print("* " * i)

Output:

Output
* * * * * 
 * * * * 
  * * * 
   * * 
    * 
   * * 
  * * * 
 * * * * 
* * * * * 

How it works

  • Upper half: Inverted pyramid (starts with n stars, decreases)
  • Lower half: Normal pyramid (starts from 2, increases to n)
  • Lower loop starts at 2 to avoid printing single star twice

Pattern 14: Right Arrow Pattern

An arrow pointing to the right.

right_arrow.py
# Right Arrow Pattern
n = 5

# Upper half
for i in range(1, n + 1):
    print("* " * i)

# Lower half
for i in range(n - 1, 0, -1):
    print("* " * i)

Output:

Output
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * 
* * * 
* * 
* 

How it works

  • Upper half: Right triangle (1 to n stars)
  • Lower half: Inverted right triangle (n-1 down to 1 stars)
  • No centering needed - starts from left edge

Pattern 15: Butterfly Pattern

A symmetrical butterfly shape.

butterfly.py
# Butterfly Pattern
n = 5

# Upper half
for i in range(1, n + 1):
    print("* " * i, end="")
    print("  " * (n - i), end="")
    print("* " * i)

# Lower half
for i in range(n, 0, -1):
    print("* " * i, end="")
    print("  " * (n - i), end="")
    print("* " * i)

Output:

Output
*         * 
* *     * * 
* * *   * * * 
* * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * 
* * *   * * * 
* *     * * 
*         * 

How it works

  • Each row has: left stars + spaces in middle + right stars
  • Upper half: Stars increase, spaces decrease
  • Lower half: Stars decrease, spaces increase
  • Middle spaces = 2 × (n - i) for proper gap

Pattern 16: Plus (+) Pattern

A plus sign made of stars.

plus_pattern.py
# Plus Pattern
n = 5

for i in range(n):
    for j in range(n):
        if i == n // 2 or j == n // 2:
            print("*", end=" ")
        else:
            print(" ", end=" ")
    print()

Output:

Output
    *     
    *     
* * * * * 
    *     
    *     

How it works

  • Star printed when row is middle (i == n//2) OR column is middle (j == n//2)
  • n//2 = 2 for n=5, so middle row and column are index 2
  • Uses OR condition to create the + shape

Pattern 17: X Pattern

An X shape made of stars.

x_pattern.py
# X Pattern
n = 5

for i in range(n):
    for j in range(n):
        if i == j or i + j == n - 1:
            print("*", end=" ")
        else:
            print(" ", end=" ")
    print()

Output:

Output
*       * 
  *   *   
    *     
  *   *   
*       * 

How it works

  • i == j gives the main diagonal (\)
  • i + j == n - 1 gives the anti-diagonal (/)

Pattern 18: Hollow Diamond

A diamond shape with hollow center.

hollow_diamond.py
# Hollow Diamond Pattern
n = 5

# Upper half
for i in range(1, n + 1):
    print(" " * (n - i), end="")
    if i == 1:
        print("*")
    else:
        print("*" + " " * (2 * i - 3) + "*")

# Lower half
for i in range(n - 1, 0, -1):
    print(" " * (n - i), end="")
    if i == 1:
        print("*")
    else:
        print("*" + " " * (2 * i - 3) + "*")

Output:

Output
    *
   * *
  *   *
 *     *
*       *
 *     *
  *   *
   * *
    *

How it works

  • First row and last row: print single star only
  • Other rows: print star, spaces in middle, star
  • Middle spaces = (2 × i - 3) to create hollow effect

Pattern 19: Checkerboard Pattern

A chess-like alternating pattern.

checkerboard.py
# Checkerboard Pattern
n = 5

for i in range(n):
    for j in range(n):
        if (i + j) % 2 == 0:
            print("*", end=" ")
        else:
            print("#", end=" ")
    print()

Output:

Output
* # * # * 
# * # * # 
* # * # * 
# * # * # 
* # * # * 

How it works

  • If (i + j) is even, print * ; if odd, print #
  • Alternating pattern created by checking sum of indices
  • Like a chess board - adjacent cells are always different

Pattern 20: Number Square

Numbers arranged in increasing order in a square grid.

number_square.py
# Number Square Pattern
n = 5
num = 1

for i in range(n):
    for j in range(n):
        print(f"{num:2}", end=" ")
        num += 1
    print()

Output:

Output
 1  2  3  4  5 
 6  7  8  9 10 
11 12 13 14 15 
16 17 18 19 20 
21 22 23 24 25 

How it works

  • Counter num starts at 1, increments with each print
  • f"{num:2}" ensures 2-digit width for alignment
  • n×n grid = 25 numbers for n=5

Pattern 21: Hollow Rhombus

A parallelogram with hollow center.

hollow_rhombus.py
# Hollow Rhombus Pattern
n = 5

for i in range(1, n + 1):
    # Leading spaces
    print(" " * (n - i), end="")
    for j in range(1, n + 1):
        if i == 1 or i == n or j == 1 or j == n:
            print("*", end=" ")
        else:
            print(" ", end=" ")
    print()

Output:

Output
    * * * * * 
   *       * 
  *       * 
 *       * 
* * * * * 

How it works

  • Leading spaces decrease each row (n-i) - creates slant
  • Stars printed only on borders (first/last row or column)
  • Inner cells are spaces to create hollow effect

Pattern 22: Number Diamond

A diamond pattern with numbers.

number_diamond.py
# Number Diamond Pattern
n = 5

# Upper half
for i in range(1, n + 1):
    print(" " * (n - i), end="")
    for j in range(1, i + 1):
        print(j, end=" ")
    print()

# Lower half
for i in range(n - 1, 0, -1):
    print(" " * (n - i), end="")
    for j in range(1, i + 1):
        print(j, end=" ")
    print()

Output:

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

How it works

  • Upper half: Numbers increase (1 to i) per row
  • Lower half: Mirror of upper (decreasing rows)
  • Each row starts from 1, unlike Floyd's triangle

Pattern 23: Inverted Pyramid

A centered upside-down pyramid.

inverted_pyramid.py
# Inverted Pyramid Pattern
n = 5

for i in range(n, 0, -1):
    print(" " * (n - i), end="")
    print("* " * i)

Output:

Output
* * * * * 
 * * * * 
  * * * 
   * * 
    * 

How it works

  • range(n, 0, -1) goes from n down to 1
  • Spaces increase each row (n - i) to maintain center
  • Stars decrease from n to 1

Pattern 24: Binary Triangle

A triangle with alternating 1s and 0s.

binary_triangle.py
# Binary Triangle Pattern
n = 5

for i in range(1, n + 1):
    for j in range(1, i + 1):
        print((i + j) % 2, end=" ")
    print()

Output:

Output
0 
1 0 
0 1 0 
1 0 1 0 
0 1 0 1 0 

How it works

  • (i + j) % 2 gives alternating 0 and 1
  • Each row starts with opposite of previous row
  • Like checkerboard but in triangle shape

Pattern 25: Mirrored Triangles

Two right triangles mirroring each other.

mirrored_triangles.py
# Mirrored Right Triangles
n = 5

for i in range(1, n + 1):
    # Left triangle
    print("* " * i, end="")
    # Spaces in middle
    print("  " * (n - i), end="")
    # Right triangle
    print("* " * i)

Output:

Output
*         * 
* *       * * 
* * *     * * * 
* * * *   * * * * 
* * * * * * * * * * 

How it works

  • Left triangle: i stars (increases)
  • Middle gap: 2×(n-i) spaces (decreases)
  • Right triangle: i stars (same as left)

Pattern 26: Christmas Tree

A simple tree shape with a trunk.

christmas_tree.py
# Christmas Tree Pattern
n = 5

# Tree top (pyramid)
for i in range(1, n + 1):
    print(" " * (n - i), end="")
    print("* " * i)

# Trunk
for i in range(2):
    print(" " * (n - 1), end="")
    print("*")

Output:

Output
    * 
   * * 
  * * * 
 * * * * 
* * * * * 
    *
    *

How it works

  • Tree part: Standard pyramid pattern
  • Trunk: 2 single stars centered below the tree
  • Trunk position: (n-1) spaces to align with center

Pattern 27: Simple Heart Pattern

A heart shape using stars.

heart.py
# Simple Heart Pattern
n = 6

# Top part of heart (two bumps)
for i in range(n // 2, n, 2):
    print(" " * (n - i), end="")
    print("*" * i, end="")
    print(" " * ((n - i) + 1), end="")
    print("*" * i)

# Bottom part (inverted pyramid)
for i in range(n, 0, -1):
    print(" " * (n - i), end="")
    print("* " * i)

Output:

Output
   ***    ***
 *****  *****
* * * * * * 
 * * * * * 
  * * * * 
   * * * 
    * * 
     * 

How it works

  • Top part: Two "bumps" created with solid stars
  • Bottom part: Inverted pyramid narrowing down
  • Complex pattern - focus on understanding the output visually

Pattern 28: Hourglass Pattern

An hourglass shape (inverted pyramid + pyramid).

hourglass.py
# Hourglass Pattern
n = 5

# Upper half (inverted pyramid)
for i in range(n, 0, -1):
    print(" " * (n - i), end="")
    print("* " * i)

# Lower half (pyramid, skip first row as it's already printed)
for i in range(2, n + 1):
    print(" " * (n - i), end="")
    print("* " * i)

Output:

Output
* * * * * 
 * * * * 
  * * * 
   * * 
    * 
   * * 
  * * * 
 * * * * 
* * * * * 

How it works

  • Upper half: Inverted centered pyramid (n to 1 stars)
  • Lower half: Normal centered pyramid (2 to n stars)
  • Same as sandglass - just different visualization

Pattern 29: Zero-One Triangle

Rows alternate between starting with 1 or 0.

zero_one_triangle.py
# Zero-One Triangle Pattern
n = 5

for i in range(1, n + 1):
    for j in range(1, i + 1):
        if (i + j) % 2 == 0:
            print("1", end=" ")
        else:
            print("0", end=" ")
    print()

Output:

Output
1 
0 1 
1 0 1 
0 1 0 1 
1 0 1 0 1 

How it works

  • If (i + j) is even, print 1; if odd, print 0
  • Row 1 (odd) starts with 1, Row 2 (even) starts with 0
  • Each row alternates: 1 0 1 0... or 0 1 0 1...

Pattern 30: Palindrome Number Triangle

Numbers that read same forwards and backwards in each row.

palindrome_triangle.py
# Palindrome Number Triangle
n = 5

for i in range(1, n + 1):
    # Leading spaces
    print(" " * (n - i), end="")
    
    # First half: increasing numbers
    for j in range(1, i + 1):
        print(j, end="")
    
    # Second half: decreasing numbers
    for j in range(i - 1, 0, -1):
        print(j, end="")
    
    print()

Output:

Output
    1
   121
  12321
 1234321
123454321

Pattern Logic

  • First half: Print 1 to i
  • Second half: Print (i-1) to 1
  • Spaces for centering: (n - i) spaces