Operators with Booleans

We have been alluding to the topic of this lesson for a while now. The topic today is on conditional expressions, specifically, the operators we may use to evaluate a conditional expression. The operators we will cover in this lesson are: == (equality), < > <= >= (inequalities), and (&), or (|), xor (|), and not (not).

The equality operator ==

The equality operator is ==. It is used to compare two values. If the two values are equal, then it evaluates to True. Otherwise, it evaluates to False. Let's look at the four possible combinations of True and False.

We more commonly use the equality operator to compare variables. Let's look at a couple examples.

The inequality operators: <, >, <=, and >=

The inequality operators are <, >, <=, and >=. Same as the equality operator, these operators compare two values. If the inequality is true, then it evaluates to True. Otherwise, it evaluates to False. Let's look at some examples.

The bitwise AND operators: and, &

The bitwise and operator may also be written as &, however the and way is more common. This operator is also used to compare two boolean values. Similarly to the equality operator, if both sides evaluate to True, then it evaluates to True. However, if any one side is False, then the entire expression will evaluate to False. Let's look at the four possible combinations of True and False.

Let's look at some more involved examples of the and operator. In this code editor we combine the and operator with the equality operator to check two conditions at once.

In the first example, the first term 1 == 1 evaluates to True, as does the second term. As such, we have the conditional expression True and True.

In the second example, the first term a == b evaluates to True, however the second term, a == c evaluates to False. As such, we have the conditional expression True and False.

The bitwise OR operators: or, |

The bitwise or operator may also be written as |, and same as the and operator, the or way is more common. However, unlike the bitwise and operator, the bitwise or operator will evaluate to True if at least one of the sides is True. Only when both sides are False will it evaluate to False. Let's look at the four possible combinations of True and False.

Let's look at the same conditional expressions as before, but this time connect them with the or operator. With the first example, both conditional expressions are still True. As such, when connected via the or operator we have True or True. Similarly, in the second example, we have the conditional expression True or False.

The bitwise XOR operator: ^

The bitwise XOR operator is written as ^. This is a less common operator, however it is useful in certain situations. The ^ operator will evaluate to True if exactly one of the sides is True. If both sides are True or both sides are False, then it will evaluate to False.

The logical NOT operator: not

The logical not operator is written as not. This operator will flip the boolean value. So True becomes False and False becomes True.

Similarly, the != operator, which is the inequality operator is the opposite of the == operator. It will evaluate to True if the two values are not equal, and False otherwise.

Practice Question

Will it snow on Thursday? Consider the following code. What would the condition evaluate to if we ran: snow_day("Thurs").

score: 0%