Editor

Yin Yang in Python Turtle

Beginner · Shapes · 41 lines · draws in about 12 seconds

Two half circles of half the size make the S-shaped curve, and filling the S together with the left half of the big circle makes the black side.

Yin Yang in Python Turtle: the finished drawing
import turtle
screen = turtle.Screen()
screen.bgcolor("teal")
screen.title("Yin and yang")
t = turtle.Turtle()
t.speed(4)
t.pensize(3)
R = 200 # radius of the big circle
# Start at the bottom of the big circle
t.penup()
t.goto(0, -R)
t.pendown()
# A big white circle
t.color("black", "white")
t.begin_fill()
t.circle(R)
t.end_fill()
# The black half: an S-shaped curve up the middle, then the left edge back down
t.color("black", "black")
t.begin_fill()
t.circle(R / 2, 180) # lower half of the S, bulging right
t.circle(-R / 2, 180) # upper half of the S, bulging left
t.left(180)
t.circle(R, 180) # down the left side of the big circle
t.end_fill()
# A white dot in the black part and a black dot in the white part
t.penup()
t.goto(0, -R / 2)
t.dot(R / 3, "white")
t.goto(0, R / 2)
t.dot(R / 3, "black")
t.hideturtle()
turtle.done()

How the code works

A big white circle

The turtle starts at the bottom of the circle, so circle(R) draws a circle of radius 200 around the centre of the screen. color("black", "white") gives it a black outline and a white fill.

# Start at the bottom of the big circle
t.penup()
t.goto(0, -R)
t.pendown()

# A big white circle
t.color("black", "white")
t.begin_fill()
t.circle(R)
t.end_fill()

The black half

circle(R / 2, 180) draws half of a small circle, curving left. With a negative radius, circle(-R / 2, 180) curves the other way and finishes the S at the top. left(180) turns round and circle(R, 180) follows the left side of the big circle back to the start, which closes the shape for the fill.

# The black half: an S-shaped curve up the middle, then the left edge back down
t.color("black", "black")
t.begin_fill()
t.circle(R / 2, 180)     # lower half of the S, bulging right
t.circle(-R / 2, 180)    # upper half of the S, bulging left
t.left(180)
t.circle(R, 180)         # down the left side of the big circle
t.end_fill()

Two dots

Each dot sits in the middle of one of the small half circles, half the radius below or above the centre. dot() draws a filled circle of the given size where the turtle is.

# A white dot in the black part and a black dot in the white part
t.penup()
t.goto(0, -R / 2)
t.dot(R / 3, "white")
t.goto(0, R / 2)
t.dot(R / 3, "black")

Make it yours

Change the code above and press Run. Some ideas:

  • Change R = 200 to R = 120 and the whole drawing shrinks, because every size is worked out from R.
  • Change t.color("black", "black") to t.color("black", "navy") and t.dot(R / 3, "black") to t.dot(R / 3, "navy") for a navy and white version.
  • Change screen.bgcolor("teal") to screen.bgcolor("gold") for a warm background.

What it uses

  • circle with an extent
  • negative radius
  • begin_fill and end_fill
  • dot

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