>>> 'a' == ('a' or 'b') # 'a' = True, donc ('a' or 'b') = 'a' sans avoir à évaluer 'b'. Ce qui revient à : 'a' == 'a'
Ligne 145 ⟶ 133 :
False
>>> 'a' == ('a' and 'b') # 'a' = True et 'b' = True, donc ('a' and 'b') = 'b' (dernière expression évaluée). Ce qui revient à : 'a' == 'b'
False
Ligne 151 ⟶ 139 :
True
</source>
So Python was really doing its job when it gave those apparently bogus results. As mentioned previously, the important thing is to recognize what value your boolean expression will return when it is evaluated, because it isn't always obvious.
Going back to those initial expressions, this is how you would write them out so they behaved in a way that you want:
>>> 'a' == 'a' or 'a' == 'b'
True
>>> 'b' == 'a' or 'b' == 'b'
True
>>> 'a' == 'a' and 'a' == 'b'
False
>>> 'b' == 'a' and 'b' == 'b'
False
When these comparisons are evaluated they return truth values in terms of True or False, not strings, so we get the proper results.