Nauč se Python > Kurzy > Beginners course PyLadies > Loops > Turtle and loops

🐍 🐢

In this lesson, we will be drawing with the turtle module.

Run Python in interactive mode (after you activate Conda, write Python in the command line).

$ python

>>>

The characters > and $ are printed by the computer, not by you. On Windows, it will be > instead of $. Before the $ or >, there can be some other words.

Then write:

from turtle import forward

forward(50)

Now a popup window will appear, don't close it. Place it somewhere where you will be able to see it and your command line, too.

Where is the turtle?

Currently, the turtle is disguised as an arrow. There is a way how to unmask it:

from turtle import shape

shape('turtle')

Rotation

The turtle can rotate and crawl across the "paper". It has a brush on its tail which draws a line.

from turtle import left, right

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

Now give the turtle some commands. If you don't like the drawing you can close the window, or import and use the function clear().

Turtle program

Interactive mode is good for trying new stuff but we will now go back to our editors and write some program in a file.

Create a file ~/pyladies/03/drawing.py.

The directory ~/pyladies can have a different name on your laptop – see Python installation.

You can have a different name for your file, just don't use turtle.py.

Write drawing commands into the file and in the end call the function exitonclick (imported from module turtle).

Question

What does the function exitonclick do?

After you are done, we can start with drawing pictures:

Square

Draw a square.

Turtle square

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

Řešení

Rectangle

Draw a rectangle.

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

Turtle rectangle

Řešení

Three squares

Now draw three sqares, each rotated by 20°.

Three turtle squares

Řešení

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? Save it as ~/pyladies/03/loop.py

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

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

What does the command for do?

Řešení

Overwriting variables

What does the following program do?

sum = 0

for number in 8, 45, 9, 21:
    sum = sum + number

print(sum)

Řešení

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

Řešení

Discontinuous line

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

Try to draw a discontinuous line.

Turtle and discontinuous line

Řešení

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?

Řešení

Three squares

Finally, draw 3 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 code.

Three turtle squares

Řešení

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


Toto je stránka lekce z kurzu, který probíhá nebo proběhl naživo s instruktorem.