Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #79530

Re: Truthiness

From Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de>
Subject Re: Truthiness
Date 2014-10-23 16:49 +0200
References <61f0b04c-b827-4c03-b55d-4d5bd85b56f6@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.15111.1414075725.18130.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Truthiness Simon Kennedy <sffjunkie@gmail.com> - 2014-10-23 07:30 -0700
  Re: Truthiness Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-23 08:45 -0600
    Re: Truthiness Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-24 11:42 +1100
  Re: Truthiness Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2014-10-23 16:47 +0200
    Re: Truthiness Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2014-10-23 17:01 +0200
    Re: Truthiness Denis McMahon <denismfmcmahon@gmail.com> - 2014-10-23 16:07 +0000
  Re: Truthiness Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2014-10-23 16:49 +0200
  Re: Truthiness Skip Montanaro <skip.montanaro@gmail.com> - 2014-10-23 09:51 -0500
  Re: Truthiness Simon Kennedy <sffjunkie@gmail.com> - 2014-10-23 07:56 -0700
    Re: Truthiness random832@fastmail.us - 2014-10-23 11:24 -0400

csiph-web