CS 151: Lab #2

Lab Exercise 2: Python, Functions, and Parameters

Main course page

We'll spend some more time this week learning Python syntax and creating functions, which are collections of code that we may want to execute more than once. An important aspect of functions are parameters, which are a way of making a function do more than one thing.


  1. On the Desktop, make a folder called lab2.
  2. Open a text editor (e.g TextWrangler). Create a new file called step1.py and save it in the lab2 folder. As we did last week, we're going to put a series of python commands into the file and then tell python to execute those commands.
  3. Put a comment at the top of the file with your name and the date. In python, you make a comment by using a #. Anything after the # is not read by the interpreter.
  4. After the comment line, write the command to tell python to import the turtle package:

    from turtle import *

  5. Now we can use turtle commands in our python file to tell python to make some pictures. Start by resetting the turtle using the function reset(). Then write out a series of forward and left commands to make the turtle draw a 50 wide and 100 tall rectangle.

    On the last line in your file, put the command raw_input() so that the python interpreter waits until you hit return in the Terminal to exit.

    If you want to turn off the turtle aniimation and make the drawing fast, put the command tracer(False) after the reset() command.

  6. Open up the Terminal application. By default you are in your home directory. Use the cd command to go to your Desktop/lab2 directory.

    cd Desktop/lab2

    Now tell the python interpreter to execute the commands in your file.

    python step1.py

    If something goes wrong when you run your program, read what the computer tells you. Sometimes the information is useful and you can figure out what went wrong (sometimes it's not so useful unless you know a lot about the python interpreter). It's always worth double-checking that the file you're trying to run is actually in your current directory by typing ls, which lists the files.

  7. Our step1.py program is a fine python program, but what if we want to make two rectangles? We'd have to copy and paste the rectangle code. If we wanted to make 100 blocks, we'd have to copy and past the code 100 times. That seems like a waste of time. Instead, maybe we can put a label on the 8 lines of code that make our rectangle and use the label to mean "execute this code".

    Functions let us break code into meaningful parts and put labels on them. To define a function in python, we use the keyword def, which is just short for define.

    In your text editor, create a new file. Save it as step2.py in your lab2 folder. At the top of the file, put a comment with your name and the date, then put the import statement, just as we did with step1.py.

    from turtle import *

  8. Now we're going to define a function with the label block. Our first version of this function won't have any arguments. To define the function we use the def keyword, followed by our label block followed by parentheses and a colon.

    def block():

    In python, the colon plays a special role. It means that the statement starts a section of code that is within the scope of the command. In this case, it starts a section of code that is within the scope of the function.

    The def command starts the code section, and on the next line we want to tab in the first command for the function, which should be the first forward command for drawing the block shape. Copy and paste the rest of the commands for drawing the block into the function, making sure they are all identically indented.

  9. After the function definition, we need to write the code that will execute it. First, we need to set up the turtle by calling reset(). Make sure the reset() command has no indentation, otherwise, it will still be part of the function. Second, we want to call the function block by using the label and a pair of parentheses. For example:


    Now let's move the turtle forward by 100 pixels and draw a second block. Here's where having our block as a function comes in handy. To draw the block again we just call the block() function. Last, put in the raw_input() command again to make the python intepreter wait.


    Run your step2.py program using the Terminal.

  10. One nice capability of functions is that they can take parameters. Parameters are symbols that represent places in memory that hold information. The block function, as we wrote it in the prior step, only draws blocks of a fixed size because we hard-coded the numbers into the function. We're going to change the function so that when we call the block command, we also tell it what size to make the block.

    First, we want to add two parameters to the block function definition by putting two variable names in the parentheses of the function definition.

    def block( dx, dy )

    Now we want to change our first call to forward so that instead of the number 50, we tell it to move forward by dx, which will hold whatever value is given to it when the block function is called.

    forward( dx )

    We want to change the second call to forward so that instead of the number 100, we tell it to move forward by dy, which will also be set when the function is called.

    forward( dy )

    Finish modifying the function by setting the third call to forward to move by dx and the fourth call to move by dy.

    Now go down to the lines in your file where the block function is called. Since we changed the definition of block to have two parameters, we need to include two parameters when we call it. Modify the two calls to block to look like the following.

    block(50, 100)
    block(25, 50)

    Save and run your new step2.py program.

    Now add two more parameters to your function so that the definition looks like the following. We're going to use the two extra parameters to position the block at an arbitrary location in the image.

    def block(x0, y0, dx, dy):

    To set the position of the lower left corner of the block we'll use a sequence of three commands at the beginning of the block function.

    goto( x0, y0 )

    The above commands should lift up the pen, send the turtle to the location x0, y0 and the put the pen down again. The rest of the block function will then draw the rectangle.

  11. Copy your step2.py file to two files: step3.py and shapes.py.

    cp step2.py step3.py
    cp step2.py shapes.py

    Open the shapes.py file and delete all of the commands after the block function. The shpes.py file should contain only the import statement and the block function.

    Open the step3.py file and delete the block function. Then add the following to the top of the file after the first import statement.

    from shapes import *

    Run the step3.py file and see what happens. It should do the same thing as the step3.py file, but now we've separated our function from the set of commands that make use of it. The turns out to be good programming practice for a number of reasons.

  12. Finish up the lab time exploring how to make use of your block function. For example, try rotating the turtle and then calling the block function, or make lots and lots of blocks in different places. You can also experiment with colors, line width, or other turtle attributes.

    Make sure you copy your step*.py and shapes.py files to your personal network drive before you leave the lab.

When you are done with the lab exercises, go ahead and get started on the assignment.