🐍 🐢

In this lesson, we will be drawing with the ColabTurtle module a lot.

In the older versions of the materials, we have used the turtle module which is available in for every Python installation but more and more Mac users started to have unfixable issues with it so we have decided to switch to ColabTurtle that we also use for the homework for this lesson.

Start the Google Colab with a newly created notebook and install and initialize the ColabTurtle in a following way.

First run this line in a first cell:

!pip3 install ColabTurtle

This will install the ColabTurtle module inside your colab environment for this lesson. You need to do it once in every colab notebook, where you want to use it. You also need to run this cell if you close and open this colab notebook again.

When this is done, you can import all functions from ColabTurtle module using this:

from ColabTurtle.Turtle import initializeTurtle, forward

Don't worry, we will talk about the possible ColabTurtle functions throughout the lesson.

Before you start using any turtle commands in a new cell, you need to initialize it by typing this:

initializeTurtle()

Initialization means that a fresh canvas window is created below the current cell, turtle image is rotated with a head north and you can start drawing!

Try moving the turtle forward by 50 units (screen pixels) by running following in a new cell:

forward(50)

You can see that a new canvas is drawn below the current cell and the turtle is leaving a drawn line behind itself with length 50.

Rotation

In order to clear the canvas for next drawing, you need to call the initializeTurtle() again.

The turtle can rotate and crawl across the "paper".

from ColabTurtle.Turtle import forward, left, right, initializeTurtle
initializeTurtle()

forward(50)
left(60)
forward(50)
right(60)
forward(50)

This is already really powerful way how to draw a lot of beautiful pictures.

For the further steps you can freely consult the ColabTurtle API description (what each available function does and what parameters does each function have) on https://github.com/tolgaatam/ColabTurtle#api.

Square

Draw a square.

Turtle square

A square has four equal straight sides and four 90° angles.

Solution

Rectangle

Draw a rectangle.

Try to make it so that the turtle will "look" to the top in the end (like it was in the beginning).

Turtle rectangle

Solution

Three squares

Now draw three squares, each rotated by 20° from the previous one!.

Three turtle squares

Solution

Can we write it better?

So much code! There has to be a way how to simplify it.

There is. Now we will learn the command for.

Repetition

What does the following code do?

for number in range(5):
    print(number)

for greeting in 'Ahoj', 'Hello', 'Hola', 'Hei', 'Hallo':
    print(greeting + '!')

What does the command for do?

Solution

Overwriting variables

What does the following program do?

sum = 0
# lets print how the sum is initialized before the loop starts
print("sum is", sum)
for number in 8, 45, 9, 21:
    # internal variable inside the loop is number
    print("number is", number)
    sum = sum + number
    # sum variable is holding the temporary sum of values
    print("sum is", sum)
print('loop ended, sum is', sum)

Solution

Square

Back to drawing! This time we will use loops.

Draw a square.

Use forward only twice, once in the import and once as function.

Turtle square

Solution

Discontinuous line

The functions penup and pendown from the ColabTurtle module tell the turtle to stop/start drawing.

Try to draw a discontinuous line.

Turtle and discontinuous line

Solution

Now try to make it so that the lines that are drawn become gradually bigger.

Turtle and discontinuous line]

Help

What exactly does the command for do? Can we use the variable that it sets up?

Solution

Loop variable Naming

Always use a meaningful loop variable name, like for index_tab_browser in range(3, 18): close_tab_in_browser(index_tab_browser) not just i, j, x, y etc. When using meaningful names:

  • the code is more understandable to colleagues reading your code,
  • it's easier to find errors in the loop logic
  • text searches for the variable name return relevant pieces of code operating on the same data are more reliable

There is one exception - when it's a single-level loop and the variable has no meaning other than "the number of times I've been through this loop", in which case i is usually be used.

Three squares

Finally, draw three squares, each rotated by 20°. Now you know how to write it in a simple way: repeat the code by using for, do not copy the same code three times.

Three turtle squares

Solution

Extra tasks

When you are done, try to draw stairs:

Turtle stairs

When you are also done with the stairs, try to draw 7 hexagons:

Turtle hexagons