Saturday, 19 September 2026

🐍 Python Pattern Challenge — Day 7

 


🐍 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 = 4


for 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 → 1

Solution 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, 1

Then 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

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (345) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) book (1) Books (359) Bootcamp (14) C (78) C# (12) C++ (83) cloud (1) Course (93) Coursera (305) Cybersecurity (36) data (10) Data Analysis (46) Data Analytics (31) data management (16) Data Science (433) Data Strucures (18) Deep Learning (220) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (9) Excel (24) Finance (13) flask (4) flutter (1) FPL (17) Generative AI (77) Git (13) Google (55) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (404) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (35) Python (1375) Python Coding Challenge (1242) Python Library (3) Python Mathematics (17) Python Mistakes (51) Python Pattern Challenge (6) Python Quiz (635) Python Tips (112) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (21) SQL (55) Udemy (22) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)