Python Modules

So you've probably been wondering where the sprites and graphics are in Python. It turns out that there are many different ways to display graphics in Python by using modules. A module is code that someone else has written that we can then import into our file and use (just like a library in Snap!).

The following turtle exercise is optional

A simple and popular graphics module for Python is called turtle. To import the turtle module, uncomment the following line of code to the top of your file:


import turtle
        

If you decided to complete this exercise, you must comment out the line import turtle in your starter code file beofre you submit it to the autograder. Otherwise, the autograder will break. This import statement is commented out by default in the starter code.

This imports the turtle module and allows us to use all of its functionality. If we want to call a function from the turtle module we would do something like below:


turtle.forward(100) # moves the sprite 100 steps forward
        

Notice how the text following "#" are faded. This is because "#" signals the beginning of a comment. Comments are for you (and other people looking at your code) to read.

Sometimes we might want to rename the modules that we import so that they are more easy to type or have a more intuitive name. Let's say that we felt like turtle was too long of a word to type before each function call. We could have instead written the following import line: import turtle as t. This would then allow us to access the turtle module via the alias name t. This would allow us to call a function like this:


import turtle as t
t.forward(100)
        

Turtle Functions

The turtle module has all of the same drawing functions as Snap! and much more. A few of the module's functions are shown below, but if you want to learn more about the incredible things that you can do with turtle please refer to the documentation. The first time that you call any one of the turtle functions a new window will open for the turtle to draw in. You can think of this window as being analogous to the stage in Snap! its just that we only see the "stage" in python when we actually draw something.


# moves the sprite 100 steps forward
turtle.forward(100)

# moves the sprite 100 steps backward
turtle.backward(100)

# rotate to the right 45 degrees
turtle.right(45)

# rotate to the left 45 degrees
turtle.left(45)

# Pen doesn't draw when moving
turtle.penup()

# Pen draws when moving
turtle.pendown()

# Go to a coordinate (x: 130, y: -50)
# The turtle starts at the center of the stage which is at position (x: 0, y: 0)
turtle.goto(130, -50)

# Write the string "Hello World!" to the stage at the sprite's current position.
# Don't worry too much about the font=(...) part, just make sure it's there.
# Refer to the official documentation for more information (linked above).
turtle.write("Hello World!", font=("Arial", 30, "normal"))

# clear all drawings on the stage
turtle.clear()
        

As you can see almost all of these functions are identical to what is in Snap!. Many of these functions have abbreviations as well, such as turtle.fd(100) which moves the sprite forward. Check out the documentation for other tips, tricks, and help.

Exercise 5 (Optional)

Important: Before submitting your code, please make sure the line import turtle as t is commented out. Otherwise, the Gradescope autograder will not work.

Write a function that when called draws the fractal pattern shown below (this is the c-curve from the past recursion lab). Also use the turtle's write function to write your name (or something funny or both!) on the stage somewhere. You should use recursion and the turtle module to complete this exercise.

There should be an image here There should be an image here There should be an image here There should be an image here There should be an image here There should be an image here

Congrats, you've finished your first Python lab!