Chapter 12 – Coding With Python Turtle
Click here to Download file
Python Turtle is a beginner‑friendly graphics library that helps students learn coding concepts like loops, angles, and coordinates by drawing shapes on the screen using simple commands.
Python is a high‑level, interpreted language that executes programs line by line, which makes it easier for beginners to find and correct errors.
- Install Python from the official website and open IDLE or any Python editor.
- The
turtlemodule comes pre‑installed with standard Python. - Programs are saved as
.pyfiles and can be run repeatedly.
print("Welcome to Python")
print("Python runs line by line!")
A Python program is usually written in a file so that it can be saved, edited, and executed multiple times.
- Open IDLE → choose File → New File.
- Type your Python or Turtle code in the new editor window.
- Save with File → Save As using
.py(for example,turtle_square.py). - Run the program using Run → Run Module or by pressing F5.
The Turtle module uses a virtual “turtle” like a pen that moves on the screen and draws lines according to Python commands.
- Import the module, create a turtle object, and then give it movement commands.
- The main window is called the screen; the turtle draws inside this window.
import turtle screen = turtle.Screen() # create a window t = turtle.Turtle() # create a turtle object t.forward(100) # move forward 100 units t.right(90) # turn right 90 degrees screen.mainloop() # keep the window open
A square has four equal sides and four right angles of 90 degrees, so the turtle repeats the same steps four times.
- Move forward to draw one side of the square.
- Turn left or right by 90 degrees after each side.
- Use a loop to reduce repetition in the program.
import turtle
t = turtle.Turtle()
for _ in range(4): # repeat 4 times
t.forward(100)
t.right(90)
turtle.done()
Turtle can fill any closed shape (like a square or circle) with a solid color using a start and end fill command.
- Choose a fill color with
t.fillcolor(). - Use
t.begin_fill()before drawing the shape andt.end_fill()after completing it.
import turtle
t = turtle.Turtle()
t.fillcolor("cyan") # set fill color
t.begin_fill()
for _ in range(4):
t.forward(120)
t.right(90)
t.end_fill()
turtle.done()
The speed of the turtle can be controlled to show drawings slowly or quickly, which is useful for demonstrations.
- Use
t.speed(n)wherenmay be 1 (slow) to 10 (fast), or keywords like"slowest"and"fastest". - Slower speed is helpful for teaching steps; faster speed gives almost instant drawings.
import turtle t = turtle.Turtle() t.speed(1) # slow t.forward(100) t.speed(10) # very fast t.right(90) t.forward(100) turtle.done()
The pen size controls how thick or thin the lines appear on the screen, which helps to emphasize important shapes.
- Use
t.pensize(width)to change the pen thickness. - Thin lines are used for normal shapes; thick lines are used for borders or designs.
import turtle t = turtle.Turtle() t.pensize(2) t.forward(80) t.penup() t.goto(0, -30) t.pendown() t.pensize(6) t.forward(80) turtle.done()
A circle in Turtle is drawn using a fixed radius, and the turtle moves around the center in a curved path.
- Use
t.circle(radius)to draw a circle. - A larger radius produces a bigger circle; a smaller radius produces a smaller circle.
import turtle t = turtle.Turtle() t.circle(80) # circle with radius 80 turtle.done()
Customized circles allow you to draw arcs or combine circles to make patterns and creative designs.
- Use
t.circle(radius, extent)whereextentis the angle (like 90°, 180°) to draw part of a circle. - Repeat circles at different angles to form flowers or circular designs.
import turtle
t = turtle.Turtle()
t.speed(0)
for _ in range(6):
t.circle(80, 180) # half circle
t.right(60) # turn for next petal
turtle.done()
The direction of the turtle controls which way it moves on the screen and can be changed without drawing if the pen is lifted.
t.left(angle)andt.right(angle)rotate the turtle by a given number of degrees.t.setheading(angle)sets the turtle to an exact direction (0° = east, 90° = north, etc.).
import turtle t = turtle.Turtle() t.setheading(0) # face east t.forward(80) t.setheading(90) # face north t.forward(80) turtle.done()
Sometimes you need to erase the drawing but keep the turtle active in the same window.
t.clear()removes all drawings from the screen.- The turtle’s position and settings remain, so you can start a new drawing immediately.
import turtle t = turtle.Turtle() t.forward(100) t.right(90) t.forward(50) t.clear() # remove drawing t.left(45) t.forward(80) turtle.done()
Resetting brings the turtle and the screen back to the original starting state.
t.reset()clears the screen and moves the turtle back to the center.- It also restores the default direction, color, and speed of the turtle.
import turtle t = turtle.Turtle() t.circle(60) t.reset() # clear and reset turtle t.forward(100) turtle.done()
Stamping leaves an impression of the turtle’s shape at its current position, while the real turtle can continue moving.
- Use
t.stamp()to create a copy mark of the turtle on the screen. - Multiple stamps can create trails or patterns without drawing lines between them.
import turtle
t = turtle.Turtle()
t.shape("turtle")
for _ in range(6):
t.stamp() # leave a turtle stamp
t.forward(40)
turtle.done()
Cloning creates a new turtle with the same properties as the original, so several turtles can draw at the same time.
- Use
t.clone()to make a second turtle object. - Each turtle can have its own color, speed, and direction, which helps in drawing complex or symmetric designs.
import turtle
t1 = turtle.Turtle()
t1.color("red")
t2 = t1.clone() # clone of t1
t2.color("blue")
t1.forward(100)
t2.right(90)
t2.forward(100)
turtle.done()

0 Comments