Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| dev:python:core:structures_de_controle [2025/07/27 16:23] – supprimée - modification externe (Date inconnue) 127.0.0.1 | dev:python:core:structures_de_controle [2025/07/27 16:44] (Version actuelle) – yoann | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | {{tag> | ||
| + | ====== Structures de contrôle ====== | ||
| + | |||
| + | ===== Structures conditionnelles ===== | ||
| + | |||
| + | ==== if ==== | ||
| + | <code python> | ||
| + | if a_condition: | ||
| + | # bloc a exécuter lorsque a_condition est vraie | ||
| + | elif other_condition: | ||
| + | # bloc a exécuter lorsque a condition est fausse | ||
| + | # et other_condition est vraie | ||
| + | else: | ||
| + | # bloc a exécuter si les conditions précédentes sont fausses. | ||
| + | </ | ||
| + | |||
| + | ==== match et case ==== | ||
| + | |||
| + | Pour les versions **Python > 3.10**, l' | ||
| + | |||
| + | <code python> | ||
| + | x = input(" | ||
| + | |||
| + | match x: | ||
| + | case 1: | ||
| + | print(" | ||
| + | case 2: | ||
| + | print(" | ||
| + | case 3: | ||
| + | print(" | ||
| + | case 4: | ||
| + | print(" | ||
| + | case _: | ||
| + | print(" | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Equivalents switch-case ==== | ||
| + | |||
| + | Pour les version de Python plus anciennes, **Python < 3.10**, la structure de contrôle **match case** ou switch ... case n' | ||
| + | |||
| + | <code python> | ||
| + | if n == 0: | ||
| + | print "n vaut zéro\n" | ||
| + | elif n== 1 or n == 9 or n == 4: | ||
| + | print "n est un carré\n" | ||
| + | elif n == 2: | ||
| + | print "n est pair\n" | ||
| + | elif n== 3 or n == 5 or n == 7: | ||
| + | print "n est premier\n | ||
| + | </ | ||
| + | |||
| + | La façon élégante et pythonienne consiste à s' | ||
| + | |||
| + | <code python> | ||
| + | options = {0 : zero, | ||
| + | 1 : sqr, | ||
| + | 4 : sqr, | ||
| + | 9 : sqr, | ||
| + | 2 : even, | ||
| + | 3 : prime, | ||
| + | 5 : prime, | ||
| + | 7 : prime, | ||
| + | } | ||
| + | |||
| + | def zero(): | ||
| + | print "You typed zero.\n" | ||
| + | |||
| + | def sqr(): | ||
| + | print "n is a perfect square\n" | ||
| + | |||
| + | def even(): | ||
| + | print "n is an even number\n" | ||
| + | |||
| + | def prime(): | ||
| + | print "n is a prime number\n" | ||
| + | </ | ||
| + | |||
| + | Une fois définit il suffit | ||
| + | <code python> | ||
| + | options[num]() | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Les boucles ===== | ||
| + | |||
| + | ==== Boucle for ==== | ||
| + | |||
| + | Exécuter un bloc d' | ||
| + | |||
| + | <code python> | ||
| + | for i in range(10): | ||
| + | print i | ||
| + | </ | ||
| + | |||
| + | ==== Boucle while ==== | ||
| + | |||
| + | Exécution du bloc d' | ||
| + | |||
| + | <code python> | ||
| + | while ma_condition == True: | ||
| + | instruction 1 | ||
| + | instruction 2 | ||
| + | ... | ||
| + | instruction n | ||
| + | </ | ||
| + | |||
| + | ===== Equivalent do while ===== | ||
| + | |||
| + | La boucle do while présente dans certains langages n' | ||
| + | |||
| + | Boucle do while en C | ||
| + | <code c> | ||
| + | do | ||
| + | { | ||
| + | | ||
| + | | ||
| + | } | ||
| + | while(condition) | ||
| + | </ | ||
| + | |||
| + | Equivalent do while en Python: | ||
| + | <code python> | ||
| + | while True: | ||
| + | instruction 1 | ||
| + | instruction n | ||
| + | if not condition: | ||
| + | break | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | ===== Références ===== | ||
| + | |||
| + | * https:// | ||
| + | * https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||