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
Python Coding Challenge - Question with Answer (ID 021026)
Python Coding challenge - Day 1271| What is the output of the following Python Code?
Python Developer October 02, 2026 Python Coding Challenge No comments
Code Explanation:
100 Python Programs for Beginner with explanation
Python Coding challenge - Day 1270| What is the output of the following Python Code?
Python Developer October 02, 2026 Python Coding Challenge No comments
Code Explanation:
Book: 400 Days Python Coding Challenges with Explanation
Popular Posts
-
What you'll learn Understand the cybersecurity landscape and learn core concepts foundational to security, compliance, and identity so...
-
Introduction Python has become one of the most popular programming languages because of its simple syntax, flexibility, and ability to sol...
-
Guide to NumPy: 2nd Edition – The Complete Reference for Scientific Computing and High-Performance Array Programming in Python Introductio...
-
Introduction Python is one of the most widely used programming languages, powering web development, automation, data analysis, artificial in...
-
Introduction Python is one of the most widely used programming languages in the world. Its readable syntax, flexible features, and extensi...
-
Learn Python: Basic to Advance: Practical & Theory – A Complete Guide to Building Python Skills Understand Python programming from the...
-
Introduction Artificial Intelligence (AI) and Machine Learning (ML) are transforming industries — but working with them effectively requir...
-
Python for Cybersecurity: Build 16 Real Security Tools and Become Job Ready – A Practical Guide to Cybersecurity Automation Explore how Py...
-
Foundations of Robotics: A Multidisciplinary Approach with Python and ROS – A Complete Guide to Modern Robotics Explore robotics through p...
-
Machine learning has rapidly evolved into one of the most influential fields in computer science, driving innovations in artificial intell...
