Python Turtles

This may be one the most satisfying parts of learning Python.   It is intuitive, visually rewarding and just kinda fun!  Get ready for Python Turtles!  Now type the following commands into IDLE.  Do not type the # sign or stuff to the right of it.  That is a comment to learn from.

Open IDLE (go to start menu, run box, type IDLE, hit Enter)
>>>import turtle  #imports the needed code
>>>s = turtle.Screen()   #open a turtle screen
>>>t = turtle.Turtle(shape=’turtle’)  #creates a turtle in shape of turtle
>>>t.forward(100)  #it moves 100 pixels forwards
>>>t.left(90) #changes the direct of the turtle is facing by 90 degrees to the left
>>>,360) #make a full circle 100 pixel radius 360 degree around
>>>, “blue”) #makes a blue dot 100 pixel diameter
>>>t.goto(-35, 120)  #dead center is 0,0 this is 35 pixels left of center and 120 pixels above center
>>>t.goto(0,0)  #returns to center
>>>t.speed(9) #sets the speed from 1 slowest – 10 fast, 0 = fastest, “normal”
>>>t.pen(fillcolor=”black”, pencolor=”red”, pensize=10)  #makes a large red line
>>>t.forward(100)  #it moves 100 pixels forwards
>>>t.pencolor(‘#32c18f’)  #changes the line color to html hex color of choice
>>>t.isdown()  #returns true if figurative “pen is on paper” or false “pen is lifted”
>>>t.fillcolor(“red”)  #makes your turtle red
>>>e = t.clone() #makes a copy of turtle “t” named “e”
>>>t.reset()  #clears the trails and centers the turtles
>>>s.clear()  #clears the turtle screen


Now lets make a group of turtles and reference them a slightly different way

>>>import turtle  #imports the needed code
>>>s = turtle.Screen()   #open a turtle screen
>>>turtles =[]  #Create empty list
>>>turtles.append(turtle.Turtle())   #add one turtle
>>>turtles.append(turtle.Turtle())   #add second turtle
>>>turtles.append(turtle.Turtle())   #add third turtle
>>>turtles[0].lt(90)   #Move the turtles left (lt short for left)  90 degrees
>>>turtles[1].rt(90)   #Move the turtles right (rt short for right)  90 degrees
>>>turtles[0].fd(100)   #Move the turtles forward0 (fd short for forward)  100 pixels
>>>turtles[2].clear()   #Clears the turtle trail (if any)
>>>turtles[2].ht()   #Hide turtle, but not delete
>>>turtles[2].st()    #Show turtle
>>>del turtles[2]  #Delete the turtle object

See more turtle commands:


Typing line by line sucks a little because you have to retype everything to see it again.   Let’s make a program that makes a smiley face we can run over and over again.

Copy the code below and paste it into a text editor.  Save it in the directory you installed Python as   c:\python  is the directory where I have it installed.

import turtle
s = turtle.Screen()  #launches turtle box screen
t = turtle.Turtle(shape='turtle')
t.pen(fillcolor="black", pencolor="blue", pensize=10)

#'turtle t draws a smiley face with chin at coordinate (x, y)'
# set turtle direction and pen size

# move to (x, y) and draw head
t.goto(0, 0)

# draw right eye
t.goto(35, 120)

# draw left eye
t.goto(-35, 120)

#draw smile
t.goto(-60.62, 65)
t.setheading(-60) # smile is a 120 degree, 120) # section of a circle

t.goto(0, 80)


Open the command prompt by typing “cmd” in the Windows search box and hitting Enter.  Navigate to the Python directory and run the file.   Then type python  For me it is the following commands:

cd python

If everything went according to plan, you should see a turtle window open and smiley face being drawn.  If so – well done!  Try making something of your own by editing the code or starting from scratch.

<– PREVIOUS  or  NEXT –>