# 9.8. Boolean Algebra

>>> True and True or False
True
>>>
>>> False and False or True
True

>>> (True and True) or False
True
>>>
>>> True and (True or False)
True

>>> True and False or False
False
>>>
>>> True and (False or False)
False

>>> True or False and True
True
>>>
>>> False or True and False
False
>>>
>>> False or True and True
True
>>>
>>> (False or True) and True
True
>>> (False or True) and False
False
>>>
>>> True and True or False and True
True
>>> True and (True or False) and True
True
>>>
>>> True or False and True or False
True
>>> (True or False) and (True or False)
True
>>> True or False and True or True
True
>>> (True or False) and (True or True)
True


## 9.8.1. Precedence

• First ands are evaluated together

• Then ors

## 9.8.2. Optimization

• Python won't evaluate the rest of an expression if already knows an answer

Python won't evaluate the rest of an expression if already knows an answer. In the following example you may see, that expression on the right side is not checked at all! Mind also, that variable x was never defined!

>>> False and x > 1
False
>>>
>>> x
Traceback (most recent call last):
NameError: name 'x' is not defined


Python will see False and .... False and something will always return False so Python does performance optimization and won't even check the other argument. In this case, it would fail with NameError if it does.

## 9.8.3. Example

>>> firstname = 'Mark'
>>> lastname = 'Watney'
>>>
>>>
>>> (firstname == 'Mark' and lastname == 'Watney') \
...     or (firstname == 'Melissa' and lastname == 'Lewis') \
...     or (firstname == 'Rick' and lastname == 'Martinez')
True


Because:

>>> firstname = 'Mark'
>>> lastname = 'Watney'
>>>
>>>
>>> firstname == 'Mark' and lastname == 'Watney'
True
>>>
>>> firstname == 'Melissa' and lastname == 'Lewis'
False
>>>
>>> firstname == 'Rick' and lastname == 'Martinez'
False


Rule:

>>> True or False or False
True


## 9.8.4. Control Flow

• Use parenthesis for explicit order

>>> firstname = 'Mark'
>>> lastname = 'Watney'
>>>
>>> if (firstname == 'Mark' and lastname == 'Watney') \
...         or (firstname == 'Melissa' and lastname == 'Lewis') \
...         or (firstname == 'Rick' and lastname == 'Martinez'):
...
...     print('Hello user')
... else:
Hello user


## 9.8.5. Good Practices

>>>
... for line in file:
...     if line and (not line.startswith('#') or not line.isspace()):
...         ...

>>>
... for line in file:
...     if len(line) == 0:
...         continue
...
...     if line.startswith('#'):
...         continue
...
...     if line.isspace():
...         continue