Python – Boucles

Structure conditionnelle if

StructureExemple
if condition :
instruction
elif condition :
instruction
else :
instruction
if a>0 :
print(“positif”)
elif a<0 :
print(“négatif”)
else :
print(“nul”)

Équivalent condition if : match

Structureexemple
Variable
match variable :
    case test :
instruction
    case test :
instruction
    case test :
instruction

case _: équivaut à else
choix = 2
match choix : # On va comparer la variable choix à plusieurs choix possibles
   case 0 : # choix vaut 0
       print(« Vous avez choisi zéro. »)
   case 1 : # choix vaut 1
       print(« Vous avez choisi un. »)
   case 2 : # choix vaut 2
       print(« Vous avez choisi deux. »)
   case _: # choix vaut autre chose
       print(« Vous avez choisi autre chose. »)

Vous avez choisi deux.

La boucle while

Structureexemple
While condition :
           instruction 1
           instruction 2
           instruction 3
nb = 7 
i = 1
while i <= 10 :
     print(f »{i} * {nb} = {i * nb} »)
     i += 1


Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *