Fundamentals 8 min read

Python Turtle Graphics: Commands, Examples, and Code Snippets

This tutorial introduces Python's standard turtle graphics library, explains its drawing principles, details core commands for window setup, movement, direction, and pen control, and provides ten illustrated code examples ranging from basic shapes to complex patterns.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python Turtle Graphics: Commands, Examples, and Code Snippets

The turtle library is a standard Python module for simple graphics; after import turtle you can control a virtual turtle that moves on a canvas, leaving a trail that forms the drawn image.

Drawing principle: the turtle starts at the center of the window; its movements (forward, backward, turns, etc.) create visible lines, and its appearance can be customized (color, pen width, fill).

Window setup command

turtle.setup(400, 300, 200, 100) – sets width, height, and the distance from the left and top edges of the screen; the origin is at the top‑left corner, units are pixels.

Movement commands

turtle.goto(x, y) – jumps directly to coordinates (x, y) with the window center as origin.

turtle.fd(d) or turtle.forward(d) – moves forward d pixels in the current heading.

turtle.bk(d) or turtle.backward(d) – moves backward d pixels.

turtle.circle(r, angle) – draws an arc of angle degrees with radius r .

Direction commands

turtle.seth(angle) – sets the heading to angle degrees (0° points to the right, positive is counter‑clockwise).

turtle.left(angle) – rotates left by angle degrees.

turtle.right(angle) – rotates right by angle degrees.

Pen control commands

turtle.penup() – lifts the pen (no drawing).

turtle.pendown() – puts the pen down (drawing).

turtle.pensize(width) – sets pen thickness.

turtle.pencolor(color) – sets pen color (name, RGB tuple, or hex code).

turtle.fillcolor(color) – sets fill color for shapes.

turtle.begin_fill() / turtle.end_fill() – starts and ends a fill region.

turtle.filling() – returns whether a fill is in progress.

Examples

1. Equilateral triangle (side 200 px):

<code>import turtle as t</code>
<code>for i in range(3):</code>
<code>    t.seth(i*120)</code>
<code>    t.fd(200)</code>

2. Square (side 200 px):

<code>import turtle</code>
<code>d = 0</code>
<code>for i in range(4):</code>
<code>    turtle.fd(200)</code>
<code>    d = d + 90</code>
<code>    turtle.seth(d)</code>

3. Rhombus (side 200 px):

<code>import turtle as t</code>
<code>t.right(-30)</code>
<code>for i in range(2):</code>
<code>    t.fd(200)</code>
<code>    t.right(60*(i+1))</code>
<code>for i in range(2):</code>
<code>    t.fd(200)</code>
<code>    t.right(60*(i+1))</code>

4. Hexagon (side 200 px):

<code>import turtle as t</code>
<code>for i in range(6):</code>
<code>    t.fd(200)</code>
<code>    t.left(60)</code>

5. Pentagon (side 200 px):

<code>import turtle</code>
<code>d = 0</code>
<code>for i in range(5):</code>
<code>    turtle.fd(200)</code>
<code>    d += 72</code>
<code>    turtle.seth(d)</code>

6. Four‑petal flower (radius 200 px, 90° arcs):

<code>import turtle as t</code>
<code>for i in range(4):</code>
<code>    t.seth(90*(i+1))</code>
<code>    t.circle(200,90)</code>
<code>    t.seth(-90 + i*90)</code>
<code>    t.circle(200,90)</code>

7. Four‑leaf clover:

<code>import turtle</code>
<code>for i in range(4):</code>
<code>    turtle.right(90)</code>
<code>    turtle.circle(50,180)</code>

8. Star (radius 90 px):

<code>import turtle</code>
<code>for i in range(4):</code>
<code>    turtle.circle(-90,90)</code>
<code>    turtle.right(180)</code>

9. Concentric circles (starting radius 10 px, step 40 px):

<code>import turtle</code>
<code>r = 10</code>
<code>head = 90</code>
<code>for i in range(4):</code>
<code>    turtle.seth(head)</code>
<code>    turtle.circle(r)</code>
<code>    r = r + 40</code>
<code>turtle.done()</code>

10. Another concentric‑circle pattern with pen up/down handling:

<code>import turtle</code>
<code>r = 10</code>
<code>dr = 40</code>
<code>head = 90</code>
<code>for i in range(4):</code>
<code>    turtle.pendown()</code>
<code>    turtle.circle(r)</code>
<code>    r += dr</code>
<code>    turtle.penup()</code>
<code>    turtle.seth(-head)</code>
<code>    turtle.fd(dr)</code>
<code>    turtle.seth(0)</code>
<code>turtle.done()</code>

The examples demonstrate that using the turtle library is straightforward, and with Python's simplicity these graphics can be created quickly after a brief introductory tutorial.

GraphicsPythoncode examplestutorialturtledrawing
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.