Truthiness

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Thu Oct 23 10:49:05 EDT 2014


On 10/23/2014 04:30 PM, Simon Kennedy wrote:
> Just out of academic interest, is there somewhere in the Python docs where the following is explained?
>

https://docs.python.org/3/library/stdtypes.html#truth-value-testing

>>>> 3 == True
> False

as opposed to:

https://docs.python.org/3/library/stdtypes.html#comparisons

>>>> if 3:
> 	print("It's Twue")
> 	
> It's Twue
>
> i.e. in the if statement 3 is True but not in the first
>

Here is the misunderstanding: it is not true that 3 is True in the if 
example, but it's evaluated as True in a boolean context. Illustration:

 >>> 3 == True
False

 >>> bool(3) == True
True

 >>> bool(3) is True
True

If you combine if and a comparison, it behaves like in your first example:

 >>>
if 3 == True:
     print('True')
else:
     print('False')

False





More information about the Python-list mailing list