0% found this document useful (0 votes)
3 views8 pages

Python Pattern Printing 30 Questions

The document presents 30 Python pattern printing questions along with their solutions, categorized into basic, intermediate, and advanced levels. Each pattern demonstrates different programming techniques, including loops, recursion, and matrix manipulation. The patterns range from simple shapes like squares and triangles to more complex structures like spirals and Sierpinski triangles.

Uploaded by

Sone Lal Patel
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

Python Pattern Printing 30 Questions

The document presents 30 Python pattern printing questions along with their solutions, categorized into basic, intermediate, and advanced levels. Each pattern demonstrates different programming techniques, including loops, recursion, and matrix manipulation. The patterns range from simple shapes like squares and triangles to more complex structures like spirals and Sierpinski triangles.

Uploaded by

Sone Lal Patel
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Pattern Printing - 30

Questions with Solutions

Python Pattern Printing — 30


Questions with Solutions

Basic
1. Square pattern of stars

n = 5
for i in range(n):
print("* " * n)

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

2. Left-aligned right triangle

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

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

3. Inverted right triangle

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

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

4. Right-aligned triangle

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

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

5. Number triangle (1 to n per row)


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

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

6. Same number repeated per row

n = 5
for i in range(1, n + 1):
print(str(i) * i)

1
22
333
4444
55555

7. Alphabet triangle

n = 5
for i in range(n):
print(chr(65 + i) * (i + 1))

A
BB
CCC
DDDD
EEEEE

8. Hollow square

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

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

Intermediate
9. Pyramid pattern

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

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

10. Inverted pyramid

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

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

11. Diamond pattern

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

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

12. Number pyramid with same number per row

n = 5
for i in range(1, n + 1):
print(" " * (n - i) + (str(i) + " ") * i)

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

13. Floyd’s Triangle

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

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

14. Pascal’s Triangle

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

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

15. Butterfly pattern

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

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

16. Hollow diamond

n = 5
for i in range(1, n + 1):
print(" " * (n - i) + "*" + " " * (i - 1) + ("*" if i > 1 else
""))
for i in range(n - 1, 0, -1):
print(" " * (n - i) + "*" + " " * (i - 1) + ("*" if i > 1 else
""))

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

17. Zigzag pattern

rows, cols = 3, 9
for i in range(rows):
for j in range(cols):
if (j % (rows * 2 - 2) == i) or (j % (rows * 2 - 2) == (rows
* 2 - 2) - i):
print("*", end="")
else:
print(" ", end="")
print()

* * *
* * * *
* *

18. Right triangle with alphabets (repeating alphabet per row,


not incrementing count)

import string
n = 5
letters = string.ascii_uppercase
for i in range(n):
print(" ".join(letters[:i + 1]))

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

19. Number pattern — decreasing count, decreasing value

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

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

20. Hourglass (number) pattern

n = 5
for i in range(n, 0, -1):
print(" " * (n - i) + str(i) * (2 * i - 1))
for i in range(2, n + 1):
print(" " * (n - i) + str(i) * (2 * i - 1))

55555
4444
333
22
1
22
333
4444
55555

Advanced
21. Spiral matrix printing (n x n)

n = 4
matrix = [[0] * n for _ in range(n)]
top, bottom, left, right = 0, n - 1, 0, n - 1
num = 1
while top <= bottom and left <= right:
for j in range(left, right + 1):
matrix[top][j] = num; num += 1
top += 1
for i in range(top, bottom + 1):
matrix[i][right] = num; num += 1
right -= 1
for j in range(right, left - 1, -1):
matrix[bottom][j] = 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(*row)

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

22. Diamond of numbers with a repeating cycle (1 to n and


back)

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

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

23. Binary triangle (each row alternates starting bit)

n = 5
for i in range(n):
row = []
start = 0 if i % 2 == 0 else 1
for j in range(i + 1):
[Link](str((start + j) % 2))
print(" ".join(row))

0
1 0
0 1 0
1 0 1 0
0 1 0 1 0

24. Diagonal matrix pattern

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

1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1

25. Checkerboard pattern

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

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

26. Rhombus pattern

n = 5
for i in range(n):
print(" " * (n - i - 1) + "* " * n)

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

27. Number pattern forming a plus/cross sign

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

*
*
* * * * *
*
*

28. Right-angled triangle using recursion (no explicit loop


counter reuse)

def print_triangle(n, row=1):


if row > n:
return
print("* " * row)
print_triangle(n, row + 1)

print_triangle(5)

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

29. Pascal’s Triangle using recursion for binomial coefficients

def factorial(n):
return 1 if n <= 1 else n * factorial(n - 1)

def combination(n, r):


return factorial(n) // (factorial(r) * factorial(n - r))

n = 5
for i in range(n):
print(" " * (n - i), end="")
for j in range(i + 1):
print(combination(i, j), end=" ")
print()
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

30. Sierpinski-style triangle pattern (fractal approximation


using bitwise AND)

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

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

You might also like