Lab 1: Pictures and Sounds

Most programs that we use every day display images and play sounds, and Python can do those things too.

Drawing Pictures

In your lab01 folder, create a new file named, and paste in the following code:

from turtle import *


What happens when you run the file? Discuss with your partner: what do you think fd, lt, rt, and bk functions do, and what do those names stand for? (try changing the values to see what changes)

Click here to show the answers

fd stands for "forward," lt and rt stand for "left" and "right," and bk stands for "backwards." Each of these functions acts like a command that moves or turns the cursor, and wherever the cursor moves, a line is drawn. Here is a picture of what gets drawn, with a second version showing the motions of the cursor during the drawing process in pink:

The results of the code above: a line extending 50 units to the East, then going North 100 units, and finally Southwest (at a 45-degree angle) 100 units, with an arrow at the end of that line pointing back along its length indicating that the cursor is at the end of that final line facing towards the line it just drew.The same image, but with pink arrows next to the black drawn lines indicating each movement and turn that the cursor takes (it follows the steps in the previous description).

Now add these lines to the end of your file:


What do you expect to be drawn if you run the file again?

Click here to show the answer

These commands create a large dark blue pen, which will draw a thick line with rounded ends, going backwards from the end of the original drawing. It will look like this (second image shows direction of movement):

The same image of lines as before, with an additional thick and rounded blue line extending farther Southwest from the end of the diagonal line where the cursor ended up before. The cursor is now at the Southwest end of this new line, pointing Northeast, and mostly embedded in the circular end of the thick line.The same image, annotated to show how the pen moved Southwest along the thick blue line.

What happens if you leave out the import line? What do you think import does?

Click here to show the answer

Without import, Python will complain that fd is not defined. import makes new functions available in the current file. The specific code from turtle import * is a way of asking to import "everything" from the turtle module, which is where the drawing functions we'll use are defined.

Using functions from the turtle library, we can draw lines, and we've provided an additional module called turtleBeads which allows you to more easily draw complex shapes like circles or ellipses. At the top of your file, add the following line:

from turtleBeads import *

(Make sure that your file is saved in the same directory as the file we provided.)

Then, at the bottom of your file, add these lines of code:


Can you guess what they will do?

Click here to show the answer

drawCircle draws a circle, which is pretty easy to guess, but leap is a bit more obscure. As it turns out, leap moves the cursor forward just like fd does, but without drawing a line. These images show the result and indicate where the leap happened:

The same image, with an additional thick and rounded blue circle off the Northwest end of the diagonal line. The cursor is visible in the center of the circle.The image with the circle again, but annotated to show that the the command leap(170) moved the turtle from the lower tip of the blue line to the center of the circle without leaving a pen trail.

Note that in the base turtle library, the functions penup and pendown can be used to stop or restart drawing, which is a more complex way to move the cursor without drawing. turtleBeads also includes a hop function to move sideways without drawing.

Making Noise

We use the turtle functions to draw, and we can use functions from a library called wavesynth to make sounds. Like, is a file that we've supplied, so it has to be in the same directory as your file.

In addition, the library cannot play sounds directly unless you install the simpleaudio package. To do that in Thonny, go to the "Tools" menu, click on "Manage packages," search for "simpleaudio," select the first result, and then click the "Install" button at the bottom (you can then close the package manager).

Click here for video overview of installing simpleaudio

This short video gives an overview of how to install simpleaudio (you only need to do this once).

Once simpleaudio is installed, create a new file named and paste the following code into it:

from wavesynth import *



Run this code and listen to the result. What do you think the addNote, climbUp, and climbDown functions do? (Hint: try removing them or changing the numbers and seeing what happens.) What about saveTrack and playTrack?

Click here to show the answer

The code should play a sequence of five notes one after the other, the first three going up and the last two coming back down. Each note lasts 1/2 second. It should sound like this:

The addNote function adds a note to the audio being produced, and the argument (the number in parentheses) controls how long the note will be. Meanwhile, the climbUp and climbDown functions will change the current pitch, meaning that notes added afterwards will sound higher or lower (their arguments determine how much higher or lower). saveTrack produces an audio file containing all of the notes added so far, while playTrack actually plays the track. The argument to saveTrack determines the name of the file that we save.

Now, add the following code to your file, before saveTrack and playTrack but after the three original notes have been added:


Discuss with your partner what you think will happen now. What would you guess rewind does?

Click here to show the answer

The code plays the same 5-note sequence, but this time there's a much lower 2.5-second note playing at the same time. It should sound like this:

The rewind function backs up so that the next note begins at an earlier time, instead of beginning at the end of the previous note. You can use this to create multiple notes that play simultaneously.

Next, add these lines before your saveTrack line:


Run the file to hear what changes. What do you think quieter does? How about addBeat and addRest?

Click here to show the answers

The code plays the same 3-note sequence with a fourth lower note, but now adds some drum beats as well: a half-second beat starting halfway through the first note, and then two half-second beats during the last note. It should sound like this:

addBeat creates a drum sound, while addRest just skips ahead without any sound playing (you can think of it as "adding silence"). The quieter function makes any notes or beats added afterwards sound quieter than those before, with the argument controlling how much (each step is a factor of 2/3). louder can be used as the opposite of quieter, although there is a maximum volume level.

Finally, try adding this line of code to the start of your file, before the first addNote call:


Run the file again, then "comment out" the setPitch line by adding a '#' at the start, and run it to hear the old sound. How does setPitch affect the outcome? You don't need to know what C5 means at this point, but you can try any name starting with a letter A through G and a number 1 through 8 (warning: pitches with a number higher than about 6 might be uncomfortably high).

Click here to show the answers

By adding setPitch, you change how high or low the starting note is (that's what "pitch" means in music), and the other notes are based on that pitch too. Here's what it should sound like using C5 as the starting pitch:

It's not necessary for you to understand the pitch system completely, but in general, a higher number makes a higher-pitched sound, and the letter A through G controls exactly how high. So C5 is a lot higher than C4, while D4 is just slightly higher than C4. For the curious, we're using scientific pitch notation.

Function Info

We've been asking you to guess what different functions do, but in fact, there are two ways that you can get more information about the functions you will use in this class. First, within Python, there's a function called help which can tell you some details about any other function. For example, in the shell, try running:


This should show you some text describing what the function does. In addition to the help function, our website has a quick-reference page that has brief descriptions of each operator and function you'll need in this class, along with examples of how to use them. It includes specific sections for turtle and for wavesynth that each have some extra advice about using those modules.

For turtle specifically, you can also find official documentation, and we have a page that lists the available color names.

Next Up

When you're finished with this part of the lab, decide with your partner whether you're more interested in exploring drawing or music further, and then move on to either the Turtle graphics part or the Wavesynth sounds part as appropriate. You probably won't have time to complete either part in lab, but we'll post solutions to both parts which you can use as references when working on the problem set for this week.

Table of Contents