« Programmation Python/Booléens » : différence entre les versions

Contenu supprimé Contenu ajouté
→‎Exemple : exemple non pertinent car inutilement prématurément complexe
Ligne 104 :
 
== Note sur les opérateurs booléens ==
{{attention|<code>x == ('a' or 'b')</code> ne vérifie pas si <code>x</code> est équivalent aux caractères <code>'a'</code> ou <code>'b'</code>, mais au résultat de leur expression booléenne.}}
A common mistake for people new to programming is a misunderstanding of the way that boolean operators works, which stems from the way the python interpreter reads these expressions. For example, after initially learning about "and " and "or" statements, one might assume that the expression <code>x == ('a' or 'b')</code> would check to see if the variable <code>x</code> was equivalent to one of the strings <code>'a'</code> or <code>'b'</code>. This is not so. To see what I'm talking about, start an interactive session with the interpreter and enter the following expressions:
 
>>> 'a' == ('a' or 'b')
>>> 'b' == ('a' or 'b')
>>> 'a' == ('a' and 'b')
>>> 'b' == ('a' and 'b')
 
And this will be the unintuitive result:
 
Exemple :
>>>''' 'a' == ('a' or 'b')'''
True