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:
or
has lower precedence thanand
in 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
or
true
right term never evaluated (as result oftrue or whatever
true
in boolean logic) - if left-term of
and
false
right term never evaluated (as result offalse , whatever
false
in boolean logic)
so, given example:
the first step evaluate
not true
false
:((not (not true)) or (false , (not true))) ^^^^^^^^ false
then
not false
evaluatedtrue
:((not false ) or (false , (not true))) ^^^^^^^^^^^^^^ true
as no have
true or ...
python stops evaluation resulttrue
:( true or (false , (not true))) ^^^^^^^^^^^^^^ ...................... true (right-hand side ignored)
Comments
Post a Comment