Getting to Know the Python Turtle 🐢 Library
Turtle is a pre-installed Python library that enables users to create pictures and shapes by providing them with a virtual canvas. The onscreen pen that you use for drawing is called the turtle and this is what gives the library its name. In short, the Python turtle library helps new programmers get a feel for what programming with Python is like in a fun and interactive way.
turtle is mainly used to introduce children to the world of computers. It’s a straightforward yet versatile way to understand the concepts of Python. This makes it a great avenue for kids to take their first steps in Python programming. That being said, the Python turtle library is not restricted to little ones alone! It’s also proved extremely useful for adults who are trying their hands at Python, which makes it great for Python beginners.
With the Python turtle library, you can draw and create various types of shapes and images. Here’s a sample of the kinds of drawings you can make with turtle:
Some turtle method
Turtle(): It creates and returns a new turtle object
forward(): It moves the turtle forward by the specified amount
backward(): It moves the turtle backward by the specified amount
right(): It turns the turtle clockwise
left(): It turns the turtle counter clockwise
penup(): It picks up the turtle’s Pen
pendown(): Puts down the turtle’s Pen
up(): Picks up the turtle’s Pen
down(): Puts down the turtle’s Pen
color(): name Changes the color of the turtle’s pen
fillcolor(): name Changes the color of the turtle will use to fill a polygon
heading(): It returns the current heading
position(): It returns the current position
goto() x, y: It moves the turtle to position x,y
begin_fill(): Remember the starting point for a filled polygon
end_fill(): It closes the polygon and fills with the current fill color
dot(): Leaves the dot at the current position
stamp(): Leaves an impression of a turtle shape at the current location
shape() shapename Should be ‘arrow’, ‘classic’, ‘turtle’ or ‘circle’
Example code
# import turtle librar
import turtle
my_window = turtle.Screen()
my_window.bgcolor("blue") # creates a graphics windo
my_pen = turtle.Turtle()
my_pen.forward(150)
my_pen.left(90)
my_pen.forward(75
my_pen.color("white"
my_pen.pensize(12
Outpu
Draw a Squa
Example cod
# import turtle librar
import turtle
my_pen = turtle.Turtle()
for i in range(4)
my_pen.forward(50)
my_pen.right(90)
turtle.done(
Outpu
Draw a st
Example cod
# import turtle librar
import turtle
my_pen = turtle.Turtle()
for i in range(50)
my_pen.forward(50)
my_pen.right(144)
turtle.done(
Outpu
Draw a Hexag
Example cod
# import turtle librar
import turtle
polygon = turtle.Turtle(
my_num_sides =
my_side_length = 7
my_angle = 360.0 / my_num_side
for i in range(my_num_sides)
polygon.forward(my_side_length)
polygon.right(my_angle)
turtle.done()
Draw a square inside another square bo
Example code
# import turtle library
import turtle
my_wn = turtle.Screen(
my_wn.bgcolor("light blue")
my_wn.title("Turtle")
my_pen = turtle.Turtle()
my_pen.color("black")
def my_sqrfunc(size)
for i in range(4)
my_pen.fd(size)
my_pen.left(90)
size = size - 1
my_sqrfunc(146
my_sqrfunc(126)
my_sqrfunc(106)
my_sqrfunc(86)
my_sqrfunc(66)
my_sqrfunc(46)
my_sqrfunc(26)
Drawing of another pattern
Example code
# import turtle library
import turtle
my_wn = turtle.Screen()
turtle.speed(2)
for i in range(30)
turtle.circle(5*i)
turtle.circle(-5*i)
turtle.left(i)
turtle.exitonclick(
Drawing of another pattern
Example code
# import turtle librar
import turtle
colors = [ "red","purple","blue","green","orange","yellow")
my_pen = turtle.Pen()
turtle.bgcolor("black")
for x in range(360)
my_pen.pencolor(colors[x % 6]
my_pen.width(x/100 + 1)
my_pen.forward(x)
my_pen.left(59)
Example code
# import turtle library
import turtle
my_window = turtle.Screen()
my_window.bgcolor("blue") # creates a graphics windo
my_pen = turtle.Turtle()
my_pen.forward(150)
my_pen.left(90)
my_pen.forward(75)
my_pen.color("white")
my_pen.pensize(12)
Draw a Square
Example code
# import turtle library
import turtle
my_pen = turtle.Turtle()
for i in range(4)
my_pen.forward(50)
my_pen.right(90)
turtle.done()
Example code
# import turtle librar
import turtle
my_pen = turtle.Turtle()
for i in range(50)
my_pen.forward(50)
my_pen.right(144)
turtle.done()
Draw a Hexagon
Example code
# import turtle library
import turtle
polygon = turtle.Turtle()
my_num_sides = 6
my_side_length = 7
my_angle = 360.0 / my_num_side
for i in range(my_num_sides)
polygon.forward(my_side_length)
polygon.right(my_angle)
turtle.done()
Draw a square inside another square box
Example code
# import turtle library
import turtle
my_wn = turtle.Screen()
my_wn.bgcolor("light blue")
my_wn.title("Turtle")
my_pen = turtle.Turtle()
my_pen.color("black")
def my_sqrfunc(size)
for i in range(4)
my_pen.fd(size)
my_pen.left(90
size = size - 1
my_sqrfunc(146)
my_sqrfunc(126)
my_sqrfunc(106)
my_sqrfunc(86)
my_sqrfunc(66)
my_sqrfunc(46)
my_sqrfunc(26)
Drawing of another pattern
Example code
# import turtle library
import turtle
my_wn = turtle.Screen()
turtle.speed(2)
for i in range(30)
turtle.circle(5*i)
turtle.circle(-5*i)
turtle.left(i)
turtle.exitonclick()
Drawing of another pattern
Example code
# import turtle library
import turtle
colors = [ "red","purple","blue","green","orange","yellow")
my_pen = turtle.Pen()
turtle.bgcolor("black")
for x in range(360)
my_pen.pencolor(colors[x % 6])
my_pen.width(x/100 + 1)
my_pen.forward(x)
my_pen.left(59)
my_pen.width(x/100 + 1)
my_pen.forward(x)
my_pen.left(59)
0 Comments