Rangoli in Python Turtle
Intermediate · Festivals · 71 lines · draws in about 10 seconds
This program draws a symmetric rangoli by repeating one petal function around the centre, layer by layer, with white chalk lines and dots.

How the code works
A petal from two quarter circles
The turtle walks out from the centre, turns 45 degrees right, draws a quarter circle, turns 90 degrees left and draws another quarter circle back. The two curves make a leaf shape pointing at angle.
def petal(distance, angle, length, color): """A leaf-shaped petal that starts `distance` from the centre and points at `angle`.""" t.penup() t.goto(0, 0) t.setheading(angle) t.forward(distance) t.right(45) t.pendown() t.color("white", color) t.begin_fill() radius = length / 1.414 # two quarter circles make a petal this long t.circle(radius, 90, steps=15) t.left(90) t.circle(radius, 90, steps=15) t.end_fill()
Dots in a ring
For each dot the turtle goes back to the centre, faces turn + number * 360 / count and walks out radius steps, so the dots are evenly spaced round the circle.
def ring_of_dots(radius, count, size, color, turn=0): """Dots spaced evenly round a circle, the first one at angle `turn`.""" t.penup() for number in range(count): t.goto(0, 0) t.setheading(turn + number * 360 / count) t.forward(radius) t.dot(size, color)
Sixteen petals in two colours
number % 2 is 0 for even numbers and 1 for odd ones, so pink and orange take turns. Each petal is turned 360 / 16 = 22.5 degrees from the last.
for number in range(16): if number % 2 == 0: color = "#ff3d7f" else: color = "#ff9f1c" petal(140, number * 360 / 16, 75, color)
Two layers of eight petals
Later shapes cover earlier ones, so the eight yellow petals go down first and the eight teal petals sit on top of them in the gaps, turned 22.5 degrees further round.
for number in range(8): petal(30, number * 45, 110, "#ffd60a") for number in range(8): petal(30, number * 45 + 22.5, 80, "#2ec4b6")
Make it yours
Change the code above and press Run. Some ideas:
- Change
range(16)torange(24)and360 / 16to360 / 24for a busier outer ring. - Change the yellow petal length from
110to130and watch them push into the outer ring. - Swap in your own colours, for example
"#ff3d7f"to"crimson"and"#2ec4b6"to"limegreen".
What it uses
- functions with parameters
- for loops
- the % operator
- circle with extent
Every turtle command the site supports is listed on Python turtle online.