0% found this document useful (0 votes)
2 views3 pages

23 - Python Graphics - Turtle

Uploaded by

Ashu Ashu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

23 - Python Graphics - Turtle

Uploaded by

Ashu Ashu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

The turtle library in Python is a popular and easy-to-use graphical module used

to create drawings and simple animations. It is especially useful for beginners to


learn programming concepts and computer graphics. The "turtle" moves around
the screen based on commands you give it, like drawing lines, turning, and filling
shapes.
Basic Concepts of the Turtle Library
 Turtle Object: The main drawing tool in the turtle library, which can move
around the screen, draw lines, and change its appearance.
 Window: The drawing canvas where the turtle moves around.
 Commands: Functions that control the turtle's behavior, such as moving,
rotating, and changing colors.
Setting Up the Turtle Graphics Window
Before using turtle graphics, you must import the turtle module.

import turtle

Creating a Turtle Object


To use the turtle, you need to create a Turtle() object. You can give this object a
name (e.g., t = [Link]()).

import turtle
t = [Link]()

Basic Functions and Methods in the Turtle Library


1. forward(distance)
Moves the turtle forward by the specified distance in pixels.
 Syntax: [Link](distance)
2. backward(distance)
Moves the turtle backward by the specified distance in pixels.
 Syntax: [Link](distance)
3. left(angle)
Turns the turtle left by the specified angle (in degrees).
 Syntax: [Link](angle)
4. right(angle)
Turns the turtle right by the specified angle (in degrees).
 Syntax: [Link](angle)
5. penup()
Lifts the pen so that the turtle moves without drawing.
 Syntax: [Link]()
6. pendown()
Puts the pen down, allowing the turtle to draw.
 Syntax: [Link]()
7. color(color)
Changes the turtle's color. You can specify the color name (e.g., 'red') or a hex
value (e.g., '#ff0000').
 Syntax: [Link](color)
8. begin_fill()
Starts filling the shape that the turtle is about to draw.
 Syntax: t.begin_fill()

t.begin_fill()
[Link](50)
t.end_fill()

9. end_fill()
Stops filling the shape with color.
 Syntax: t.end_fill()
10. circle(radius, extent=None, steps=None)
Draws a circle with a specified radius. You can also specify how much of the circle
to draw using the extent (angle) and steps (number of steps to approximate the
circle).
 Syntax: [Link](radius, extent=None, steps=None)
11. setposition(x, y) or goto(x, y)
Moves the turtle to a specified position on the screen without drawing.
 Syntax: [Link](x, y)
12. setheading(angle)
Sets the turtle's orientation (heading) to a specified angle.
 Syntax: [Link](angle)
13. speed(speed)
Controls the turtle's drawing speed. Speed values can be an integer from 0
(fastest) to 10 (slowest), or strings like 'fastest', 'fast', 'normal', 'slow', 'slowest'.
 Syntax: [Link](speed)
14. reset()
Resets the turtle's position, heading, and clears the screen.
 Syntax: [Link]()
15. clear()
Clears the drawing, but the turtle stays in its current position.
 Syntax: [Link]()
16. exitonclick()
Exits the Turtle graphics window when clicked.
 Syntax: [Link]()

You might also like