Programmation en Python
Chapitre 2: Les structures de contrôle
Dr. Gomis
L2 MPI & PC - UFR SATIC/ UADB
18 novembre 2022
Programmation en Python 2
Plan
Expression conditionnelle
Structures conditionnelles
Gestion des exceptions
Structures itératives
Références
Dr. Gomis Programmation en Python 2
Programmation en Python 3
Expression conditionnelle
Définition
Une expression conditionnelle, encore appelée expression logique ou
expression booléenne, est une expression dont la valeur est soit VRAI
soit FAUX.
a , b , c , d = 1 2 , 1 9 , 2 0 , 40
cond1 = ( a < b ) # exp . s i m p l e
cond2 = ( a < b ) and ( c > d ) # exp . c o m p l e x e
p r i n t ( cond1 , cond2 ) # True F a l s e
Dr. Gomis Programmation en Python 3
Programmation en Python 4
Expression conditionnelle
Définition
Une expression conditionnelle, encore appelée expression logique ou
expression booléenne, est une expression dont la valeur est soit VRAI
soit FAUX.
ch1 = " w o r l d "
ch2 = " H e l l o w o r l d "
cond3 = ch1 i n ch2
cond4 = ch1 == ch2
# On a a u s s i != <= >= < >
Dr. Gomis Programmation en Python 4
Programmation en Python 5
Expression conditionnelle
Définition
Une expression conditionnelle, encore appelée expression logique ou
expression booléenne, est une expression dont la valeur est soit VRAI
soit FAUX.
p r i n t ( True and False ) # False
p r i n t ( n o t False ) # True
p r i n t ( True o r False ) # True
Dr. Gomis Programmation en Python 5
Programmation en Python 6
Structures conditionnelles
Structure conditionnelle stricte
"Si ... alors” ⇒ if ... :
i f temperature < 2 5 :
p r i n t ( " Promenade " )
Dr. Gomis Programmation en Python 6
Programmation en Python 7
Structures conditionnelles
Structure conditionnelle stricte
condition = temperature < 25
i f condition :
p r i n t ( " Promenade " )
Dr. Gomis Programmation en Python 7
Programmation en Python 8
Structures conditionnelles
Structure conditionnelle stricte
condition1 = temperature < 25
condition2 = n o t condition1
i f condition1 :
p r i n t ( " Promenade " )
i f condition2 :
p r i n t ( " R e g a r d e r un f i l m " )
NB : La condition 2 est exactement le contraire de la condition 1
Dr. Gomis Programmation en Python 8
Programmation en Python 9
Structures conditionnelles
Structure conditionnelle alternative
"Si ... alors ... Sinon ...” ⇒ if ... : ... else : ...
i f temperature < 2 5 :
p r i n t ( " Promenade ! " )
else :
p r i n t ( " R e g a r d e r un f i l m " )
Dr. Gomis Programmation en Python 9
Programmation en Python 10
Structures conditionnelles
Bloc d’instructions
Pour délimiter un bloc ⇒
# L ' i n d e n t a t i o n (dé calage ) est o b l i g a t o i r e
# c a r e l l e i n f l u e n c e l ' ex é c u t i o n du code
i f temperature < 2 5 :
code = 1
p r i n t ( " Beau temps " )
else :
code = 0
p r i n t ( " m a u v a i s temps " )
Dr. Gomis Programmation en Python 10
Programmation en Python 11
Structures conditionnelles
Structures conditionnelles imbriquées
"Sinon si ... alors” ⇒ elif
i f 10 < temperature < 2 5 :
p r i n t ( " Promenade " )
e l i f temperature <= 1 0 :
p r i n t ( " S o r t i r en v i l l e " )
else :
p r i n t ( " Rester chez s o i " )
Dr. Gomis Programmation en Python 11
Programmation en Python 12
Gestion des exceptions
Dr. Gomis Programmation en Python 12
Programmation en Python 13
Gestion des exceptions
try ... except
Un bloc try-except peut surveiller toute exception ou seulement une
certaine exception
ch = i n p u t ( " Donner une c h a i n e : " )
try :
p r i n t ( " La c o n v e r s i o n donne : " , i n t ( ch ) )
except :
p r i n t ( " I m p o s s i b l e de c o n v e r t i r " )
Dr. Gomis Programmation en Python 13
Programmation en Python 14
Gestion des exceptions
try ... except ... else
Un bloc try-except peut surveiller toute exception ou seulement une
certaine exception
ch = i n p u t ( " Donner une c h a i n e : " )
try :
entier = i n t ( ch )
except :
p r i n t ( " I m p o s s i b l e de c o n v e r t i r " )
else :
p r i n t ( " La c o n v e r s i o n donne : " , entier )
Dr. Gomis Programmation en Python 14
Programmation en Python 15
Gestion des exceptions
try ... except ... else ... finally
ch = i n p u t ( " Donner une c h a i n e : " )
try :
entier = i n t ( ch )
except :
p r i n t ( " I m p o s s i b l e de c o n v e r t i r " )
else :
p r i n t ( " La c o n v e r s i o n donne : " , entier )
finally :
p r i n t ( " F i n du programme " )
Dr. Gomis Programmation en Python 15
Programmation en Python 16
Gestion des exceptions
Plusieurs exceptions différentes
try :
note = f l o a t ( i n p u t ( " V o t r e n o t e : " ) )
assert 0 <= note <= 20
e x c e p t AssertionError :
p r i n t ( " Note i n c o r r e c t e " )
e x c e p t ValueError :
p r i n t ( " E n t r e r un nombre " )
else :
p r i n t ( "Ok" )
finally :
p r i n t ( " F i n du programme " )
Dr. Gomis Programmation en Python 16
Programmation en Python 17
Structures itératives
La boucle conditionnelle
Tant que ... faire ⇒ while
# Exemple
x = 0
while x < 10:
print (x)
x += 1
Dr. Gomis Programmation en Python 17
Programmation en Python 18
Structures itératives
La boucle déterministe
Pour chacun ... dans ... faire ⇒ for ... in
f o r i in range (10) :
p r i n t ( " bonjour " )
Dr. Gomis Programmation en Python 18
Programmation en Python 19
Structures itératives
La boucle déterministe
Pour chacun ... dans ... faire ⇒ for ... in
# Dé c r é menter
f o r i i n r a n g e ( 1 0 , 0 , −1) :
p r i n t ( " bonjour " )
Dr. Gomis Programmation en Python 19
Programmation en Python 20
Structures itératives
Saut d’instructions
"Passer au suivant” ⇒ continue
ch = " c e c i e s t une c h a i n e de c a r a c t è r e s "
f o r caractere i n ch :
i f caractere == ' a ' :
continue
p r i n t ( caractere )
# Une c h a i n e de c a r a c t è r e s e s t une s é q u e n c e
Dr. Gomis Programmation en Python 20
Programmation en Python 21
Structures itératives
Interruption de boucle
Arrêter la boucle ⇒ break
f o r nombre i n r a n g e ( 1 , 100) :
p r i n t ( nombre )
i f nombre % 3 == 0 :
break
# r a n g e ( d é but , f i n , p a s )
Dr. Gomis Programmation en Python 21
Programmation en Python 22
Références
Dr. Gomis Programmation en Python 22
Programmation en Python 23
Références
Fin du chapitre
Merci de votre attention
Dr. Gomis Programmation en Python 23