python 3.x - Conditional statement with empty string -
why '' , not '123'
evaluate ''
instead of false
, not '123' , ''
evaluates false
in python 3.4.3?
the logical and/or operators stop evaluating terms (short-circuit) answer decided.
examples and
>>> '' , not '123' ''
the first 1 false, and
short-circuited , first 1 returned.
>>> not '123' , '' false
not '123'
returns false
. since false, and
short-circuited , result of not '123'
1 returned.
for same reason, following returns zero:
>>> 0 , '123' 0
and following returns []
:
>>> [] , '123' []
examples or
>>> '' or '123' '123' >>> not '123' or 'hi' 'hi' >>> '123' or 'hi' '123'
documentation
this behavior specified in the documentation where:
x or y
definedif x false, y, else x
x , y
definedif x false, x, else y
not x
definedif x false, true, else false
Comments
Post a Comment