Chapter 9 : First steps in Python

Note: if you are attending a face-to-face course, you will receive this document printed on paper for your convenience.

 Project layout 

By the end of this chapter, learners will be able to master the layout of Python programs and use the basic elements of syntax. They will be able to handle the IDLE editor with ease.


The objectives of this chapter are:

  1. To be able to layout a Python program correctly;
  2. Master the basic elements of Python syntax;
  3. Handling the IDLE editor easily and correctly;
  4. Understand editor error messages and apply the necessary corrections.
 Graphics and page layout 

We saw in the chapter on Python Basics that page layout plays a vital role in Python. Let's start with a few programs in graphics mode. The huge advantage of this is that even the slightest error appears directly on the screen, and it's less complicated to find the cause of the problem than with conventional programs.

The small programs in blue must not be "copied and pasted" into your IDLE editor. For the quality of your learning experience, it is imperative that you retype them entirely in your editor. This is the best way to learn the correct layout of your programs. Warning: do not type in the line numbers, they are there only to facilitate communication.

Please note that all lines beginning with the # sign (pound sign) are comments. These are indications intended for the reader of the program, but are ignored by the computer at runtime. If you want to save time, there's no need to type these comments into your current programs.

Completing the loop

Here's an incomplete circle. There are at least two ways to close this circle... Can you find two ways to close this circle? Test your solutions!!!

1 from turtle import *
2 # Importation de toutes les commandes (*) de la bibliothèque turtle
3 for i in range (30):
4     forward (10)
5     left (10)


Run this program. Can you explain why it looks like this?

1 from turtle import *
2 for j in range (2):
3     for k in range (18):
4         forward (10)
5         right (5)
6     right (90)


Run this program. Can you explain why this happens?

1 from turtle import *
2 def petale():
3     for j in range (2):
4         for k in range (18):
5             forward (10)
6             right (5)
7         right (90)
8 for i in range (18):
9     petale()
10   right (20)

The reserved word def (line 2) is used to define a function that groups together a number of instructions to be executed. In this case, it's the drawing of a "petal". The advantage is that, from the main program starting at the red line (line 8), you can call this function as many times as you like. This makes for much lighter programs.

Brain teaser: can you demonstrate why the rosette closes correctly?

Tiny houses

This program is taken from the excellent book "L'informatique simplement, concepts de programmation" by Juraj Hromkovič and Tobias Kohn, published by Éditions LEP (Switzerland).

After running this program, what observation can you make by comparing it with the Python startup exercise on the square?

01 from turtle import *
02 def tiny_house():
03     for i in range (4):
04         forward (50)
05         right (90)
06     left (60)
07     forward (50)
08     right (120)
09     forward (50)
10     left (60)
11 how_many = int(input("How many houses do you need to draw? "))
12 up()
13 backward(300)
14 down()
15 for i in range (how_many):
16     tiny_house()

The principle is the same as for the previous program, concerning the use of a function. The program starts reading at the red line (line 11), which represents the start of the main program.

Compared with the square program, the request for the number of maisonnettes is made in a different window from that of the drawing. This is the program execution window, which will be used in the non-graphical programs of the sequel.

You'll also notice some previously unknown instructions... Please see below for an explanation.

Now it's your turn!

Fly now with your own wings, draw a boat, a rosette of 12, 18 or 36 parallelograms, regular polygons whose number of sides you have asked the user to specify... Take inspiration from drawings on the Internet...

Here are a number of instructions you can use in graphic mode. You can now perform various exercises using these instructions.

reset()Deletes everything that has already been drawn
goto(x,y)Goes to the x and y coordinates of the screen
forward(distance)Moves forward by given distance in pixels
backward(distance)Goes back by given distance in pixels
up()Raise pencil, turtle will move forward without drawing
down()Lowers pen to draw (default state)
color(couleur)color can be a predefined string ("red", "blue",...), RGB,...
left(angle)Turn on the spot to the left by angle degrees
right(angle)Turns in place to the right by angle degrees
width(épaisseur)Selects line thickness in pixels
write(texte)Writes the text string (between " ") to the graphics screen

At this stage, it's essential that you create your own rosette programs, drawings, etc., in order to master the layout of Python programs.

Enjoy the discovery and the fun.