Chapter 4. Functions and Interfaces
This chapter introduces a module called jupyturtle, which allows you to create simple drawings by giving instructions to an imaginary turtle. We will use this module to write functions that draw squares, polygons, and circles—and to demonstrate interface design, which is a way of designing functions that work together.
The jupyturtle Module
Instructions for downloading the jupyturtle module are in the notebook for this chapter. We can import it like this:
importjupyturtle
Now we can use the functions defined in the module, like make_turtle and forward:
jupyturtle.make_turtle()jupyturtle.forward(100)
make_turtle creates a canvas, which is a space on the screen where we can draw, and a turtle, which is represented by a circular shell and a triangular head. The circle shows the location of the turtle and the triangle indicates the direction it is facing.
forward moves the turtle a given distance in the direction it’s facing, drawing a line segment along the way. The distance is in arbitrary units—the actual size depends on your computer’s screen.
We will use functions defined in the jupyturtle module many times, so it would be nice if we did not have to write the name of the module every time. That’s possible if we import the module like this:
fromjupyturtleimportmake_turtle,forward
This version of the import statement imports make_turtle ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access