๐ Python Pattern Challenge — Day 31 | Hollow Diamond Star Pattern
Welcome to Day 31 of the Python Pattern Challenge! ๐
Today, we're creating a beautiful Hollow Diamond Star Pattern using Python. This pattern combines stars and spaces to form a diamond shape with an empty center.
It's a great way to practice nested loops, conditional statements, spacing, and logical thinking while creating an attractive design using just a few lines of Python code.
๐ฏ Today's Challenge
Write a Python program to print exactly this pattern:
๐ป Solution 1 — Using Nested for Loops
n = 4 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()
This code creates a filled diamond pattern. To make it hollow, we need to add conditions that print stars only along the boundary and spaces inside.
๐ง Solution 2 — Creating a Hollow Diamond
n = 5for i in range(-n + 1, n): for j in range(-n + 1, n): if abs(i) + abs(j) == n - 1: print("*", end=" ") else: print(" ", end=" ") print()
How It Works
1. Controlling the rows and columns
for i in range(-n + 1, n):The outer loop controls the rows, while the inner loop controls the positions within each row.
2. Checking the diamond boundary
if abs(i) + abs(j) == n - 1:The abs() function returns the absolute value of a number. This condition identifies positions along the diamond's boundary.
3. Printing stars and spaces
print("*", end=" ")Stars appear on the boundary, while spaces are printed inside the shape.
⚡ What You Learn Today
Using nested for loops
Applying conditional statements
Understanding the abs() function
Controlling star positions with mathematical logic
Creating symmetrical patterns using Python
๐ Challenge Yourself
Can you modify today's pattern?
Increase the diamond size.
Replace * with # or @.
Create a filled diamond.
Print the pattern using a while loop.
Take the size as user input.
Create a hollow square using similar logic.
Share your solution in the comments! ๐
31 Days. 31 Patterns. Stronger Python Logic. ๐๐ฅ
Learn • Practice • Grow with CLCODING ๐ป
.png)


0 Comments:
Post a Comment