Monday, 21 September 2026

๐Ÿ Python Pattern Challenge — Day 9

 


๐Ÿ Python Pattern Challenge — Day 9

Pattern printing is a great way to strengthen your Python logic, loops, conditions, and problem-solving skills. Today’s challenge is a little different from the previous patterns. We’ll create a unique hourglass-style star pattern by changing the number of stars across different rows.

The key is to understand how the number of stars can decrease, increase, and repeat in a controlled sequence.

Today's Challenge

Write a Python program to print:



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


Solution 1 — Using a for Loop

rows = [5, 3, 1, 3, 5, 3, 1, 3, 5] for stars in rows: spaces = (5 - stars) // 2 print(" " * spaces + "* " * stars)






How it works:

The important part is the list:

[5, 3, 1, 3, 5, 3, 1, 3, 5]

It controls how many stars appear in each row.

The pattern follows:

5 → 3 → 1 → 3 → 5 → 3 → 1 → 3 → 5

Then:

spaces = (5 - stars) // 2


calculates how much indentation is required before printing the stars.


Solution 2 — Using Nested Loops

rows = [5, 3, 1, 3, 5, 3, 1, 3, 5] for stars in rows: spaces = (5 - stars) // 2 for _ in range(spaces): print(" ", end="") for _ in range(stars): print("*", end=" ") print()








How it works:

Here, nested loops separately control the two parts:

  • First loop → creates the leading spaces.
  • Second loop → prints the required number of *.
  • Outer loop → moves through the pattern sequence.

This makes the relationship between spaces, stars, and rows easier to understand.


Solution 3 — Using a Pattern Formula

Instead of manually writing every row, we can generate the sequence using a repeating pattern.

pattern = [5, 3, 1] for block in range(3): for stars in pattern: spaces = (5 - stars) // 2 print(" " * spaces + "* " * stars)




How it works:

The pattern:

[5, 3, 1]

is repeated three times.

The outer loop:

for block in range(3):

controls the number of repetitions.

This makes the code more structured and reusable.


⚡ Short & Clean Code

