Python reference
General Python
def <func_name>():
<statement>
<statement>
|
Define a function (named group of statements) with name func_name containing indented statements. |
def draw_square(): for i in range(4): forward(100) right(90) Defines the |
<func_name>() |
Call the function named func_name defined previously. |
draw_square() Runs the code written above for drawing a square. |
def <func_name>(<param1>, <param2>...):
<statement>
<statement>
|
Define a function (named group of statements) with name func_name and parameters param1, param2 containing indented statements. Parameters provide additional information to the function to provide greater flexibility. |
def draw_square(size): for i in range(4): forward(size) right(90) Defines the |
Loops
for i in range(<num>): <statement> <statement> |
Repeats indented statements <num> times. |
for i in range(4): forward(100) right(90) Draws a square with a side of 100. |
Turtle Graphics (requires from turtle import * at top of program)
| forward(<pixels>)
fd(<pixels) |
Move the turtle forward by the specified number of pixels in the current orientation. | forward(100)
Moves the turtle forward by 100 pixels |
| backward(<pixels>)
bk(<pixels) |
Move the turtle backward by the specified number of pixels in the current orientation | backward(100)
Moves the turtle backward by 100 pixels |
| left(<angle>)
lt(<angle>) |
Turn the turtle left by the specified number of degrees | left(90)
Turns the turtle left 90 degrees |
| right(<angle>)
rt(<angle>) |
Turn the turtle right by the specified number of degrees | right(90)
Turns the turtle right 90 degrees |
| up() | Bring the pen up (moving the turtle doesn’t draw on the canvas) | up()
goto(0, 0) Return to origin without leaving a trace. |
| down() | Bring the pen down (moving the turtle draws on the canvas) | |
circle(<radius>) |
Draw a circle of the given radius | circle(30)Draw a circle of radius 30. |
| hideturtle() | Hide the turtle. | |
| goto(<x>, <y>) | Move the turtle to the specified location. | goto(0, 0)
down() goto(100, 100) Draw a line between the origin and the point (100, 100) |
| color(<str>)
color(<red>, <green>, <blue>) |
Change the drawing color according to either a string (color name or hex color code starting with #) or red, green blue values between 0 and 1. | color(“red”)
Makes the pen color red. color(1, 0, 1) Make the pen color purple. |
bgcolor(<str>) |
Change the canvas’ background color according to the same rules as color. | bgcolor(“red”)
Makes the background color red. |
| fill(<boolean>) | Change the fill status. Call fill(True) before drawing a closed shape to fill and fill(False) at the end. |
color("green")
fill(True)
for i in range(4):
forward(100)
right(90)
fill(False)
Draw a green filled square. |
| shape(<shape>) | Change the turtle’s appearance. By default, the following shapes are available: “arrow”, “turtle”, “circle”, “square”, “triangle”, “classic” | shape(“turtle”)
Changes the shape of the turtle to look like a turtle. |
| register_shape(<file>) | Add a GIF image as a possible turtle. Must be in the same directory. | register_shape(“cake.gif”)
shape(“cake.gif”) Adds a cake image to the list of possible turtles and sets the turtle to look like a cake. |
| stamp() | Draw an impression of the turtle. |





