Archive pour le mot-clef ‘python’

Initiation à la programmation avec Python

Tuesday 13 March 2012

J’ai récemment eu besoin de concevoir un petit programme pour initier des débutants à la programmation, ceci dans le cadre des “journées découvertes” organisées par mon école.

Une introduction à la programmation

Le programme devait respecter les points suivants :

  • Il devait être graphique et interactif, afin de permettre de visualiser instantanement les actions du code ;
  • Il devait utiliser un langage de haut niveau, afin de ne pas avoir besoin d’introduire de concepts “compliqués” ;
  • Il devait être en français afin d’être plus accèssible à tout le monde.

Il a finalement été décidé d’implémenter une version Python du langage LOGO (une tortue qui se déplace à l’écran en suivant des instructions simples).

Python nous a paru un choix évident car c’est un langage de très haut niveau, donc facilement comprehensible par les débutants. Il dispose de plus d’une syntaxe claire et de peu de contraintes de typage.

Le projet

Ce que j’ai réalisé est principalement une interface pour appeler des méthodes graphiques et un “wrapper” qui permet au programmeur de simplement mettre ses instructions dans un fichier sans s’occuper de l’environnement. Un autre avantage de Python ici est de pouvoir lancer le programme avec un interpreteur, et ainsi pouvoir jouer avec le programme de manière interactive.


Les fonctions fournies sont très simples et permettent d’aller en avant, de tourner, de changer la couleur et la taille du pinceau.

Par exemple, il est possible de dessiner un carré avec le code suivant :

for i in range(4):
    avancer(100)
    tourner_gauche(90)

Dessiner une spirale est également assez simple, le code suivant change aussi la couleur :

def spiral():
    angle = 2
    n = 3500

    for i in range(n):
        # calcule une nouvelle couleur
        c = int(i * 255. / n)
        changer_couleur((100, c, 50))

        # trace la ligne et change légerement l'angle
        avancer(2)
        tourner_gauche(angle)
        angle *= 0.9995

spiral()

Toutes ces instructions sont bien sur également disponible en anglais et il est possible de les traduire via un fichier d’alias dans n’importe quelle langue.

Malgré l’apparente simplicité de ce programme il est possible d’apprendre toutes les notions basiques de programmation, des variables à la récursion. Notre programme pour le petit TP (1h30) que nous organisions était le suivant :

  1. Qu’est-ce que la programmation ? Pourquoi programmer ?
  2. Variables
  3. Fonctions
  4. Boucles

Triangles de Sierpinski

Le projet est téléchargeable sur GitHub.

Conclusion

Les étudiants ayant participé à ce TP, bien que n’ayant pas ou peu programmé avant, on rapidement réussi à dessiner des formes plus ou moins complexes à l’écran et ont pour la majorité apprécié l’exercice.

Le combo Python/programmation graphique a plutôt bien fonctionné pour une initiation, en espérant que cela ait donné l’envie à certains d’en apprendre plus sur la programmation.

Introducing beginners to programming with Python.

Monday 12 March 2012

I was recently asked about a way to introduce beginners to programming. This was to be used in my school during “discovery days”, when high-school students come to visit and see what await them.

An introduction to programming

After some discussions about what this introduction should look like, we had come to the following points :

  • It should be graphical and interactive, so to provide an immediate way of visualising what you’re doing ;
  • It should use a high level language, so there is no need of introducing “complicated” low-level concepts ;
  • It should be easily translatable, because it is a French school and it is easier for some students who don’t speak english.

We finally decided to go with an implementation of the LOGO language (a pointer on the screen that you can move with simple instructions) written in Python.

Python was an obvious choice, because it is a very high-level language and so very easy to understand for beginners. A simple and readable syntax and no need to type variables.

The project

So, what I have done is basically an interface for calling graphical methods and a wrapper so that you can simply put your instruction in a file and don’t bother with the environment around. Another advantage of Python here is that you can launch the program with an interpreter and play, in an interactive manner, with the program.

The functions provided are very simple and allow to go forward, turn, change the brush color and size. A file is also provided with translations of all theses functions names in French, and it is easy to translate toward another language.

For example, drawing a square could look like :

for i in range(4):
    forward(100)
    turn_left(90)

Drawing a spiral is also quite simple, the following code also change the color :

def spiral():
    angle = 2
    n = 3500

    for i in range(n):
        # compute a new color
        c = int(i * 255. / n)
        change_color((100, c, 50))

        # draw the line and slightly change the angle
        forward(2)
        turn_left(angle)
        angle *= 0.9995

spiral()

We also provided a simplified, one-button, version of the PyScripter IDE. The goal was really that the students were not bothered by technicalities.

But despite this apparent simplicity it is possible to learn all the programming basics, from variables to recursion. What we taught during the small practical we organized (1h30) was the following :

  1. What is programming? Why program?
  2. Variables
  3. Functions
  4. Loops

Sierpinski triangles

You can download the project on GitHub.

Conclusion

The students that participated in theses practicals had no experience in programming but quickly managed to draw simples then more complicated shapes on the screen. Most of them were enjoying the activity and at the end of each of the three practicals about half of the students asked to get their code send to them by email.

So, it was good to see that this project was appreciated and maybe gave to some the will to learn more.