for s in [5,3,1,3,5,3,1,3,5]: print(" "*((5-s)//2) + "* "*s)



๐Ÿ”ฅ A single loop is enough to generate the complete pattern!


๐Ÿš€ Challenge Yourself

Can you modify this pattern:

  • Replace * with numbers?
  • Take the maximum width using input()?
  • Generate the sequence without manually writing the list?
  • Use a while loop?
  • Create a similar pattern using letters?
  • Solve it using the shortest possible Python code?

Drop your solution below! ๐Ÿ‘‡

Learn • Practice • Grow with CLCODING ๐Ÿ๐Ÿ’ป

Python Coding Challenge - Question with Answer (ID 210926)

 


Explanation:

๐ŸŸข Line 1: Set Creation
x = {1, True, 1.0, False, 0}

Here x is a set.

At first glance, it looks like there are 5 elements:

1
True
1.0
False
0

But Python treats some of these values as equal.

๐ŸŸก Line 2: 1 and True
1 == True

Output:

True

Python considers:

True == 1

So 1 and True represent the same set key.

๐Ÿ”ต Line 3: 1 and 1.0
1 == 1.0

Output:

True

Therefore:

1
True
1.0

all collapse into one set element.

๐ŸŸ  Line 4: False and 0

Similarly:

False == 0

Output:

True

So:

False
0

also collapse into one element.

๐Ÿง  Line 5: What Does the Set Actually Contain?

Instead of 5 distinct elements, Python effectively has only:

{1, False}

or an equivalent representation depending on insertion/representation details.

So there are only 2 unique elements.

๐Ÿ”ด Line 6: len(x)
print(len(x))

len() counts the number of unique elements in the set.

Therefore:

1 / True / 1.0 → one element
False / 0      → one element

✅ Final Output
2

Books: Mastering Pandas with Python

๐Ÿฆ‹ Python’s Neon Butterfly Universe

 



Code:

import turtle import math import time screen = turtle.Screen() screen.setup(800, 800) screen.bgcolor("#02030a") t = turtle.Turtle() t.hideturtle() t.speed(0) t.width(2) colors = [ "#ff006e", "#ff7b00", "#ffe600", "#00ff9d", "#00e5ff", "#4169ff", "#9b30ff" ] # ----------------------------- # Butterfly Curve # ----------------------------- def butterfly(scale, color, phase): t.color(color) points = 260 for i in range(points): theta = i * 2 * math.pi / points # Butterfly curve r = math.exp(math.sin(theta)) - 2 * math.cos(4 * theta) x = scale * r * math.sin(theta + phase) y = scale * r * math.cos(theta + phase) if i == 0: t.penup() t.goto(x, y) t.pendown() else: t.goto(x, y) screen.update() time.sleep(0.004) # ----------------------------- # Outer butterfly # ----------------------------- for i in range(7): butterfly( 85 + i * 12, colors[i], i * 0.035 ) time.sleep(0.08) # ----------------------------- # Inner butterfly # ----------------------------- for i in range(5): butterfly( 35 + i * 8, colors[(i + 2) % len(colors)], -i * 0.04 ) time.sleep(0.08) # ----------------------------- # Body # ----------------------------- t.color("#ffffff") t.width(5) t.penup() t.goto(0, -115) t.pendown() t.goto(0, 115) screen.update() time.sleep(0.3) # ----------------------------- # Antennae # ----------------------------- t.width(2) for side in [-1, 1]: t.penup() t.goto(0, 110) t.setheading(90 + side * 35) t.pendown() for _ in range(35): t.forward(3) t.left(side * 2) screen.update() time.sleep(0.01) # ----------------------------- # Glowing body # ----------------------------- for r in range(18, 2, -3): t.penup() t.goto(0, -r) t.dot( r, colors[r % len(colors)] ) screen.update() time.sleep(0.05) # ----------------------------- # Star particles # ----------------------------- for i in range(45): angle = i * 137.5 radius = 180 + (i % 5) * 22 x = radius * math.cos(math.radians(angle)) y = radius * math.sin(math.radians(angle)) t.penup() t.goto(x, y) t.dot( 2 + i % 3, colors[i % len(colors)] ) screen.update() time.sleep(0.025) turtle.done()























































Explanation:


1. Import Libraries
import turtle
import math
import time
turtle → Drawing.
math → Mathematical calculations.
time → Animation delays.

2. Create the Screen
screen = turtle.Screen()
screen.setup(800, 800)
screen.bgcolor("#02030a")
Creates the window.
Sets size to 800 × 800.
Adds a dark background.

3. Create the Turtle
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
t.width(2)
Creates the drawing turtle.
Hides the cursor.
Sets maximum speed.
Sets line width to 2.

4. Define Colors
colors = [...]
Stores bright neon colors.
Colors are reused for the butterfly.

5. Define Butterfly Function
def butterfly(scale, color, phase):
Creates a reusable butterfly-drawing function.
scale → Size.
color → Line color.
phase → Rotation/offset.

6. Set Drawing Properties
t.color(color)
points = 260
Sets the selected color.
Uses 260 points for a smooth curve.

7. Calculate the Angle
theta = i * 2 * math.pi / points
Generates an angle for each point.
Covers a complete circular cycle.

8. Calculate Butterfly Radius
r = math.exp(math.sin(theta)) - 2 * math.cos(4 * theta)
Uses the butterfly-curve formula.
Produces the wing-like shape.

9. Calculate Coordinates
x = scale * r * math.sin(theta + phase)
y = scale * r * math.cos(theta + phase)
Calculates the X and Y positions.
scale controls the size.
phase slightly rotates the curve.

10. Draw the Curve
if i == 0:
    t.penup()
    t.goto(x, y)
    t.pendown()
else:
    t.goto(x, y)
Moves to the first point without drawing.
Connects all remaining points.
Creates the butterfly outline.

11. Animate the Curve
screen.update()
time.sleep(0.004)
Updates the screen.
Adds a tiny delay for animation.

12. Draw Outer Butterflies
for i in range(7):
Creates 7 outer butterfly layers.
butterfly(85 + i * 12, colors[i], i * 0.035)
Gradually increases the size.
Changes colors.
Adds a small phase shift.

13. Draw Inner Butterflies
for i in range(5):
Creates 5 smaller inner layers.
butterfly(
    35 + i * 8,
    colors[(i + 2) % len(colors)],
    -i * 0.04
)
Creates smaller curves.
Cycles through colors.
Applies reverse phase rotation.

14. Draw Butterfly Body
t.color("#ffffff")
t.width(5)
Changes the body to white.
Makes it thicker.
t.penup()
t.goto(0, -115)
t.pendown()
t.goto(0, 115)
Starts at the bottom.
Draws a vertical body through the center.

15. Draw Antennae
t.width(2)

for side in [-1, 1]:
Makes thinner lines.
Draws both antennae.
t.goto(0, 110)
t.setheading(90 + side * 35)
Moves to the top of the body.
Sets the antenna direction.
for _ in range(35):
Creates each antenna using 35 small segments.
t.forward(3)
t.left(side * 2)
Moves forward.
Slightly bends the antenna.

16. Create Glowing Body
for r in range(18, 2, -3):
Creates multiple shrinking circles.
t.dot(r, colors[r % len(colors)])
Draws colorful dots.
Creates a glowing effect.

17. Create Star Particles
for i in range(45):
Creates 45 particles around the butterfly.
angle = i * 137.5
radius = 180 + (i % 5) * 22
Generates different particle angles and distances.
Creates a scattered pattern.

18. Calculate Particle Position
x = radius * math.cos(math.radians(angle))
y = radius * math.sin(math.radians(angle))
Converts polar coordinates into X/Y positions.

19. Draw Particles
t.goto(x, y)
t.dot(
    2 + i % 3,
    colors[i % len(colors)]
)
Moves to each particle position.
Draws small colorful dots with varying sizes.

20. Finish
turtle.done()
Keeps the Turtle window open.
Ends the animation.



Popular Posts

Categories

100 Python Programs for Beginner (119) AI (345) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) book (1) Books (359) Bootcamp (14) C (78) C# (12) C++ (83) cloud (1) Course (93) Coursera (305) Cybersecurity (36) data (10) Data Analysis (47) Data Analytics (31) data management (16) Data Science (433) Data Strucures (18) Deep Learning (220) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (9) Excel (24) Finance (13) flask (4) flutter (1) FPL (17) Generative AI (77) Git (13) Google (55) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (404) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (35) Python (1375) Python Coding Challenge (1245) Python Library (5) Python Mathematics (17) Python Mistakes (51) Python Pattern Challenge (8) Python Quiz (637) Python Tips (112) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (21) SQL (55) Udemy (22) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)