This is an archive copy of wikilivres.ca as of 18 February 2017 (from the WikiTeam dump). The original site is closed.
Page:Swinnen - Apprendre Python, 2010.djvu/45: Difference between revisions
imported>Zephyrus |
imported>Zephyrus o |
||
| (2 intermediate revisions by the same user not shown) | |||
| Page body (to be transcluded): | Page body (to be transcluded): | ||
| Line 4: | Line 4: | ||
{|style="background:whitesmoke; color:darkblue; padding:2%;" | {|style="background:whitesmoke; color:darkblue; padding:2%;" | ||
|width="40%;"| | |width="40%;"| | ||
<poem>>>> a = 20 | <poem> | ||
>>> a = 20 | |||
>>> if (a > 100) : | >>> if (a > 100) : | ||
'''...''' print « a dépasse la centaine » | |||
'''...''' else : | |||
'''...''' print « a ne dépasse pas cent » | |||
'''...''' | |||
</poem> | |||
|} | |} | ||
</div> | </div> | ||
| Line 32: | Line 34: | ||
>>> a = 0 | >>> a = 0 | ||
>>> if a > 0 : | >>> if a > 0 : | ||
'''...''' print « a est positif » | |||
'''...''' elif a < 0 : | |||
'''...''' print « a est négatif » | |||
'''...''' else : | |||
'''...''' print « a est nul » | |||
'''...''' | |||
</poem> | </poem> | ||
|} | |} | ||
</div> | </div> | ||
{{st|'''Opérateurs de comparaison'''}} | {{st|'''Opérateurs de comparaison'''}} | ||
La condition évaluée après l’instruction if peut contenir les '''''opérateurs de comparaison''''' suivants : | La condition évaluée après l’instruction if peut contenir les '''''opérateurs de comparaison''''' suivants : | ||
<div style="float:center; margin-left:auto; margin-right:auto; padding: | <div style="float:center; margin-left:auto; margin-right:auto; padding:0%; font-size:85%; width:550px; background:whitesmoke; color:darkblue;"> | ||
{|style="background:whitesmoke; color:darkblue; padding:2%;" | {|style="background:whitesmoke; color:darkblue; padding:2%;" | ||
|width="40%;"| | |width="40%;"| | ||
| Line 64: | Line 63: | ||
'''Exemple : ''' | '''Exemple : ''' | ||
<div style="float:center; margin-left:auto; margin-right:auto; padding:2%; font-size:85%; width:550px; background:whitesmoke; color:darkblue;"> | <div style="float:center; margin-left:auto; margin-right:auto; padding:2%; font-size:85%; width:550px; background:whitesmoke; color:darkblue;"> | ||
{|style="background:whitesmoke; color:darkblue; padding: | {|style="background:whitesmoke; color:darkblue; padding:0%;" | ||
|width="40%;"| | |width="40%;"| | ||
<poem> | <poem> | ||
>>> a = 7 | >>> a = 7 | ||
>>> if (a % 2 == 0) : | >>> if (a % 2 == 0) : | ||
'''...''' print « a est pair » | |||
'''...''' print « parce que le reste de sa division par 2 est nul » | |||
'''...''' else : | |||
'''...''' print « a est impair » | |||
'''...''' | |||
</poem> | </poem> | ||
|} | |} | ||
</div> | </div> | ||
Latest revision as of 01:34, 17 December 2011
Recommencez encore, en ajoutant deux lignes comme indiqué ci-dessous. Veillez bien à ce que la quatrième ligne débute tout à fait à gauche (pas d’indentation), mais que la cinquième soit à nouveau indentée (de préférence avec un retrait identique à celui de la troisième) :
|
>>> a = 20 |
Frappez <Enter> encore une fois. Le programme s’exécute, et affiche cette fois :
|
a ne dépasse pas cent |
Comme vous l’aurez certainement déjà compris, l’instruction else ( « sinon », en anglais) permet de programmer une exécution alternative, dans laquelle le programme doit choisir entre deux possibilités. On peut faire mieux encore en utilisant aussi l’instruction elif (contraction de « else if » ) :
|
>>> a = 0 |
La condition évaluée après l’instruction if peut contenir les opérateurs de comparaison suivants :
|
x == y # x est égal à y |
Exemple :
|
>>> a = 7 |