⚡ TL;DR
Learn advanced Python pattern programs — Floyd's Triangle, Pascal's Triangle, Butterfly, Spiral Matrix, ZigZag, and more. Full source code with examples.
In this tutorial, we’ll explore 10 advanced pattern programs in Python that go beyond basic triangles. These patterns are frequently asked in technical interviews and are excellent for mastering nested loops and 2D arrays.
1. Floyd’s Triangle
Print Floyd’s triangle (consecutive numbers in rows).
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15def floyds_triangle(n):
num = 1
for i in range(1, n + 1):
for j in range(i):
print(num, end=" ")
num += 1
print()
floyds_triangle(5)2. Pascal’s Triangle
Print Pascal’s triangle (binomial coefficients).
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1def pascals_triangle(n):
for i in range(n):
# Print leading spaces
print(" " * (n - i - 1), end="")
num = 1
for j in range(i + 1):
print(num, end=" ")
num = num * (i - j) // (j + 1)
print()
pascals_triangle(5)3. Butterfly Pattern
Print a butterfly shape.
* *
** **
*** ***
**** ****
**********
**** ****
*** ***
** **
* *def butterfly(n):
# Upper half
for i in range(1, n + 1):
print("*" * i + " " * (2 * (n - i)) + "*" * i)
# Lower half
for i in range(n, 0, -1):
print("*" * i + " " * (2 * (n - i)) + "*" * i)
butterfly(5)4. Spiral Matrix Pattern
Fill a matrix in spiral order and print it.
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9def spiral_matrix(n):
matrix = [[0] * n for _ in range(n)]
num = 1
top, bottom, left, right = 0, n - 1, 0, n - 1
while top <= bottom and left <= right:
for i in range(left, right + 1):
matrix[top][i] = num
num += 1
top += 1
for i in range(top, bottom + 1):
matrix[i][right] = num
num += 1
right -= 1
for i in range(right, left - 1, -1):
matrix[bottom][i] = num
num += 1
bottom -= 1
for i in range(bottom, top - 1, -1):
matrix[i][left] = num
num += 1
left += 1
for row in matrix:
print(" ".join(f"{x:2}" for x in row))
spiral_matrix(5)5. ZigZag Pattern
Print a zigzag wave of numbers.
1 5 9
2 4 6 8
3 7def zigzag(n, rows=3):
cols = 2 * n - 1
grid = [[" " for _ in range(cols)] for _ in range(rows)]
r, c = 0, 0
direction = 1 # moving down
for num in range(1, n + 1):
grid[r][c] = str(num)
if r == rows - 1:
direction = -1
elif r == 0:
direction = 1
r += direction
c += 2
for row in grid:
print(" ".join(row).rstrip())
zigzag(9, 3)6. Sandglass Pattern
Print a sandglass (hourglass) shape with numbers.
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8
3 4 5 6 7
4 5 6
5
4 5 6
3 4 5 6 7
2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9def sandglass(n):
# Upper half
for i in range(1, n + 1):
print(" " * (i - 1), end="")
for j in range(i, 2 * n - i + 1):
print(j, end=" ")
print()
# Lower half
for i in range(n - 1, 0, -1):
print(" " * (i - 1), end="")
for j in range(i, 2 * n - i + 1):
print(j, end=" ")
print()
sandglass(5)7. Wave Pattern
Print a sine wave using asterisks.
* *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * *def wave(n):
width = 2 * n + 1
for i in range(1, n + 1):
if i == n:
# Bottom row: peak points merged into a solid line
print("*" * (n + 1))
else:
line = [" "] * width
line[0] = "*" # left edge
line[2 * n] = "*" # right edge
if i > 1:
line[i] = "*" # descending diagonal
if (2 * n - i) != i:
line[2 * n - i] = "*" # ascending diagonal
print("".join(line).rstrip())
wave(8)8. Hollow Diamond Pattern
Print a hollow diamond (only edges).
*
* *
* *
* *
* *
* *
* *
* *
*def hollow_diamond(n):
# Upper half
for i in range(1, n + 1):
print(" " * (n - i), end="")
for j in range(1, 2 * i):
if j == 1 or j == 2 * i - 1:
print("*", end="")
else:
print(" ", end="")
print()
# Lower half
for i in range(n - 1, 0, -1):
print(" " * (n - i), end="")
for j in range(1, 2 * i):
if j == 1 or j == 2 * i - 1:
print("*", end="")
else:
print(" ", end="")
print()
hollow_diamond(5)9. Christmas Tree Pattern
Print a Christmas tree with trunk.
*
***
*****
*******
*********
***********
*************
|||
|||
|||def christmas_tree(n):
# Tree layers
for i in range(1, n + 1):
print(" " * (n - i) + "*" * (2 * i - 1))
# Trunk
trunk_width = 3
trunk_height = 3
for _ in range(trunk_height):
print(" " * (n - 2) + "|" * trunk_width)
christmas_tree(7)10. Number Snake Pattern
Print numbers in a snake-like sequence.
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9def number_snake(n):
for i in range(1, n + 1):
for j in range(i, i + n):
print(j, end=" ")
print()
number_snake(5)Conclusion
These advanced patterns will help you master Python loops and 2D arrays. Try combining them or creating your own variations. Each pattern teaches different algorithmic thinking - from mathematical sequences (Floyd’s, Pascal’s) to spatial reasoning (Spiral, ZigZag).
Happy coding!
