π Python Pattern Challenge — Day 13
Pattern printing is one of the best ways to improve your Python logic, loops, spacing control, and problem-solving skills. Today's challenge focuses on creating a beautiful Number Diamond Pattern, where the same number is repeated in each row, forming a symmetric diamond shape.
The pattern grows from 1 to 4 and then shrinks back to 1, making it a great exercise for understanding both increasing and decreasing sequences.
Today's Challenge
Write a Python program to print:
Best and cleanest code will be rewarded! π
Solution 1 — Using Nested Loops
n = 4 # Upper Half for i in range(1, n + 1): for j in range(n - i): print(" ", end="") for j in range(i): print(i, end=" ") print() # Lower Half for i in range(n - 1, 0, -1): for j in range(n - i): print(" ", end="") for j in range(i): print(i, end=" ")
How it works
The upper half prints:
1 2 2 3 3 3 4 4 4 4
while the lower half prints:
3 3 3 2 2 1
The first loop controls the spaces and the second loop prints the current number repeatedly.
Solution 2 — Using String Multiplication
n = 4 for i in range(1, n + 1): print(" " * (n - i) + (str(i) + " ") * i) for i in range(n - 1, 0, -1): print(" " * (n - i) + (str(i) + " ") * i)
How it works
" " * (n - i)creates the leading spaces.
(str(i) + " ") * iprints the number multiple times.
For example:
i = 3produces:
3 3 3Solution 3 — Using a Single Loop
n = 4 rows = [1, 2, 3, 4, 3, 2, 1] for i in rows: print(" " * (n - i) + (str(i) + " ") * i)
How it works
The list:
[1, 2, 3, 4, 3, 2, 1]already contains the exact sequence needed for the diamond.
Each value controls:
- Number printed
- Number of repetitions
- Indentation of the row
This makes the code simple and easy to understand.
⚡ Short & Clean Code
for i in [1,2,3,4,3,2,1]: print(" "*(4-i) + f"{i} "*i)
π₯ Just one loop generates the entire pattern.
π Challenge Yourself
Can you create:
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1
Or try:
- Replacing numbers with letters
- Taking n from user input
- Creating a hollow diamond version
- Printing only even numbers
- Solving it using a while loop
Drop your solution below! π
13 Days. 13 Patterns. Stronger Python Logic. ππ₯
Learn • Practice • Grow with CLCODING π

0 Comments:
Post a Comment