boolean - Python True/False for the following code -
how evaluated python code:
not not true or false , not true as of myself, have 2 guesses:
method 1:
step code
1 not not true or false , not true
2 not false or false , false
3 true or false , false
4 true , false
5 false
method 2:
step code
1 not not true or false , not true
2 not false or false , false
3 true or false
4 true
from table of python's operators precedence:
orhas lower precedence thanandin turn has lower precedence- than
not
according that:
not not true or false , not true is equivalent to
((not (not true)) or (false , (not true))) edit: noticed martijn pieters in comment below, worth mentionning python has short-circuits operators. means and , or guaranteed evaluated left-to-right and that:
- if left-term of
ortrueright term never evaluated (as result oftrue or whatevertruein boolean logic) - if left-term of
andfalseright term never evaluated (as result offalse , whateverfalsein boolean logic)
so, given example:
the first step evaluate
not truefalse:((not (not true)) or (false , (not true))) ^^^^^^^^ falsethen
not falseevaluatedtrue:((not false ) or (false , (not true))) ^^^^^^^^^^^^^^ trueas no have
true or ...python stops evaluation resulttrue:( true or (false , (not true))) ^^^^^^^^^^^^^^ ...................... true (right-hand side ignored)
Comments
Post a Comment