FREE INTERACTIVE ONLINE COURSE

Python 3

FOR BEGINNERS


"The first Python course that simply amazed me. Very well explained and easy to understand." (Alexandru Cosmin)

"The best Python course in Romania." (Iulian Geană)


ALL REVIEWS
LESSON 6
PAGE 1 / 4
Learning with Turtle!
Home >>> Free Online Lessons, Python 3

Our First Module

The set of functions that the Python language implicitly contains is not very large, but it is extended through modules (collections of functions, called libraries in other languages).

One of these is turtle, which allows us to program a "virtual turtle" in graphical mode that moves and can draw behind it. Isn't that super cool? Without further ado, initially run the program below:
Editor - lesson_6.py
       
Graphical Mode in Python done
Console/Output done
DETAILS

1. To include a module in our program, we use the import directive, followed by the library name, in our case, turtle:

import turtle

2. The "Turtle" is actually a graphical object called Turtle, which is stored in the variable t. Its position is in the center of the screen, oriented towards the East - the first time it moves forward 75 pixels, it implicitly goes to the right.

3. The turtle module contains a series of interesting functions that allow us to achieve remarkable graphical effects quite easily. To move the turtle, we can use the functions:

forward(num_pixels) #forward
backward(num_pixels) #backward
left(degrees) #turn left
right(degrees) #turn right


Thus, to create the square, we simply wrote the sequence 4 times:

t.forward(75)
t.left(90)


4. The function color(color_code) sets the line drawing color. Above, we used "red", but there are many more options:

"black" (
), "blue" (
), "lime" (
), "cyan" (
), "red" (
), "magenta" (
), "yellow" (
), "white" (
), "brown" (
), "tan" (
), "green" (
), "aquamarine" (
), "salmon" (
), "purple" (
), "orange" (
), "gray" (
).

You can also use hex values, such as "#00ff00" ... Google can help you with that [here].

EXERCISE

Try to modify the program so that it draws a green rectangle with dimensions of 60 x 40!

About Turtle Graphics

It is currently known as the Turtle Graphics language, because by using commands, it or a robot can draw graphic elements on a screen or surface. Logo is an educational programming language created in 1967 by Daniel G. Bobrow, Wally Feurzeig, Seymour Papert, and Cynthia Solomon:



Seymour Papert, from MIT's Artificial Intelligence Laboratory invented Turtle Graphics in the 70's. Find out his description of it:

... "the turtle." You can think of this as a drawing instrument... Imagine that you are looking at a computer screen. On it you see a small turtle, which moves when you type commands in a language called "turtle talk," leaving a line as it goes. The command "Forward 50" causes the turtle to move straight ahead a certain distance. "Forward 100" will make it move in the same direction twice as far. You soon get the idea that the numbers represent the distance it moves; they can be thought of as turtle steps. Now if you want to make it go in a different direction, you give it a command like "Right 90." It stays in the same place but turns on itself, facing east if it had previously been facing north. With this knowledge you should easily be able to make it draw a box. If that's easy for you, you can think about how to draw a circle, and if that's easy you can try a spiral. Somewhere you will meet your level of difficulty, and when you do I'll give you this piece of advice: Put yourself in the place of the turtle. Imagine yourself moving in the outline of a box or a circle or a spiral or whatever it may be.
Try to solve the proposed exercise,
then proceed to the next page.
 home   list  CONTENTS   perm_identity   arrow_upward