# Type bool¶

Python has a type bool for Boolean values. Unlike types int, float, and str, which have thousands of values, type bool has only two values: True and False.

Here are some examples of expressions that produce Booleans:

In [1]:
4 == 5.6

Out[1]:
False
In [2]:
5 == 5

Out[2]:
True
In [3]:
6.7 > 2

Out[3]:
True
In [4]:
4.5 <= 9.3

Out[4]:
True
In [5]:
27.4 != 27.5

Out[5]:
True

In the code above, we used several of Python's relational operators. Here is a summary:

Equal to: ==

Not equal to: !=

Greater than: >

Greater than or equal to: >=

Less than: <

Less than or equal to: <=

## Boolean operators¶

There are three Boolean operators (the operands to these operators are type bool): not, and, and or.

In [6]:
not True

Out[6]:
False
In [7]:
not False

Out[7]:
True

Operator and evaluates to True if and only if both operands are True.

In [8]:
True and True

Out[8]:
True
In [9]:
True and False

Out[9]:
False
In [10]:
False and True

Out[10]:
False
In [11]:
False and False

Out[11]:
False

Operator or evaluates to True if and only if at least one operator is True.

In [12]:
True or True

Out[12]:
True
In [13]:
True or False

Out[13]:
True
In [14]:
False or True

Out[14]:
True
In [15]:
False or False

Out[15]:
False

Note: the Boolean operator or isn't quite the same as the English "or".

In English, "For dessert, I'll have pie or I'll have ice cream", means that I'll only have one dessert.

In Python, it means that I'll have at least one dessert.

### Operator precedence¶

Consider this code fragment:

In [ ]:
grade1 = 80


What value does the expession produce: True or False?

The order of precedence for Boolean operators is not, and, and or.

To make the order clear, we can add parentheses:

In [17]:
(not grade1 >= 50) or (grade2 >= 50)

Out[17]:
True

We can also use parentheses to change the order of operations:

In [18]:
not ((grade1 >= 50) or (grade2 >= 50))

Out[18]:
False

## Practice exercise: English description¶

Write an English description of the situation in which the following code produces True:

not grade1 >= 50 or grade2 >= 50.

## Practice Exercise: Exclusive or¶

How do you express the English "I'll have pie or I'll have ice cream, but not both"? Define two Boolean variables:

In [19]:
pie = False
ice_cream = True


Write a Python expression that evaluates to True if and only if exactly one of pie and ice_cream refers to True.