turtle.speed() function in Python

The turtle.speed() function in Python sets the turtle’s animation speed. An integer between 0 and 10. 0 is fastest (no animation), 10 is fast, 1 is slowest.

Syntax: turtle.speed(speed=None)
Parameters: speed – an integer in the range 0-10 or a speedstring like “fastest”, “fast”, “normal”, “slow”, “slowest”.

Let us see an example to implement the turtle.speed() function in Python:

Demo9.py

# turtle.speed() function in Python
# Code by Studyopedia

import turtle

window = turtle.Screen()
t = turtle.Turtle()

t.speed(1)      # Slowest speed
t.forward(100)
t.speed(10)     # Fast speed
t.left(90)
t.forward(100)

window.exitonclick()

The following is the output:

turtle.speed() in Python Turtle

In the above code, we followed the below steps:

  1. Import the module: import turtle loads Python’s turtle graphics library.
  2. Create the screen: window = turtle.Screen() opens the drawing window.
  3. Create the turtle: t = turtle.Turtle() makes the turtle (your pen).
  4. Set slow speed: t.speed(1) sets a very slow animation speed (1 is slowest from 1–10; 0 turns animation off).
  5. Draw first segment: t.forward(100) moves 100 units forward, drawing slowly.
  6. Speed up: t.speed(10) switches to fast animation.
  7. Turn left: t.left(90) rotates the turtle 90° counterclockwise without moving.
  8. Draw second segment: t.forward(100) draws another 100 units quickly due to the higher speed.
  9. Close on click: window.exitonclick() keeps the window open until you click inside it, then closes.

If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

turtle.dot() function in Python
turtle.undo() function in Python
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment