๐ Python Pattern Challenge — Day 44 | Geometric Square & Diagonal Star Pattern
Welcome to Day 44 of the Python Pattern Challenge! ๐
Today, we're creating an interesting Geometric Square & Diagonal Star Pattern using Python. This pattern combines diagonal lines with a hollow square structure to create a unique geometric design.
At first glance, it may look complicated, but it can be created using simple loops and conditional statements.
This challenge will help you improve your understanding of nested loops, coordinates, conditions, and logical thinking.
๐ฏ Today's Challenge
Write a Python program to print exactly the following pattern:
๐ป Python Solution
n = 5
for i in range(n):
print(" " * i + "* " + " " * (2 * (n - i - 1)) + "*")
for i in range(n):
if i == 0 or i == n - 1:
print(" " + "* " * n)
else:
print(" *" + " " * (2 * n - 3) + "*")
for i in range(n - 1, -1, -1):
print(" " * i + "* " + " " * (2 * (n - i - 1)) + "*")
๐ง How It Works
1. Creating the diagonal lines
The first loop prints stars diagonally, gradually moving them toward the center.
2. Managing the spacing
" " * iThis adds spaces before each star to control its horizontal position.
3. Creating the square structure
The second loop prints the upper and lower boundaries of the square using repeated stars.
4. Creating the hollow section
" " * (2 * n - 3)This generates the empty space between the two boundary stars.
5. Completing the diagonal design
The final loop runs in reverse order, creating the lower diagonal lines and completing the geometric pattern.
๐ Challenge Yourself
Increase the pattern size.
Replace stars with numbers.
Create a hollow diamond inside a square.
Try implementing the design using nested loops.
Experiment with different geometric shapes.
๐ก Key Takeaway
Pattern printing is an excellent way to develop programming logic. By combining loops, conditions, and spacing, you can create interesting geometric designs with just a few lines of Python code.



0 Comments:
Post a Comment