Getting Started with Python's Turtle Graphics
This article introduces Python's turtle module, explains how to import it, configure the pen's speed, color, size, and use drawing functions such as forward, left, right, circle, dot, and goto, and provides several complete code examples for creating simple graphics.
This tutorial demonstrates how to use Python's built-in turtle module for creating simple graphics. It begins by importing the module and creating a pen object, then shows how to set the pen's speed, color, and size.
import turtle t = turtle.Pen() t.speed(100) t.color("red") t.pensize(40) t.forward(100)
The article explains that the forward function moves the pen forward, while left and right rotate the drawing direction. Example rotation code:
t.right(45) # angle in degrees t.forward(100)
Additional drawing functions are covered: circle(radius) draws a circle, dot() creates a dot, and goto(x, y) moves the pen to a specific coordinate without drawing unless the pen is down. The up() and down() functions control whether movement leaves a trace.
Several complete code snippets illustrate these concepts. A more elaborate example creates a colorful pattern using loops, color fills, and multiple pen movements:
# coding=utf-8 import turtle import time turtle.color("red", "yellow") turtle.begin_fill() for _ in range(50): turtle.forward(200) turtle.left(170) turtle.end_fill() turtle.mainloop()
Another example defines a custom curve function and combines it with pen rotations to produce a complex shape:
import turtle p = turtle.Pen() p.speed(50) def curvemove(): for i in range(200): p.right(1) p.forward(1) p.color('red','pink') p.begin_fill() p.left(140) p.forward(111.65) curvemove() p.left(120) curvemove() p.forward(111.65) p.end_fill()
Throughout the guide, screenshots illustrate the resulting drawings, and a final note encourages readers to explore further by reading the original source.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.