Python 004: Libraries, Functions, Objects

This will help you:

Learn how to use and learn about new Python libraries to expand what you can do.

If you've learned how to write simple scripts in Python, you hopefully feel proud of yourself - but you've only scratched the surface of what Python can do. Thanks to its relative beginner-friendliness and large developer community, there are a lot of Python libraries - basically, expansion packs - that expand what it can be used for. In this activity, we'll work with a built-in library, turtle, that allows us to draw graphics. Later on, we'll use drawSvg to create SVG files along with turtle drawings, so they can actually be laser cut.

Time: 1-2 hours / Level: A3

You should already:

  • Be familiar with Python syntax and functions (see Python Basics 1)

  • Install drawSvg: try typing pip install drawSvg in the terminal.

Get the code and resources for this activity by clicking below. It will allow you to download the files from a Google Drive folder. Unzip the folder and save it in a sensible location.

Step 1: Warm-up - Working with turtles

Open and read it over. Read the turtle documentation for each function and see what it does. Notice that there are several names for some of the functions, such as turtle.pu()/turtle.penup().

Optional read: A quirk of the turtle library, which makes it unusual compared to other libraries you may work with, is that import turtle imports a whole, single turtle object. Think of it like learning a language in school: a library is usually like a unit on, say, going to a restaurant, or going to an airport. In that unit, you might learn about different objects that relate to the activity, different actions you can do with those objects, and the words for talking about those objects and actions. Similarly, most libraries consist of objects and actions you can do, or actions specific to objects (sometimes called methods.) To actually use those objects, you usually have to create a variable for them first. This could look like:

import airportThings # all the things!
my_plane = airportThings.Plane() # make just one plane # or whatever you want to do with the plane...
your_plane = airportThings.Plane() # this makes a whole other plane!

turtle is a little different. Instead of talking about an airport, tickets, security, luggage, flights, and terminals, it just gives you an airplane. import turtle creates one turtle object called turtle, which can do all the things described in the documentation. You can clone it to get more. Like this:

import turtle as my_turtle # this is just one turtle
your_turtle = my_turtle.clone()

There's no other way to create a new turtle, separate from the original one. A lot of times, you can import a single object from a larger library, like from airportThings import Plane, but you still have to create a specific instance, like my_plane = Plane() similar to the plane example from before.

Anyway. See what you can do with the turtle. Test this code by running python in the terminal. Read the comments to help you think about what the code is doing, and read the documentation if you're confused. Can you:

  • Draw a square?

  • Draw a circle?

  • Change the line and fill color?

  • Speed it up?

  • Make a cloned turtle?

Step 2: Activity - Fast Functions

You may have noticed that making turtles do certain things takes a lot of repetition. Hopefully, you used some loops to simplify things (like making a circle).

What if you wanted to make a square, and then another square half the size? You'd have to write all that code twice. What if we had some kind of structure that could reuse code without rewriting it, and only change a few details if we wanted to?

This is exactly what functions can do. They pack multiple lines of code under a single name and take 0 or more parameters, which are like options for how the function will do things. Open and read through it to see how functions are used. Then, see if you can complete the function headers to create interesting patterns. Test it by typing python in the terminal.

Step 3: Turtle Armies

Have you ever held a bunch of markers together in a fist to draw the same thing in multiple colors? That's what we're going to do with turtles. We'll create our own TurtleArmy object or class to do this. An object or class is just a bunch of information about something in one package, along with methods, which are functions for what it can do. Open and read through it to see how classes are created. Then, see how you can use the TurtleArmy to move a bunch of pens at once. Test it by typing python in the terminal.

To use the TurtleArmy in other code you write, you can put from turtle_army import TurtleArmy at the top of the file. To create an army, type something like little_army = TurtleArmy(3) with however many turtles you want. Then, you can use little_army in most places you would use a regular turtle.

Step 4: Make it your own

Open It contains a version of the turtle object, DrawingTurtle, which can draw a vector file as it moves. Test it by typing python in the terminal.

Try using your previously made functions to draw a cool vector image with this turtle. To use the DrawingTurtle in other code you write, you can put from drawing_turtle import DrawingTurtle at the top of the file. To create a drawer, type something like this:

d = drawSvg.Drawing(200, 200, origin = 'center') # create a vector canvas
t = DrawingTurtle(d)

Then, you can use t in most places you would use a regular turtle. Try creating a cool vector design, then laser cut it into something.