Boolean Expressions

Irv Kalb Irv at furrypants.com
Tue Sep 26 19:28:19 EDT 2017


> 
> On Sep 26, 2017, at 3:23 PM, 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 ? 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 
> 
> 

As others have said, this is about the definition of the "and" operator.   One example I give in my class is this:

At the local amusement park there is a sign that says, "You must be at least 12 years old and at least 48 inches tall to ride the roller coaster".  That means that both conditions must be true in order for you to get on the roller coaster.  

The Python code would be:

if (age >= 12) and (height >= 48):
    # You are good to go

It would be evaluated like this:

If you are 5 years old (first condition is false) and you are only 36 inches tall (second condition is false), you cannot get on the roller coaster.

Logically:  False and False is False

If you are 20 years old (true) but only 30 inches tall (false), you cannot get on the roller coaster

Logically:  True and False is False

If you are 10 years old (false) but you are 75 inches tall (true), you may be a freak of nature, but you cannot get on the roller coaster.

Logically:  False and True is False

If you are 13 years old (true) and 51 inches tall (true), enjoy your ride

Logically:   True and True is True

Bottom line, "and" operates between two Booleans.  "and" only give you a True as a result, when both side of the "and" are True.

Irv

PS:  The "or" operator works like this:  If any of the inputs is True, then the result of doing an "or" is True.   The only case where "or' gives you a False, is when all the inputs are False.




More information about the Python-list mailing list