Kodewick
Start learning →

Python Turtle Yin Yang

One big half circle and two small ones bend into the S, then two small dots.

Python Turtle Yin Yang
The full code (32 lines)
import turtle

screen = turtle.Screen()
screen.bgcolor("#f3efe6")
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
r = 160

# the black half: a big half circle and two small ones make the S
t.penup()
t.goto(0, -r)
t.setheading(0)
t.pendown()
t.color("black")
t.begin_fill()
t.circle(r, 180)
t.circle(r / 2, 180)
t.circle(-r / 2, 180)
t.end_fill()

# the outline
t.setheading(0)
t.pensize(3)
t.circle(r)

# two dots: white in the black, black in the white
t.penup()
t.goto(0, r / 2)
t.dot(r / 3, "white")
t.goto(0, -r / 2)
t.dot(r / 3, "black")

How it is drawn

From the bottom (0, -r), facing right, circle(r, 180) goes up the right side to the top.

circle(r / 2, 180) bends left to the centre, circle(-r / 2, 180) (a negative radius bends right) back to the bottom. The three arcs enclose the dark half.

With a positive radius circle turns left, with a negative one right — that pair makes the S.

Last, t.dot(r / 3, color) puts a dot at each small circle's centre.

Questions

Why setheading(0) before the outline?

After the dark half the turtle faces left; circle(r) would then draw a circle below instead of around.

What does a negative radius do?

circle(50) goes round to the left, circle(-50) to the right: the same size, bent the other way.

Try another