How is the logical processing being done for strings like 'Dog' and 'Cat'

Benjamin musiccomposition at gmail.com
Mon Oct 20 22:09:12 EDT 2008


On Oct 20, 8:41 pm, Sumitava Mukherjee <sm... at cognobytes.com> wrote:
> Hi all,
> I am a novice programmer in Python.
> Please could you explain me the results (regarding logical operators).
>
> I get this:
>
> >>> print bool('God' and 'Devil')
>
> True
>
> [This is ok because (any) string is True, so; (True and True) gives
> True]
>
> >>> print('God' and 'Devil')
>
> Devil
>
> [This is what I don't get ]
> and for that matter,I also checked out this:
>
> >>> 01 and 10
>
> 10
>
> What is python doing when we type in ('God' and 'Devil') or key in (01
> and 10) ?

This is an interesting property of python's logical operators. They
don't have to return a boolean object. (It's documented here:
http://docs.python.org/reference/expressions.html#boolean-operations)




More information about the Python-list mailing list