Using Boolean logic and operators
Boolean logic refers to the operators, namely, and
, or
, and not
in Python. You'll recall seeing this in the brief discussion on truth tables earlier in this chapter. As we'll see next, we use the same logical processing when writing the algorithms, even if the tables are not explicitly stated or used. When solving computational thinking problems, we sometimes have to meet multiple conditions at once. Let's look at this using just language for now.
Let's sort some fruit. If the fruit is round and orange, green, or yellow, it will be sorted into group 1. If the fruit is not round, but is orange, green, or yellow, it will be sorted into group 2. If the fruit doesn't match those requirements, it goes into group 3. Let's simplify these groups:
- Group 1: Round AND (orange OR green OR yellow)
- Group 2: Not round AND (orange OR green OR yellow)
- Group 3: All other fruit
I know I stated the round condition...