The turtle draws
Section 7: Drawing with the turtleLesson 7.1 · ⏱ 8 min
The turtle library has a turtle that draws as it walks. forward moves forward, and left and right turn by degrees.
import turtle
t = turtle.Turtle()
t.color("blue")
t.pensize(4)
for i in range(4):
t.forward(150)
t.left(90)
🐍 Open the code in Robo Python ↗💡 Tip
The angle = 360 ÷ the number of sides. 120 for a triangle, and 60 for a hexagon.
📌 Another example: a triangle
Three sides, and we turn 120 degrees each time.
import turtle
t = turtle.Turtle()
t.color("green")
t.pensize(4)
for i in range(3):
t.forward(150)
t.left(120)
🐍 Open the code in Robo Python ↗
✍️ Practise
Try the lesson's examples here: copy the code, change it, and click ▶ Run.
✍️ Practise here in the lesson✖ Close the labor open the lab in a new page ↗
🎯 Challenge yourself
Easy
A red square with sides of 120.
👀 Show the solution🙈 Hide the solution
import turtle
t = turtle.Turtle()
t.color("red")
for i in range(4):
t.forward(120)
t.left(90)
🐍 Open the code in Robo Python ↗
Result: a red square.
Medium
A hexagon: 6 sides, and a 60 turn.
👀 Show the solution🙈 Hide the solution
import turtle
t = turtle.Turtle()
t.color("blue")
for i in range(6):
t.forward(90)
t.left(60)
🐍 Open the code in Robo Python ↗
Result: a hexagon.
Hard
A circle with circle, and a square around it.
👀 Show the solution🙈 Hide the solution
import turtle
t = turtle.Turtle()
t.color("purple")
t.circle(60)
t.penup()
t.goto(-60, 0)
t.pendown()
for i in range(4):
t.forward(120)
t.left(90)
🐍 Open the code in Robo Python ↗
Result: a circle inside a square.

