Skip to main content
Engineering LibreTexts

4.1: The turtle module

  • Page ID
    40871
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    To check whether you have the turtle module, open the Python interpreter and type

    >>> import turtle
    >>> bob = turtle.Turtle()
    

    When you run this code, it should create a new window with small arrow that represents the turtle. Close the window.

    Create a file named mypolygon.py and type in the following code:

    import turtle
    bob = turtle.Turtle()
    print(bob)
    turtle.mainloop()
    

    The turtle module (with a lowercase ’t’) provides a function called Turtle (with an uppercase ’T’) that creates a Turtle object, which we assign to a variable named bob. Printing bob displays something like:

    <turtle.Turtle object at 0xb7bfbf4c>
    

    This means that bob refers to an object with type Turtle as defined in module turtle.

    mainloop tells the window to wait for the user to do something, although in this case there’s not much for the user to do except close the window.

    Once you create a Turtle, you can call a method to move it around the window. A method is similar to a function, but it uses slightly different syntax. For example, to move the turtle forward:

    bob.fd(100)
    

    The method, fd, is associated with the turtle object we’re calling bob. Calling a method is like making a request: you are asking bob to move forward.

    The argument of fd is a distance in pixels, so the actual size depends on your display.

    Other methods you can call on a Turtle are bk to move backward, lt for left turn, and rt right turn. The argument for lt and rt is an angle in degrees.

    Also, each Turtle is holding a pen, which is either down or up; if the pen is down, the Turtle leaves a trail when it moves. The methods pu and pd stand for “pen up” and “pen down”.

    To draw a right angle, add these lines to the program (after creating bob and before calling mainloop):

    bob.fd(100)
    bob.lt(90)
    bob.fd(100)
    

    When you run this program, you should see bob move east and then north, leaving two line segments behind.

    Now modify the program to draw a square. Don’t go on until you’ve got it working!


    This page titled 4.1: The turtle module is shared under a CC BY-NC 3.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?