Lesson 2

Importing code into Python


Alright, now that we’ve made sure everything is set up correctly, let’s start some coding!

Type the code into your Project1.py file as shown below.

On line 1 we have some green text, beginning with a “#“.

This is called a comment. Comments are ignored by Python when running the code. So why do they exist?

Comments are used as notes for both yourself and other programmers to help remember/understand how code works. They are probably of the most useful programming tools, and one of the easiest!

On line 3 we have a keyword called import (keywords are important built-in words of a programming language, they usually have colors to help them stand out.).

“turtle” is a library, pre-existing code that we can pull and use in our own programs so that we do not have to write everything from scratch.

Since our program now knows what a turtle is, we will be able to control it with our code

Programming Our Turtle


Now that we can code commands for our turtle, let add some more “turtle specific” code.

On Line 5, we will be creating a variable. This variable will help us save time later on. Instead of having to type turtle.Pen() over and over, we can use ‘t’ instead, and our program will know what we mean

On line 7, we will set the size of the pen our turtle will use.The pensize takes an integer which represents how many pixels wide we want the pen to be. Start with 2, this number can always be changed later on.

On line 8, we we will be choosing the color of that pen. The pencolor takes a string (a text contained in quotes), and has to match one of the possible colors the turtle knows. For now, use one of the following colors: blue, black,purple, red, teal, green, violet. Make Sure to match the case!

On Line 9, we will set the speed at which our turtle will move. Start with 2, this can always be changed later.

So far, we have set up all of the ‘variables’ that affect our pen. The Size, the Color, and the Speed. Now we can actually start programming our turtle to move.

Explaining Turtle Commands


To move our turtle, we use a set of commands that our turtle recognizes.

t.forward() will move our turtle forward a certain number of pixels.

t.forward() will move our turtle backwards

t.right() will move our turtle to the rightr

t.left() will move it to the left.

Copy the code shown below, or create your own!

To test your code, look for “Run” on the top toolbar. Select “Run without Debugging.