Chapitre 1 : Programmation Python
Pr. Samira LAFRAXO
Université Ibnou Zohr, Faculté des Sciences Agadir
Centre d’Excellence IT
[Link]@[Link]
Filière: ADIA / IISE - S6
Module: Introduction au Machine Learning / IA
March 6, 2024
Pr. Lafraxo Programmation Python March 6, 2024 1 / 55
Plan du Chapitre
1 Introduction à Python
2 Structures de contrôle
3 Conteneurs standards
4 Fonctions
5 Modules
6 Programmation orientée objet
Pr. Lafraxo Programmation Python March 6, 2024 2 / 55
Plan du Chapitre
1 Introduction à Python
2 Structures de contrôle
3 Conteneurs standards
4 Fonctions
5 Modules
6 Programmation orientée objet
Pr. Lafraxo Programmation Python March 6, 2024 3 / 55
Python
Python est un langage de script:
Développé depuis 1989 par Guido van Rossum
Orienté objet (mais peut être utilisé quasi non-”objectivement”)
Open Source et gratuit
Portable
Simple à comprendre/ écrire
Versions:
Version 1.0 : 1990
Version 2.0 : 2000
Version 3.0 : 2008
Pr. Lafraxo Programmation Python March 6, 2024 4 / 55
Python
Pr. Lafraxo Programmation Python March 6, 2024 5 / 55
Python
Avantages : Interpréteur permettant de tester n’importe quel petit bout
de code, compilation transparentes
Inconvénients : peut être lent
Que peut-on faire avec Python ?
Développement Web: Django, Pyramid, Zope, Plone,...
Calcul Scientifique: Numpy, Scipy, sage, ...
Représentation graphique: gnuplot, matplotlib, VTK, ...
GUIs: TkInter, PyGtk, PyQt,...
Manipulation d’images: Pillow, OpenCV-Python,...
Bases de données: MySQL, PostgrSQL, Oracle, ...
Pr. Lafraxo Programmation Python March 6, 2024 6 / 55
Règles d’écritures
La casse est significative
Le typage des variables est dynamique
Une instruction doit commencer en première colonne
L’indentation est obligatoire pour marquer les blocks
Si une instruction dépasse la taille d’une ligne, il est possible soit
d’écrire un caractère de continuation ou de mettre l’instruction entre
parenthèses
Exemples
Pr. Lafraxo Programmation Python March 6, 2024 7 / 55
Opérations de base
Affectation
Pr. Lafraxo Programmation Python March 6, 2024 8 / 55
Opérations de base
Pr. Lafraxo Programmation Python March 6, 2024 9 / 55
Chaines de caractères
Encadrées par des apostrophes (’ ’) ou des guillemets (” ”)
Les opérateurs sur les chaı̂nes permettent de concaténer, filtrer ou
extraire des parties des chaı̂nes de caractères entres elles.
Pr. Lafraxo Programmation Python March 6, 2024 10 / 55
Chaines de caractères
Exemple
Pr. Lafraxo Programmation Python March 6, 2024 11 / 55
Chaines de caractères
Exemple
Pr. Lafraxo Programmation Python March 6, 2024 12 / 55
Conversion entre types
La conversion de types permet de passer d’un type à l’autre.
La règle générale est de se servir du nom du type avec des
parenthèses.
Pr. Lafraxo Programmation Python March 6, 2024 13 / 55
print() / input()
print(msg) : imprime à l’écran une chaı̂ne de caractères donnée en
argument
input(msg): permet de récupérer une chaı̂ne de caractères tapée par
l’utilisateur (retourne une chaine)
Exemples :
Pr. Lafraxo Programmation Python March 6, 2024 14 / 55
Plan du Chapitre
1 Introduction à Python
2 Structures de contrôle
Structures alternatives
Structures itératives
3 Conteneurs standards
4 Fonctions
5 Modules
6 Programmation orientée objet
Pr. Lafraxo Programmation Python March 6, 2024 15 / 55
Les blocs
Pr. Lafraxo Programmation Python March 6, 2024 16 / 55
Les structures conditionnelles
Pr. Lafraxo Programmation Python March 6, 2024 17 / 55
Les structures conditionnelles
Pr. Lafraxo Programmation Python March 6, 2024 18 / 55
Conditions
Pr. Lafraxo Programmation Python March 6, 2024 19 / 55
Boucle for
Pr. Lafraxo Programmation Python March 6, 2024 20 / 55
Boucle for
Pr. Lafraxo Programmation Python March 6, 2024 21 / 55
Boucle while
Pr. Lafraxo Programmation Python March 6, 2024 22 / 55
Boucle while
Pr. Lafraxo Programmation Python March 6, 2024 23 / 55
Plan du Chapitre
1 Introduction à Python
2 Structures de contrôle
3 Conteneurs standards
Listes
Tuples
Ensembles
Dictionnaires
4 Fonctions
5 Modules
6 Programmation orientée objet
Pr. Lafraxo Programmation Python March 6, 2024 24 / 55
Listes
Pr. Lafraxo Programmation Python March 6, 2024 25 / 55
Manipulations des Listes
Pr. Lafraxo Programmation Python March 6, 2024 26 / 55
Manipulations des Listes
Pr. Lafraxo Programmation Python March 6, 2024 27 / 55
Exemples
Pr. Lafraxo Programmation Python March 6, 2024 28 / 55
Exemples
Pr. Lafraxo Programmation Python March 6, 2024 29 / 55
Exemples
Pr. Lafraxo Programmation Python March 6, 2024 30 / 55
Suppression d’un élément
Pr. Lafraxo Programmation Python March 6, 2024 31 / 55
Copie d’une liste
Pr. Lafraxo Programmation Python March 6, 2024 32 / 55
Tuple
Pr. Lafraxo Programmation Python March 6, 2024 33 / 55
Tuples
Pr. Lafraxo Programmation Python March 6, 2024 34 / 55
Ensembles
Pr. Lafraxo Programmation Python March 6, 2024 35 / 55
Opérations
Pr. Lafraxo Programmation Python March 6, 2024 36 / 55
Exemples
Pr. Lafraxo Programmation Python March 6, 2024 37 / 55
Dictionnaires
Pr. Lafraxo Programmation Python March 6, 2024 38 / 55
Accès à un élément
Pr. Lafraxo Programmation Python March 6, 2024 39 / 55
Principales méthodes
Pr. Lafraxo Programmation Python March 6, 2024 40 / 55
Exemple
Pr. Lafraxo Programmation Python March 6, 2024 41 / 55
Exemple
Pr. Lafraxo Programmation Python March 6, 2024 42 / 55
Plan du Chapitre
1 Introduction à Python
2 Structures de contrôle
3 Conteneurs standards
4 Fonctions
Définition
Arguments d’une fonction
Fonctions variadiques
Portée d’une variable
5 Modules
6 Programmation orientée objet
Pr. Lafraxo Programmation Python March 6, 2024 43 / 55
Définition
Pr. Lafraxo Programmation Python March 6, 2024 44 / 55
Exemples
Pr. Lafraxo Programmation Python March 6, 2024 45 / 55
Argument d’une fonction
Pr. Lafraxo Programmation Python March 6, 2024 46 / 55
Arguments nommés
Pr. Lafraxo Programmation Python March 6, 2024 47 / 55
Fonction Variadique
Pr. Lafraxo Programmation Python March 6, 2024 48 / 55
Fonction Variadique
Pr. Lafraxo Programmation Python March 6, 2024 49 / 55
Fonction Variadique
Pr. Lafraxo Programmation Python March 6, 2024 50 / 55
Exemple
Pr. Lafraxo Programmation Python March 6, 2024 51 / 55
Variable locale / globale
Pr. Lafraxo Programmation Python March 6, 2024 52 / 55
Variable locale / globale
Pr. Lafraxo Programmation Python March 6, 2024 53 / 55
Variable locale / globale
Pr. Lafraxo Programmation Python March 6, 2024 54 / 55
Enumerate() / zip()
Pr. Lafraxo Programmation Python March 6, 2024 55 / 55