A comprehensive guide to understanding and creating different pattern-printing programs in Python. Perfect for beginners who want to strengthen their logic-building and looping concepts.
| S.No. | Pattern Name | Problem Link | Solution Link |
|---|---|---|---|
| 1 | Square Star Pattern | Problem | Solution |
| 2 | Left Triangle Pattern | Problem | Solution |
| 3 | Right Triangle Pattern | Problem | Solution |
| 4 | Number Triangle Pattern | Problem | Solution |
| 5 | Same Number Triangle Pattern | Problem | Solution |
| 6 | Inverted Number Pattern | Problem | Solution |
| 7 | Number Sequence Pattern | Problem | Solution |
| 8 | Pyramid Pattern | Problem | Solution |
| 9 | Inverted Pyramid | Problem | Solution |
| 10 | Diamond Pattern | Problem | Solution |
| 11 | Number Pyramid with Dashes | Problem | Solution |
| 12 | Reverse Number Pyramid | Problem | Solution |
| 13 | Hollow Square Pattern | Problem | Solution |
| 14 | Hollow Rectangle Pattern | Problem | Solution |
| 15 | Hollow Diamond Pattern | Problem | Solution |
| 16 | Butterfly Pattern | Problem | Solution |
| 17 | Number Pattern with Dashes | Problem | Solution |
| 18 | Complex Number Pattern | Problem | Solution |
| 19 | Inverted Triangle Pattern | Problem | Solution |
| 20 | Combined Triangle Pattern | Problem | Solution |
- Basics of loops for pattern printing
- Star (
*) patterns - Number patterns
- Alphabet patterns
- Pyramid patterns
- Hollow patterns
- Advanced patterns
- Common mistakes and how to fix them
- Practice exercises
*****
*****
*****
*****
*****
Solution:
rows = 5
cols = 5
for i in range(rows):
for j in range(cols):
print("*", end="")
print()*
**
***
****
*****
Solution:
rows = 5
for i in range(rows):
for j in range(i + 1):
print("*", end="")
print() *
**
***
****
*****
Solution:
rows = 5
for i in range(rows):
print(" " * (rows - i - 1) + "*" * (i + 1))1
12
123
1234
12345
Solution:
rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end="")
print()1
22
333
4444
55555
Solution:
for i in range(5):
for j in range(i + 1):
print(i + 1, end="")
print()12345
1234
123
12
1
Solution:
for i in range(5):
for j in range(1, 6 - i):
print(j, end="")
print()1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Solution:
num = 0
for i in range(1, 6):
for j in range(i):
num += 1
print(f"{num} ", end="")
print() *
***
*****
*******
*********
Solution:
rows = 5
for i in range(rows):
print(" " * (rows - i - 1) + "*" * (2 * i + 1))*********
*******
*****
***
*
Solution:
rows = 5
for i in range(rows, 0, -1):
print(" " * (rows - i) + "*" * (2 * i - 1)) *
***
*****
*******
*********
*******
*****
***
*
Solution:
# Upper part
for i in range(5):
for j in range(5 - i):
print(" ", end="")
for k in range(i + 1):
print("*", end="")
for m in range(i):
print("*", end="")
print()
# Lower part
for i in range(4):
for j in range(i + 2):
print(" ", end="")
for k in range(4 - i):
print("*", end="")
for m in range(3 - i):
print("*", end="")
print()----1
---12
--123
-1234
12345
Solution:
for k in range(1, 6):
for m in range(5 - k):
print("-", end="")
for n in range(1, k + 1):
print(n, end="")
print()----1
---21
--321
-4321
54321
Solution:
for k in range(1, 6):
for m in range(5 - k):
print("-", end="")
for n in range(k, 0, -1):
print(n, end="")
print()*****
* *
* *
* *
*****
Solution:
rows = 5
cols = 5
for i in range(rows):
for j in range(cols):
if i == 0 or i == rows - 1 or j == 0 or j == cols - 1:
print("*", end="")
else:
print(" ", end="")
print()****
* *
* *
****
Solution:
for i in range(1, 5):
for j in range(1, 5):
if 2 <= i <= 3 and 2 <= j <= 3:
print(" ", end="")
else:
print("*", end="")
print()**********
**** ****
*** ***
** **
* *
* *
** **
*** ***
**** ****
**********
Solution:
# Upper Part
for i in range(5):
for j in range(5 - i):
print("*", end="")
for k in range(i):
print(" ", end="")
for m in range(i):
print(" ", end="")
for n in range(5 - i):
print("*", end="")
print("")
# Lower Part
for i in range(1, 6):
for j in range(i):
print("*", end="")
for k in range(5 - i):
print(" ", end="")
for m in range(5 - i):
print(" ", end="")
for n in range(i):
print("*", end="")
print("")* *
** **
*** ***
**** ****
**********
**** ****
*** ***
** **
* *
Solution:
# Upper Part
for i in range(1, 6):
for j in range(i):
print("*", end="")
for k in range(5 - i):
print(" ", end="")
for m in range(5 - i):
print(" ", end="")
for n in range(i):
print("*", end="")
print()
# Lower Part
for i in range(4):
for j in range(4 - i):
print("*", end="")
for k in range(i + 1):
print(" ", end="")
for m in range(i + 1):
print(" ", end="")
for n in range(4 - i):
print("*", end="")
print()1------1
12----21
123--321
12344321
Solution:
for i in range(1, 5):
for j in range(1, i + 1):
print(j, end="")
print("-" * (4 * 2 - i * 2), end="")
for k in range(i, 0, -1):
print(k, end="")
print()4 4 4 4 4 4 4
4 3 3 3 3 3 4
4 3 2 2 2 3 4
4 3 2 1 2 3 4
4 3 2 2 2 3 4
4 3 3 3 3 3 4
4 4 4 4 4 4 4
Solution:
for i in range(1, 8):
for j in range(1, 8):
if (i == 2 and 2 <= j <= 6) or (i == 6 and 2 <= j <= 6) or (3 <= i <= 5 and j == 2) or (3 <= i <= 5 and j == 6):
print("3 ", end="")
elif (j == 3 and 3 <= i <= 5) or (j == 5 and 3 <= i <= 5) or (i == 3 and 3 <= j <= 5) or (i == 5 and 3 <= j <= 5):
print("2 ", end="")
elif i == 4 and j == 4:
print("1 ", end="")
else:
print("4 ", end="")
print()*****
****
***
**
*
Solution:
for i in range(5):
for j in range(5 - i):
print("*", end="")
print()*
**
***
****
*****
****
***
**
*
Solution:
for i in range(5):
for j in range(i + 1):
print("*", end="")
print()
for i in range(5):
for j in range(4 - i):
print("*", end="")
print()This prints each symbol on a new line.
Python is indentation-sensitive β wrong spaces break your program.
Examples:
range(5)β 0 to 4range(1,5)β 1 to 4range(5,0,-1)β 5 to 1
Always test edge cases for your loops.
Try to code these yourself:
- Hollow pyramid
- Pascal's triangle
- Hourglass pattern
- Floyd's triangle
- Spiral pattern
- Zigzag pattern
- Heart pattern
- Arrow patterns
- Start simple: Begin with basic patterns before moving to complex ones
- Understand the logic: Don't just copy code - understand the pattern structure
- Use variables: Make your code flexible by using variables for rows/columns
- Debug step by step: Print intermediate values to understand what's happening
- Practice regularly: Pattern printing improves with consistent practice
Happy Coding! π