FREE ONLINE INTERACTIVE COURSE

Python 3

FOR BEGINNERS

LESSON 6
PAGE 1/4
Learning with Turtle
Home >>> Free online lessons

Our first module

The set of functions in Python 3 language is not very large, however it is extended by modules (function collections, called libraries).

One of the modules is called turtle, which allows us to program a "virtual turtle" graphically which moves and can draw behind it. Isn't that super cool? Without too much detail, initially run the program below:
Editor - lesson6_turtle.py
       
Graphic mode in Python done
Result in console done
DETAILS

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

import turtle

2. "The turtle" is in fact a graphic object, named Turtle, which is retained by the t variable. Its position is in the center of the screen, facing East - the first time it goes 75 pixels, and defaults to the right.

3. The turtle module contains a number of interesting features that allow us getting great graphic effects pretty easy. To move the turtle, we can use the functions:

forward(pixels)
backward(pixels)
left(degrees) #rotate to left (counterclockwise)
right(degrees) #rotate to right (clockwise)


To create the square, I simply wrote the sequence 4 times:

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


4. color(color_code) function sets color of the line. We used "red", but there are many more:

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

You can also use hexa values, like "#00ff00" ... Google also helps us [here].

EXERCISE

Try to modify the program so that a rectangle measuring 60 x 40 pixels is drawn in green color!

About Turtle Graphics

Logo is an educational programming language, designed in 1967 by Daniel G. Bobrow, Wally Feurzeig, Seymour Papert and Cynthia Solomon. Today the language is remembered mainly for its use of "turtle graphics", in which commands for movement and drawing produced line graphics either on screen or with a small robot called a "turtle". The language was originally conceived to teach concepts of programming related to LISP language and only later to enable what Papert called "body-syntonic reasoning" where students could understand (and predict and reason about) the turtle's motion by imagining what they would do if they were the turtle.



[see more details here]
Try to solve the proposed exercise,
then move on to the next page.
 home   list  LESSONS   arrow_upward