Sunday, 13 September 2026

🐍 Python Pattern Challenge — Day 5

 

🐍 Python Pattern Challenge — Day 5

Pattern printing is a great way to strengthen your Python logic, loops, conditions, and problem-solving skills. Today’s challenge is a little different — instead of printing a completely filled pyramid, we’ll create a hollow pyramid using stars.

Today's Challenge

Write a Python program to print:

 





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


Solution 1 — Using a for Loop

n = 5 for i in range(n): spaces = " " * (n - i - 1) if i == 0: print(spaces + "*") elif i == n - 1: print("* " * (2 * n - 1)) else: print(spaces + "* " + " " * (2 * i - 1) + "*")





How it works:

  • " " * (n - i - 1) → creates the spaces before the stars.
  • The first row contains only one star.
  • The middle rows contain stars only at the two boundaries.
  • The last row contains all stars.
  • 2 * i - 1 controls the empty space inside the pyramid.

Solution 2 — Using Nested Loops

n = 5 for i in range(n): spaces = " " * (n - i - 1) if i == 0: print(spaces + "*") elif i == n - 1: print("* " * (2 * n - 1)) else: print(spaces + "* " + " " * (2 * i - 1) + "*")





How it works:

Here, the nested loops control the pattern step by step:

  • First loop → creates the left indentation.
  • Second loop → controls the pyramid width.
  • j == 0 → prints the left boundary star.
  • j == 2 * i → prints the right boundary star.
  • i == n - 1 → fills the entire bottom row.

This approach is useful for understanding how conditions can control individual characters inside a pattern.


Solution 3 — Using a Single for Loop

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






Here, each row is constructed dynamically using string multiplication and concatenation, without nested loops.


πŸš€ Challenge Yourself

Can you create the same pattern:

  • Using a while loop?
  • Without using nested loops?
  • By taking the number of rows using user input?
  • Make it a hollow diamond instead of a pyramid?
  • In the shortest possible Python code?

Drop your solution below! πŸ‘‡

Learn • Practice • Grow with CLCODING πŸπŸ’»


Bootcamp: 

https://whatsapp.com/channel/0029Va5BbiT9xVJXygonSX0G

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (355) Android (25) AngularJS (1) API (2) Assembly Language (2) aws (31) Azure (12) BI (10) book (1) Books (359) Bootcamp (19) C (78) C# (12) C++ (83) cloud (1) Course (96) Coursera (306) Cybersecurity (37) data (10) Data Analysis (48) Data Analytics (31) data management (16) Data Science (442) Data Strucures (21) Deep Learning (227) Django (17) Downloads (3) edx (21) Engineering (15) Euron (30) Events (9) Excel (24) Finance (13) flask (4) flutter (1) FPL (17) Gadgets (1) Generative AI (79) Git (14) Google (55) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (416) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (35) Python (1399) Python Bug (1) Python Coding Challenge (1293) Python Library (28) Python Mathematics (22) Python Mistakes (51) Python Pattern Challenge (35) python quixz (1) Python Quiz (673) Python Tips (114) Questions (3) R (72) React (7) Scripting (3) security (5) Selenium Webdriver (4) Software (21) SQL (56) Udemy (24) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)