Nick the Gr33k <supp...@superhost.gr> wrote: > >but i'm doing this all day long i just dont comprehend why it works this >way. >it doesn't make any sense to me.
It's just a rule you'll have to learn. The "and" and "or" operators in Python simply do not return a boolean value. The expression "a or b" is evaluated as: if a is true then return a otherwise return b It's true that in many languages, "or" returns a Boolean value, so the result is: if a is true then return True otherwise return bool(b) Because Python lets you use arbitrary values in a Boolean context, the net result is exactly the same. However, the Python is a lot more flexible, because it lets you simulate the C ternary ?: operator. Similarly, "a and b" is evaluated as: if a is false then return a otherwise return b In a long series separated by "or", the expression is true as soon as one of the subexpressions is true. So, as a short-circuit, Python simply returns the first one that has a "true" value. So, for example, these all return 'abcd': 'abcd' or 'defg' or 'hjkl' ==> 'abcd' 0 or 'abcd' or 'defg' or 'hjkl' ==> 'abcd' 0 or None or 'abcd' or 'defg' or 'hjkl' ==> 'abcd' Similarly, "and" returns the first "false" value, or if they're all true, the last value. Why? Because it can't know whether the whole expression is true unless it looks at every value. So: 0 and 1 and 'what' ==> 0 1 and 0 and 'what' ==> 0 1 and None and 0 ==> None 1 and 1 and 'what' ==> 'what' -- Tim Roberts, t...@probo.com Providenza & Boekelheide, Inc. -- http://mail.python.org/mailman/listinfo/python-list