Friday, 2 October 2026
๐ Python Pattern Challenge — Day 18
Python Developer October 02, 2026 Python Pattern Challenge No comments
๐ Python Pattern Challenge — Day 18
Pattern printing is a great way to improve your Python loops, spacing, repetition, and logical thinking. For Day 18, let's create a simple Star Hourglass Pattern ⭐.
This pattern starts with a wide row of stars, gradually becomes smaller toward the center, and then expands again. It is a good exercise for understanding increasing and decreasing loops.
Today's Challenge
Write a Python program to print:
Best and cleanest code will be rewarded! ๐
Solution 1 — Using Nested for Loops
n = 5 for i in range(n, 0, -1): print(" " * (n - i), end="") for j in range(2 * i - 1): print("*", end=" ") print() for i in range(2, n + 1): print(" " * (n - i), end="") for j in range(2 * i - 1): print("*", end=" ") print()
How it works
The first loop creates the decreasing part:
* * * * * * * * * * * * * * * * * * * * * * * * *
The second loop creates the increasing part:
* * * * * * * * * * * * * * * * * * * * * * * *
The expression:
2 * i - 1controls the number of stars in each row.
Solution 2 — Using String Multiplication
n = 5 for i in range(n, 0, -1): print(" " * (n - i) + "* " * (2 * i - 1)) for i in range(2, n + 1): print(" " * (n - i) + "* " * (2 * i - 1)) This version is shorter because Python can repeat a string using *. For example: "* " * 5 produces: * * * * *
Solution 3 — Using a Single Loop
n = 5 for i in list(range(n, 0, -1)) + list(range(2, n + 1)): print(" " * (n - i) + "* " * (2 * i - 1))
How it works
The sequence:
5, 4, 3, 2, 1, 2, 3, 4, 5
controls the complete hourglass.
When the value decreases, the pattern shrinks.
When it increases again, the pattern expands.
⚡ Short & Clean Code
for i in [5, 4, 3, 2, 1, 2, 3, 4, 5]: print(" " * (5-i) + "* " * (2*i-1))
๐ฅ A single loop is enough to create the complete pattern.
๐ Challenge Yourself
Can you modify this pattern:
- Take n from the user using input()?
- Create the same pattern using a while loop?
- Make the hourglass larger?
- Replace * with another symbol?
- Create a hollow hourglass?
- Generate the sequence without manually writing [5, 4, 3, ...]?
Drop your solution below! ๐
18 Days. 18 Patterns. Stronger Python Logic. ๐๐ฅ
Learn • Practice • Grow with CLCODING ๐
Books: PYTHON INTERVIEW QUESTIONS AND ANSWERS
Popular Posts
-
Are you fascinated by the possibilities of Artificial Intelligence but feel limited by your current coding skills? Do you dream of creatin...
-
Artificial Intelligence is revolutionizing scientific discovery, and one of its most exciting applications is molecular discovery . Tradit...
-
Python for Data Analysis: The Modern Guide to Scalable Data, Advanced Models, and Automation Download the PDF for free: Python for Data An...
-
September Data Science Bootcamp: Day 1 to Day 22 — From Python to a Final Project Learning Data Science is not just about learning Machine ...
-
๐ Python Pattern Challenge — Day 16 Pattern printing is a great way to improve your Python loops, conditions, spacing, and logical think...
-
GitHub is more than just a platform for storing code. It's a place to learn from experienced developers, explore open-source projects,...
-
Machine Learning Projects is a free practical resource focused on learning machine learning by building projects with Python. Rather than...
-
Are you looking for free resources to learn Python? Amazon is offering over 40 Python books for free, including audiobooks that can be ins...
-
Explanation: ๐ข Step 1: First List [1, 2] This list contains two integers: 1 → int 2 → int ๐ก Step 2: Second List [1, 2.0] This list conta...
-
Microsoft Python Development Professional Certificate: A Complete Guide for 2025 Python is the go-to language for web development, automat...
