Truth value of a string in python -
if <boolean> : #
boolean has either true or false.
then why
if "poi": print "yes"
output: yes
i didn't why yes printing , since "poi" nether true or false.
python best evaluate "truthiness" of expression when boolean value needed expression.
the rule strings empty string considered false
, non-empty string considered true
. same rule imposed on other containers, empty dictionary or list considered false
, dictionary or list 1 or more entries considered true
.
the none
object considered false.
a numerical value of 0
considered false (although string value of '0'
considered true).
all other expressions considered true
.
details (including how user-defined types can specify truthiness) can found here: http://docs.python.org/release/2.5.2/lib/truth.html.
Comments
Post a Comment