Indian Flag in Python Turtle
Beginner · Flags · 75 lines · draws in about 14 seconds
This program draws the Indian tricolour at its official 3:2 size, with the three bands from one `rectangle()` function and the 24 spokes of the Ashoka Chakra from a loop that turns 15 degrees each time.

How the code works
One function for every band
rectangle() lifts the pen, goes to the top-left corner, then draws right, down, left and up while begin_fill() and end_fill() colour the inside. The flag calls it three times.
def rectangle(x, y, width, height, color): """Draw a filled rectangle whose top-left corner is at (x, y).""" t.penup() t.goto(x, y) t.setheading(0) t.pendown() t.color(color) t.begin_fill() for side in range(2): t.forward(width) t.right(90) t.forward(height) t.right(90) t.end_fill()
Three bands, one under the other
The flag is 540 wide and 360 high, so each band is 120 high. Starting at top = HEIGHT / 2 and stepping down one band at a time keeps the whole flag centred on (0, 0).
left = -WIDTH / 2 top = HEIGHT / 2 rectangle(left, top, WIDTH, BAND, SAFFRON) rectangle(left, top - BAND, WIDTH, BAND, "white") rectangle(left, top - 2 * BAND, WIDTH, BAND, GREEN)
A wheel with 24 spokes
The chakra is 3/4 as tall as the white band, so its radius is 3/8 of the band. Each spoke goes out from the centre and back, then the turtle turns 15 degrees, because 24 spokes share 360 degrees.
radius = BAND * 3 / 8 t.color(NAVY) t.pensize(3) t.penup() t.goto(0, -radius) t.pendown() t.circle(radius, steps=60) # 24 spokes from the centre, 360 / 24 = 15 degrees apart t.pensize(1) t.penup() t.goto(0, 0) t.pendown() for spoke in range(24): t.forward(radius) t.backward(radius) t.left(15)
Dots on the rim and the hub
Turning 7.5 degrees first puts each dot halfway between two spokes. dot() draws a filled circle where the turtle stands, so the hub is one bigger dot back in the middle.
# A small dot on the rim between each pair of spokes t.penup() t.left(7.5) for spoke in range(24): t.forward(radius - 3) t.dot(4) t.backward(radius - 3) t.left(15) # The hub in the middle t.dot(radius / 3)
Make it yours
Change the code above and press Run. Some ideas:
- Change
t.speed(7)tot.speed(1)to watch each of the 24 spokes being drawn. - Change
WIDTH = 540andHEIGHT = 360to450and300for a smaller flag: the bands and the wheel shrink with it because they are worked out fromHEIGHT. - In the spoke loop, change
range(24)torange(12)andt.left(15)tot.left(30)to see a wheel with 12 spokes.
What it uses
- functions with parameters
- begin_fill and end_fill
- for loops
- circle and dot
Every turtle command the site supports is listed on Python turtle online.