Diwali Diya in Python Turtle
Advanced · Festivals · 78 lines · draws in about 11 seconds
This program draws a glowing clay diya for Diwali, with an oval bowl worked out using sine and cosine and a flame made of three teardrops.

How the code works
Ovals from points
Turtle has no oval command, so oval() works out points with math.cos() and math.sin(), stretching a circle to the width and height it is given. With start=180 and end=360 it draws only the bottom half, which becomes the bowl.
def oval(x, y, width, height, color, start=0, end=360): """Fill an oval centred at (x, y), or the part of it from angle start to angle end.""" points = [] for angle in range(start, end + 1, 5): a = math.radians(angle) points.append((x + width / 2 * math.cos(a), y + height / 2 * math.sin(a))) t.penup() t.goto(points[0]) t.pendown() t.color(color) t.begin_fill() for point in points: t.goto(point) t.end_fill()
A teardrop flame
A flame is a straight side down to a round bottom, circle() round the bottom, and a straight side back up to the tip. math.asin() finds the angle at which the straight sides meet the round part smoothly.
def flame(x, y, radius, height, color): """A teardrop: a round bottom centred at (x, y) and a point `height` above it.""" lean = math.degrees(math.asin(radius / height)) side = math.sqrt(height ** 2 - radius ** 2) t.penup() t.goto(x, y + height) t.setheading(270 + lean) t.pendown() t.color(color) t.begin_fill() t.forward(side) t.circle(-radius, 180 + 2 * lean) t.forward(side) t.end_fill()
A glow from big dots
Four dots, drawn from the biggest and darkest to the smallest and warmest, look like light spreading out from the flame.
for size, color in [(380, "#2a1236"), (300, "#3d1838"), (220, "#5a2234"), (150, "#7a2e2a")]: t.dot(size, color)
Three flames, one inside another
Drawing a smaller, paler flame on top of a bigger one gives the flame a hot, bright centre.
flame(0, -4, 34, 120, "darkorange") flame(0, -10, 23, 84, "gold") flame(0, -16, 12, 46, "lightyellow")
Make it yours
Change the code above and press Run. Some ideas:
- Add a fifth glow ring by putting
(450, "#22102f")at the front of the glow list. - Change
range(205, 340, 15)torange(205, 340, 30)for fewer gold dots, further apart. - Change
"darkorange"to"orangered"for a redder flame.
What it uses
- math.sin and math.cos
- default parameter values
- begin_fill and end_fill
- dot
Every turtle command the site supports is listed on Python turtle online.