Introduction: Learn to Code Using Python Turtle
In this tutorial we will introduce to the fun world of coding using Python, specifically the Turtle library.
We assume you have no prior coding experience. If you are interested in learning more, we suggest to read the author's book:
Step 1: Requirements
What is required?
A PC or Raspberry Pi with Python 2.7 or greater installed.
Amazingly Python can be run in any PC under most of operating systems. For information how to download python in your device go to:
Under Download menu, select your operating system and follow the installation instructions.
Step 2: Using IDLE Editor
Python's IDLE (Integrated Development and Learning Environment) is the editor we will use in this tutorial but there are many more. At Robo-Geek we like that is simple to understand, it gets the job done and it's not intimidating for first time coders. For more information on IDLE, please check:
After Python installation, we need to open IDLE.
How to open IDLE editor will depend on which operating system is used. For simplicity the rest of the tutorial will assume you are using a PC with Windows 10. If not, don't worry too much, do a quick internet search and you will find tons of help.
In Windows, just go to Start Menu, look at Python folder and select IDLE icon.
If successful you will see the screen shown in the picture for this step. The first line under the menu, states what Python version you are using.
Step 3: First Lines of Code - Import Python Turtle
Now the exciting part of the tutorial. Let's get to code:
First we need to call the library we will be using, this is done using the import command. Type the following and press Enter.
Notice IDLE will highlight orange the python command import. Next we need to create a turtle object, type the following code and press Enter
t = turtle.Turtle()
Once you finish pressing Enter, if there is no syntax or spelling errors, a new screen showing a triangle in the middle of white background will be shown as in the picture for this step. Place the windows separately and adjust the windows dimension so they fit side by side.
Step 4: Creating a Square
The small triangle in the center of the screen represents a turtle.
To make the turtle move forward, type the following:
Notice, the turtle moved 100 pixels in the direction the turtle is pointing. Now let's turn the turtle 90 degrees to point down:
Now that the turtle is down, we will write a few more commands to complete the square:
t.rt(90) t.fd(100) t.rt(90) t.fd(100)
Fantastic you have completed your first square!
Now let's reset, bring the turtle home and clear the screen by:
Alternatively, we can draw the square more efficiently by using a for loop:
for i in range(4): t.fd(100) t.rt(90)
We can change the color of the turtle to blue by:
And of course we can do a lot more, this tutorial was just to give you a taste and get you going. For more information check Python Turtle documentation,
Also consider purchasing the author's book:
All the best.