Wednesday, 9 September 2026

🐍 Python Pattern Challenge — Day 4

 



🐍 Python Pattern Challenge — Day 4

Pattern printing is one of the best ways to improve your Python logic, loops, string handling, and problem-solving skills.

For Day 4, let's take the challenge one step further and create a diamond-shaped star pattern. The real challenge is controlling both spaces and stars as the pattern grows and then shrinks.

Today's Challenge

Write a Python program to print:

  

Best and cleanest code will be rewarded! πŸ†


Solution 1 — Using Two for Loops

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




How it works:

  • " " * (n - i) → creates the indentation before the stars.
  • "* " * (2 * i - 1) → creates an increasing number of stars.
  • The first loop creates the upper half.
  • The second loop creates the lower half.
  • 2 * i - 1 generates odd numbers: 1, 3, 5, 7, 9.

Solution 2 — Using Nested Loops

n = 5 for i in range(1, n + 1): for j in range(n - i): print(" ", end=" ") for j in range(2 * i - 1): 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): print("*", end=" ") print()











How it works:

Here, nested loops separately control:

  • Spaces → position of the stars.
  • Stars → number of stars in each row.
  • The first outer loop builds the diamond upward.
  • The second outer loop builds it downward.

This approach is especially useful for beginners because you can clearly see how rows, spaces, and stars are controlled independently.


Solution 3 — Using String Multiplication

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




How it works:

Instead of writing two separate loops, we create the complete sequence of rows:

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

Then:

" " * (n - i)

controls the indentation, while:

"* " * (2 * i - 1)

controls the stars.

This makes the solution short, clean, and reusable.


πŸš€ Challenge Yourself

Can you create the same diamond pattern:

  • Using a while loop?
  • Using only one loop?
  • Without using nested loops?
  • By taking the size n as user input?
  • In the shortest possible Python 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 (343) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) book (1) Books (356) Bootcamp (14) C (78) C# (12) C++ (83) cloud (1) Course (90) Coursera (303) Cybersecurity (36) data (10) Data Analysis (46) Data Analytics (31) data management (16) Data Science (429) 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 (400) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (35) Python (1368) Python Coding Challenge (1234) Python Library (1) Python Mathematics (17) Python Mistakes (51) Python Pattern Challenge (2) Python Quiz (625) Python Tips (111) pythonquiz (1) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (21) SQL (55) Udemy (20) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)