Introduction
Turtle graphics is the graphical user interface python libraries. Turtle graphics is an awesome and simple way of introducing programming to the kids.
Turtle graphic can be used to make different shapes, figures, and pictures using the method python turtle on a virtual canvas. When a turtle program is executed it automatically creates a canvas or windows where the shape is drawn. Python’s Turtle Graphics module is an easy approach to introduce programming fundamentals and computational thinking.
It offers a simple sketching environment in which users may create drawings and designs by moving a virtual turtle around the screen with commands. The Turtle module is part of the Python standard library and does not require any further installations.
Using turtle, you can make many moving shapes and objects on the screen. Turtle graphics was developed in 1960s. It is still in used to help beginners to understand the programming. Turtle is not only for kids but also for adults. You can make your own games using turtle library in python. You can further read the turtle documentation here.
Purpose of turtle graphics
Turtle Graphics was created as part of the Logo programming language in the 1960s, with the goal of helping children learn programming ideas through visual representation. Python took this approach, including the Turtle module as an educational tool for teaching essential programming concepts like loops, functions, and conditionals in an appealing manner.
Basic Turtle Commands
- The Turtle module has various instructions for controlling the turtle:
- turtle. forward(distance): Advances the turtle by the specified distance.
- turtle. backward(distance): Moves the turtle backwards.
- turtle.right(angle) rotates the turtle clockwise by a specified angle.
- turtle.left(angle) rotates the turtle counterclockwise.
- Turtle.penup(): Lifts the pen so that movement does not result in drawing.
- turtle.pendown(): Lowers the pen and resumes drawing.
- turtle.color(“color”): Adjusts the turtle’s drawing color.
- turtle.speed(speed): Sets the turtle’s speed, with 1 being slow and 10 being fast.
How to use turtle library in python?
You can use it by importing the library.
from turtle import Turtle Or from turtle import *
You can give a turtle shape for your pointer using
turtle.shape(“turtle”)
Examples of turtle graphics. You can create the following.
Creating a dashed line
Import turtle as t Tin = t.Turtle() For _ in range(15): Tin.forward(10) Tin.penup() Tin.forward(10) Tin.pendown()
output:

Creating a square
from turtle import Turtle tim = Turtle() for _ in range(4): tim.forward(100) tim.left(90)
output:

Creating a random walk
import turtle as t import random tim = t.Turtle() colours = ["CornflowerBlue", "DarkOrchid", "IndianRed", "DeepSkyBlue", "LightSeaGreen", "wheat", "SlateGray", "SeaGreen"] directions = [0, 90, 180, 270] tim.pensize(10) tim.speed("fastest") for _ in range(200): tim.color(random.choice(colours)) tim.forward(30) tim.setheading(random.choice(directions))
output:
