Boolean Expressions

Cameron Simpson cs at cskk.id.au
Tue Sep 26 18:37:10 EDT 2017


On 26Sep2017 15:23, Cai Gengyang <gengyangcai at gmail.com> wrote:
>
>I'm trying to understand the logic behind AND. I looked up Python logic tables
>
>False and False gives False
>False and True gives False
>True and False gives False
>True and True gives True.
>
>So does that mean that the way 'and' works in Python is that both terms must be True (1) for the entire expression to be True ?

Yes.

>Why is it defined that way, weird ? I was always under the impression that 
>'and' means that when you have both terms the same, ie either True and True or 
>False and False , then it gives True

I would have called this "not the same". You are describing "or".

"and" means "both true". "or" means "either true".

An "and" expression is true if the left half is true _and_ the right half is 
true. Therefore both must be true, otherwise the whole result is false.

An "or" expression is true if the left half is true _or_ the right half is 
true, therefore only one half needs to be true. You only get 'false" is both 
halves are false.

Think of "and" a little like multiplication with values or 0 (false) and 1 
(true).  Think of "or" a little like addition with values of 0 (false) and 1 
(true).

And:
1 * 1 => nonzero/true
1 * 0 => zero/false
0 * 1 => zero/false
0 * 0 => zero/false

Or:
1 + 1 => nonzero/true
1 + 0 => nonzero/true
0 + 1 => nonzero/true
0 + 0 => zero/false

If all this seems confusing I wonder about your understanding of the plain 
English words "and" and "or", but your English seems otherwise perfectly fine 
to me, so that would be very surprising.

Can you write some plain English sentences where "and" or "or" lead you to 
misinterpret a similar Python expression? Then we can see where the differences 
lie.

Cheers,
Cameron Simpson <cs at cskk.id.au> (formerly cs at zip.com.au)



More information about the Python-list mailing list