π Python Pattern Challenge — Day 7
Pattern printing is a great way to strengthen your Python logic, loops, conditions, and problem-solving skills. Today’s challenge takes things a step further by combining increasing and decreasing patterns with conditional star placement.
Instead of simply filling every row, you’ll need to carefully control where the stars appear and where spaces are placed.
Today's Challenge
Write a Python program to print:
Best and cleanest code will be rewarded! π
Solution 1 — Using for Loop
n = 4for i in range(1, n + 1): if i == 1: print(" " * (n - i) * 2 + "*") elif i == n: print("* " * (2 * i - 1)) else: print(" " * (n - i) * 2 + "* " + " " * (i - 2) + "*") for i in range(n - 1, 0, -1): if i == 1: print(" " * (n - i) * 2 + "*") elif i == n: print("* " * (2 * i - 1)) else: print(" " * (n - i) * 2 + "* " + " " * (i - 2) + "*")
How it works:
- " " * (n - i) * 2 → controls the indentation.
- The first row contains a single *.
- The middle row is completely filled.
- The other rows print stars only at the required positions.
- The second loop reverses the pattern to create the lower half.
The pattern therefore grows and then shrinks:
1 → 3 → 2 → 5 → 2 → 3 → 1Solution 2 — Using Nested Loops
n = 4 for i in range(1, n + 1): for j in range(n - i): print(" ", end="") for j in range(2 * i - 1): if i == 1 or i == n or j == 0 or j == 2 * i - 2: print("*", end=" ") else: print(" ", end=" ") print() for i in range(n - 1, 0, -1): for j in range(n - i): print(" ", end="") for j in range(2 * i - 1): if i == 1 or i == n or j == 0 or j == 2 * i - 2: print("*", end=" ") else: print(" ", end=" ") print()
How it works:
Here, nested loops control different parts of the pattern:
- First loop → controls the leading spaces.
- Second loop → controls the width of each row.
- j == 0 → prints the left boundary.
- j == 2 * i - 2 → prints the right boundary.
- i == n → creates the completely filled middle row.
This is a great exercise for understanding how conditions work inside nested loops.
Solution 3 — Using String Formatting
n = 4 for i in list(range(1, n + 1)) + list(range(n - 1, 0, -1)): spaces = " " * (n - i) if i == 1: print(spaces + "*") elif i == n: print("* " * (2 * i - 1)) else: print(spaces + "* " + " " * (i - 2) + "*")
How it works:
Instead of writing two separate loops, we create one increasing-and-decreasing sequence:
1, 2, 3, 4, 3, 2, 1Then each value determines the structure of that row.
This keeps the code compact and reusable.
⚡ Short & Clean Code
n = 4 for i in list(range(1, n + 1)) + list(range(n - 1, 0, -1)): if i in (1, n): print(" " * (n - i) + ("* " * (2 * i - 1)).rstrip()) else: print(" " * (n - i) + "* " + " " * (i - 2) + "*")
π₯ One main loop handles both the upper and lower portions of the pattern.
π Challenge Yourself
Can you modify this pattern:
- Create a perfect hollow diamond?
- Replace * with numbers?
- Use a while loop?
- Take the size using input()?
- Create the pattern using only one loop?
- Print the pattern using minimum possible code?
Drop your solution below! π
Learn • Practice • Grow with CLCODING ππ»

0 Comments:
Post a Comment