Kaushal Shriyan said unto the world upon 06/04/06 08:06 AM:
> Hi
>
> I am referring to http://www.ibiblio.org/obp/thinkCSpy/chap04.htm
> about Logical operators
>
> I didnot understood
>
>
>>>> x = 5
>>>> x and 1
>
> 1
>
>>>> y = 0
>>>> y and 1
>
> 0
>
> How 5 and 1 means 1 and 0 and 1 means 0
>
> Thanks
>
> Regards
>
> Kaushal
Kaushal,
as Jason pointed out, any non-zero number evaluates to True. Also, any
non-empty list, string, dict, etc. Witness:
>>> bool(6)
True
>>> bool(0)
False
>>> bool("non-empty string")
True
>>> bool(' ')
True
>>> bool('')
False
The other part of the puzzle is that 'and' and 'or' are
"short-circuit" operators. 'or' works like this: return the first
value flanking the or if that evaluates to True. Otherwise return the
second value:
>>> 42 or 0
42
>>> 0 or 42
42
>>> 7 or 42
7
>>> 42 or 7
42
>>> 0 or []
[]
>>> [] or 0
0
>>>
'and' works similarly. It returns the first value if that evaluates to
False. Otherwise, it returns the second:
>>> 42 and 7
7
>>> 7 and 42
42
>>> 0 and []
0
>>> [] and 0
[]
>>>
HTH,
Brian vdB
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor