Editor

Bangladesh Flag in Python Turtle

Beginner · Flags · 44 lines · draws in about 9 seconds

This program draws the flag of Bangladesh at its official 10:6 size, with the red disc placed a little towards the flagpole as on the real flag.

Bangladesh Flag in Python Turtle: the finished drawing
import turtle
# The flag is 10 units long and 6 units high
LENGTH = 540
HEIGHT = LENGTH * 6 / 10
GREEN = "#006A4E"
RED = "#F42A41"
screen = turtle.Screen()
screen.title("Flag of Bangladesh")
screen.bgcolor("#dde6ee")
t = turtle.Turtle()
t.speed(5)
# Bottle-green background
left = -LENGTH / 2
t.penup()
t.goto(left, HEIGHT / 2)
t.pendown()
t.color(GREEN)
t.begin_fill()
for side in range(2):
t.forward(LENGTH)
t.right(90)
t.forward(HEIGHT)
t.right(90)
t.end_fill()
# Red disc: its radius is 1/5 of the length, and its centre is
# 9/20 of the length from the left edge, a little towards the flagpole
radius = LENGTH / 5
centre_x = left + LENGTH * 9 / 20
t.penup()
t.goto(centre_x, -radius)
t.pendown()
t.color(RED)
t.begin_fill()
t.circle(radius, steps=90)
t.end_fill()
t.hideturtle()
turtle.done()

How the code works

The green field

The flag is 10 units long and 6 high, so HEIGHT is worked out from LENGTH. The loop draws along the top, down, back and up while the fill colours it green.

# Bottle-green background
left = -LENGTH / 2
t.penup()
t.goto(left, HEIGHT / 2)
t.pendown()
t.color(GREEN)
t.begin_fill()
for side in range(2):
    t.forward(LENGTH)
    t.right(90)
    t.forward(HEIGHT)
    t.right(90)
t.end_fill()

A disc that is not quite centred

The official design puts the disc's centre 9/20 of the length from the flagpole side instead of halfway, so it looks centred when the flag flies. Its radius is 1/5 of the length.

radius = LENGTH / 5
centre_x = left + LENGTH * 9 / 20
t.penup()
t.goto(centre_x, -radius)
t.pendown()
t.color(RED)
t.begin_fill()
t.circle(radius, steps=90)
t.end_fill()

Make it yours

Change the code above and press Run. Some ideas:

  • Change 9 / 20 to 1 / 2 and compare: the disc moves to the exact middle, which the real flag does not do.
  • Change LENGTH = 540 to LENGTH = 400: the height, the disc and its position all follow.
  • Add t.pencolor("white") and t.pensize(4) after t.color(RED) to give the disc a white edge.

What it uses

  • variables for sizes
  • circle with steps
  • begin_fill and end_fill

Every turtle command the site supports is listed on Python turtle online.