Python Basic Doubt

Chris Angelico rosuav at gmail.com
Sat Aug 10 23:20:03 EDT 2013


On Sun, Aug 11, 2013 at 4:09 AM, Krishnan Shankar
<i.am.songoku at gmail.com> wrote:
> i.e. Is this code possible
>
> if a is False:
>     print 'Yes'
> if b is False:
>     print 'No'

You would use that if you want to check if a/b is the exact bool value
False. Normally you would simply spell it thus:

if not a:
    print 'Yes'
if not b:
    print 'No'

which will accept any value and interpret it as either empty (false)
or non-empty (true).

Using the equality operator here adds another level of potential confusion:

>>> 0 == False
True
>>> [] == False
False
>>> 0.0 == False
True
>>> () == False
False

whereas if you use the normal boolean conversion, those ARE all false:

>>> bool(0)
False
>>> bool([])
False
>>> bool(0.0)
False
>>> bool(())
False

ChrisA



More information about the Python-list mailing list