Understanding Boolean Expressions

Dave Angel davea at davea.name
Tue Apr 16 18:57:00 EDT 2013


On 04/16/2013 06:19 PM, Bruce McGoveran wrote:
> Hello.  I am new to this group.  I've done a search for the topic about which I'm posting, and while I have found some threads that are relevant, I haven't found anything exactly on point that I can understand.  So, I'm taking the liberty of asking about something that may be obvious to many readers of this group.
>
> The relevant Python documentation reference is:  http://docs.python.org/2/reference/expressions.html#boolean-operations.
>
> I'm trying to make sense of the rules of or_test, and_test, and not_test that appear in this section.  While I understand the substance of the text in this section, it is the grammar definitions themselves that confuse me.  For example, I am not clear how an or_test can be an and_test.  Moreover, if I follow the chain of non-terminal references, I move from or_test, to and_test, to not_test, to comparison.  And when I look at the definition for comparison, I seem to be into bitwise comparisons.  I cannot explain this.
>
> Perhaps an example will help put my confusion into more concrete terms.  Suppose I write the expression if x or y in my code.  I presume this is an example of an or_test.  Beyond that, though, I'm not sure whether this maps to an and_test (the first option on the right-hand side of the rule) or to the or_test "or" and_test option (the second on the right-hand side of the rule).
>
> If people can offer some thoughts to put me in the right direction (or out of my misery), I would appreciate it.
>

It's been decades since I really had to understand a complete grammar. 
But in some ways, a fragment like you're referencing is even harder. The 
trick is to take those names "and_test" as rather arbitrary.  There's no 
token meaning "an expression containing 4 or's, 2 and's, and 1 not"  So 
sometimes the terms used are rather silly looking.

The point is you can combine 'not', 'and', 'or', and comparison 
operators like ==, >=  etc.  in some complex ways.  The grammar just 
says which of these is legal, and doesn't help you figure out the 
semantics, which is usually much more important to you and me.

First, nearly all objects have a 'truth' value, for which I use the 
terms 'true' and 'false'.  For numbers, nonzero values are true, and 
zero values are false.  For collections, nonempty collections are true 
and empty ones are false.  And so on.  So if x is an int, and if you say:
     if x:

You're checking the 'truth' of x, and it'll execute the if clause for 
nonzero integers, for example.

If you have an arbitrary object 'x', it is not necessarily of type bool. 
  But if you compare it to another one,  x==y   the result is a bool, 
either True or False.  Likewise if you apply the unary 'not' to an 
object;  the result will be either True or False.

But if you combine two expressions with 'or' the result is NOT 
necessarily of type bool.  If the first expression is true, then the 
second expression isn't even examined.  If the first expression is 
false, the second expression is the result.

'and' has the reverse meaning.  And more complex expressions can be 
understood by breaking them into steps, according to operator precedence 
and parentheses.

-- 
DaveA



More information about the Python-list mailing list