christophe@pallier.org
Problem => Algorithm => Coding
Strictly speaking, to program is to explain to a computer how to carry out an algorithm (the second arrow), not to find an algorithm for a problem (first arrow).
Although “AIP” means “Atelier d’introduction à la Programmation/Python” we will try to train you also on the first stage.
Examples of Problems:
- Cooking boiled eggs
- Integer division of two numbers
- Missile Pursuit (or animal chasing a prey)
- Play a game like chess
- Recover 3D information from stereoscopic images
Note: suggested readings David Marr (1982, Vision)’s three levels of description in cognitive science.
Because computers are stupid (that is, they lack a theory of mind and cannot guess your desires and intentions), you must provide very detailed and exhaustive instructions. The choice of the programming language is relevant here.
There exist many programming languages (and also different computer architectures, e.g. parallel vs. serial)
In 2000, we decided to use Python for the Atelier d’Experimentation, because it is:
Learning to program takes time because it requires to master not only the semantic but also the syntax (=grammar) of the language.
Like learning to play an instrument, programming is a skill that you can only acquire by trying (and making lots of mistakes and correcting them!!!).
Yes you can! Let’s go!
We not use Python for this first lecture!
Scratch (http://scratch.mit.edu) was designed by the MIT media lab to teach kids how to program.
Its main interest: programs are created using a graphical interface, preventing syntactic errors (thus, you can learn the language without learning the grammar!)
One can either work online at https://scratch.mit.edu/projects/editor/?tip_bar=home or offline by downloading Scratch at
You may want to run the tutorial “Getting Started with Scratch” on the website.
In the ‘motion’ group, take the instruction ‘turn 15 degrees’ and drag it onto the ‘Scripts’ panel.
Double-Click repeatedly on the block ‘turn 15 degrees’, you should see the cat (‘sprite 1’) rotate.
In Scratch, when one double-clicks an instruction in the ‘Scripts’ panel, the computer executes it.
Drag the instruction ‘move 10 steps’ from the motion group, and add it to the bottom of the instruction ‘turn 15 degrees’. Change the value ‘10’ into ‘50’.
You have just created a block of instructions, that is, your first program or script, Bravo!
Click on the ‘pen’ group, and add ‘pen down’ at the top of the block.
Run it.
Construct the following scripts and play with them until you are sure to understand the behavior of the computer..
Computers are good at doing tasks repeatedly (as they do not get tired).
Click on the “Control” group, and try to construct the following script:
Modify the script as follows:
Tip: the condition ‘key space pressed?’ is in the ‘Sensing’ group.
This illustrates a repeat…until loop: the inner block is executed until the condition is satisfied.
Add a new sprite, and duplicate the script from sprite1. Click on the green flag. You should see the two sprites running in circles.
Remark that the scripts associated to the two sprites run in parallel (rather than sequentially).
Create a new scratch project, and change the costume of the sprite into a ball.
Then write and execute the following script.
You should see the ball bounce on the edges.
With Scratch, use the instructions “pen down” and “move” and “turn” to (a) make the cat draw a square (with sides measuring 100 steps) (b) draw an hexagon (c) draw a circle
Using the Control/Forever, make the cat turn continuously along a circle.
Using the group ‘variable’, we are going to create a variable ‘a’ and make it display continuously the x-coordinate of the ball.
The concept of variable is very important. You can think of it as a name for a object that can change (here the object is a number).
Now study the following script:
The loop is executed 100 times. Each time, the value of the variable a
is incremented by 1, and is used to compute new x
and y
coordinates where to sprite is instructed to moved to.
(x * x + y * y) < 1
.)The aim of this exercice is to recreate the Spirograph patterns.
First, check out the demo at http://www.mathplayground.com/Spiromath.html. With the help of the mathematical equations provided at http://www.mathematische-basteleien.de/spirographs.htm write a scratch program that draws these figures.
There are several examples on the scratch web site. You can study their code: