Japan Flag in Python Turtle
Beginner · Flags · 38 lines · draws in about 10 seconds
This program draws the flag of Japan: a white 3:2 rectangle with a red disc in the exact centre whose diameter is 3/5 of the flag's height.

import turtle
# The flag is 3 units wide for every 2 units high
WIDTH = 540
HEIGHT = 360
screen = turtle.Screen()
screen.title("Flag of Japan")
screen.bgcolor("#dde6ee")
t = turtle.Turtle()
t.speed(5)
# White background of the flag
t.penup()
t.goto(-WIDTH / 2, HEIGHT / 2)
t.pendown()
t.color("white")
t.begin_fill()
for side in range(2):
t.forward(WIDTH)
t.right(90)
t.forward(HEIGHT)
t.right(90)
t.end_fill()
# Red sun in the exact centre: its diameter is 3/5 of the flag's height
radius = HEIGHT * 3 / 10
t.penup()
t.goto(0, -radius)
t.pendown()
t.color("#BC002D")
t.begin_fill()
t.circle(radius, steps=90)
t.end_fill()
t.hideturtle()
turtle.done()
How the code works
The white field
The pen goes to the top-left corner, then the loop draws a long side and a short side twice. The page behind is pale blue, so the white flag stands out.
# White background of the flag t.penup() t.goto(-WIDTH / 2, HEIGHT / 2) t.pendown() t.color("white") t.begin_fill() for side in range(2): t.forward(WIDTH) t.right(90) t.forward(HEIGHT) t.right(90) t.end_fill()
The red disc
The disc's diameter is 3/5 of the height, so its radius is 3/10. circle() turns around a centre radius steps to the turtle's left, so starting at (0, -radius) facing east puts the centre at (0, 0), and steps=90 keeps the edge smooth.
radius = HEIGHT * 3 / 10 t.penup() t.goto(0, -radius) t.pendown() t.color("#BC002D") t.begin_fill() t.circle(radius, steps=90) t.end_fill()
Make it yours
Change the code above and press Run. Some ideas:
- Change
steps=90tosteps=8to turn the sun into an octagon. - Change
radius = HEIGHT * 3 / 10toradius = HEIGHT * 4 / 10and compare how much bigger the sun looks. - Change
t.speed(5)tot.speed(1)and count the 90 short lines that make up the circle.
What it uses
- circle with steps
- begin_fill and end_fill
- for loops
Every turtle command the site supports is listed on Python turtle online.