Practice with Relational and Logical Operators

Try these exercises for more practice with relational and logical operators.

3. Logical Operators

There are three logical operators that are used to compare values. They evaluate expressions down to Boolean values, returning either  True or  False . These operators are and or, and  not  and are defined in the table below.

Operator What it means What it looks like
and True if both are true x and y
or True if at least one is true x or y
not True only if false not x

Logical operators are typically used to evaluate whether two or more expressions are true or not true. For example, they can be used to determine if the grade is passing and  that the student is registered in the course, and if both cases are true then the student will be assigned a grade in the system. Another example would be to determine whether a user is a valid active customer of an online shop based on whether they have store credit or have made a purchase in the past 6 months.


To understand how logical operators work, let’s evaluate three expressions:

print((9 > 7) and (2 < 4))  # Both original expressions are True
print((8 == 8) or (6 != 6)) # One original expression is True
print(not(3 <= 1))          # The original expression is False
Output
True
True
True

In the first case, print((9 > 7) and (2 < 4)), both 9 > 7 and 2 < 4 needed to evaluate to True since the and operator was being used.

In the second case, print((8 == 8) or (6 != 6)), since 8 == 8 evaluated to True, it did not make a difference that 6 != 6 evaluates to False because the or operator was used. If we had used the and operator, this would evaluate to False.

In the third case, print(not(3 <= 1)), the not operator negates the False value that 3 <=1 returns.

Let’s substitute floats for integers and aim for False evaluations:

print((-0.2 > 1.4) and (0.8 < 3.1)) # One original expression is False
print((7.5 == 8.9) or (9.2 != 9.2)) # Both original expressions are False       
print(not(-5.7 <= 0.3))             # The original expression is True

In the example above, - and must have at least one False expression evaluate to False, - or must have both expressions evaluate to False, - not must have its inner expression be True for the new expression to evaluate to False.

If the results above seem unclear to you, we’ll go through some truth tables below to get you up to speed.

You can also write compound statements using andor, and not:

not((-0.2 > 1.4) and ((0.8 < 3.1) or (0.1 == 0.1)))

Let’s look at the inner-most expression first: (0.8 < 3.1) or (0.1 == 0.1). This expression evaluates to True because both mathematical statements are True.

Now, we can take the returned value True and combine it with the next inner expression: (-0.2 > 1.4) and (True). This example returns False because the mathematical statement -0.2 > 1.4 is False, and (False) and (True) returns False.

Finally, we have the outer expression: not(False), which evaluates to True, so the final returned value if we print this statement out is:

Output
True

The logical operators andor, and not evaluate expressions and return Boolean values.