Truthiness

Alain Ketterlin alain at dpt-info.u-strasbg.fr
Thu Oct 23 16:47:27 CEST 2014


Simon Kennedy <sffjunkie at gmail.com> writes:

> Just out of academic interest, is there somewhere in the Python docs where the following is explained?
>
>>>> 3 == True
> False
>>>> if 3:
> 	print("It's Twue")
> 	
> It's Twue
>
> i.e. in the if statement 3 is True but not in the first

https://docs.python.org/2/reference/compound_stmts.html#the-if-statement

says: "The if statement [...] selects exactly one of the suites by
evaluating the expressions one by one until one is found to be true (see
section Boolean operations for the definition of true and false)"

and then:

https://docs.python.org/2/reference/expressions.html#booleans

says: "In the context of Boolean operations, and also when expressions
are used by control flow statements, the following values are
interpreted as false: False, None, numeric zero of all types, and empty
strings and containers (including strings, tuples, lists, dictionaries,
sets and frozensets). All other values are interpreted as true."

(links are to the 2.7 version of the reference manual, I think not much
has changed in 3.* versions.)

-- Alain.



More information about the Python-list mailing list