Python Basics

Python Turtle Programming Tutorial

Python Turtle is a good way to learn basic programming syntax of Python. You can use it to teach programming to kids. It is like a canvas or easel board that enables you to draw different figures by giving commands to turtle.

Steps involved in drawing using Python Turtle

  1. Prior to drawing anything using the turtle, you must start by importing the turtle module.
    import turtle
    from turtle import *
  2. Call turtle.Turtle() to create a new drawing board and assign it to an object t.
    t = turtle.Turtle()
  3. Start drawing by calling the different methods of turtle such as forward(), left(), right(). etc.
  4. When you are done with the drawing, call turtle.done()

Important methods that are used in turtle drawing

Methods Description
Turtle() Creates a turtle object.
forward(distance) This method moves the turtle forward by a specified distance.
backward(distance) This method moves the turtle backward by a specified distance.
left(angle) It is used to turn the turtle anti-clockwise by a specified angle.
right(angle) It is used to turn the turtle clockwise by a specified angle.
goto(x, y) It moves the turtle to the position (x,y) on the canvas.
dot(size) This method draws a dot of a specified size.
circle(radius) It draws a circle of a provided radius.
stamp() Leaves a mark of turtle shape at the current position.
shape('shapename') Changes the shape of a turtle to the specified shape. You can pass arrow, square, triangle, circle, and turtle.
setheading(angle) It is used to set the orientation of the turtle to the provided angle.
penup() or up() It lifts up the turtle's pen and no drawing after calling this method.
pendown() or down() It puts the turtle's pen on the canvas and drawing can be done after calling this method.
color('colorname') or color('#rrggbb') Sets the fill color and color of the turtle's pen.
pencolor('colorname') or pencolor('#rrggbb') Sets a color of the turtle's pen.
fillcolor('colorname') or fillcolor('#rrggbb') Sets the fill color of the turtle that will fill a polygon.
begin_fill() When you want to fill a shape with a color, then call this method. This method will remember the initial position of a filled polygon.
end_fill() This method fills the polygon with the current fill color by closing it between the current position and the initial position.
write('text') Writes a text on the canvas.

Using above methods, you can draw numerous shapes and figures. Even you can draw using different colors and fill shapes with your favorite color.