Comparison and Logical Operators

Comparison Operators

We often need to compare two values. Is one value greater, smaller, or equal?

Python has the following comparison operators:

num_1 == num_2 # num_1 equals num_2
num_1 != num_2 # num_1 does not equal num_2
num_1 > num_2 # num_1 is greater than num_2
num_1 < num_2 # num_1 is less than num_2
num_1 >= num_2 # num_1 is greater than or equal to num_2
num_1 <= num_2 # num_1 is less than or equal to num_2

Each of the above conditions is either True or False, which is a bool value.

For example:

Don't mix up the assignment operator (=) and the equality operator (==). It's a common mistake that can be hard to see.

Careful: don't test two floats for exact equality, because round-off errors can make values that should be equal differ slightly.

Instead, if you need to compare float values for equality, you can do something like this:

abs(num_1 - num_2) < 0.000001

This takes the absolute value of the difference between the two numbers and checks whether it's less than a very small value.

Unlike many computer languages, Python allows you to chain comparison operators. In Python you check if x is between 2 and 7 by saying

2 < x < 7

whereas in many languages you would need to write that as

2 < x and x < 7

Chaining works with any of the comparison operators, so 13 <= age <= 19 checks that age is from 13 to 19, including both ends.

The logical operator and is discussed in the following section.

Logical Operators

Sometimes we need to combine comparisons. Python has the following logical operators:

bool_1 and bool_2                  # Both bool_1 and bool_2 equal True
bool_1 or bool_2                   # At least one of bool_1 and bool_2 equals True
not bool_1                         # Flips the value of bool_1. If bool_1 is True not bool_1 is False and vice versa

Each of these conditions is also either True or False.

In Plain Language

Logical operators can look confusing, but they work like everyday language.

Consider two conditions, represented by P and Q. When P and Q are joined with the and operator, the statement is true only if both P and Q are true. For example, if someone asks "Do you have a car and a driver's license?" The answer would be yes only if you have BOTH a car AND a driver's license. On the other hand, the or operator makes the statement True if either P or Q (or both) are True. If someone asks "Do you have a car or a bike?" The answer would be yes if you have either a car or a bike (or both).

The not operator simply reverses the truth value of a statement, so if P is False, then not P is True and vice versa.

A Common Syntax Error

To check if num_x and num_y are both equal to 5, it is not correct to do the following:

num_x and num_y == 5  # wrong

since that checks num_x on its own as a true or false value, and also checks num_y == 5. Instead, you would need to do this:

num_x == 5 and num_y == 5

or since we know we can chain comparisons:

num_x == num_y == 5

Check your understanding. Predict the result. Work out each comparison first, then combine them with and, or and not.

age: int = 15
has_ticket: bool = True
can_enter: bool = age >= 13 and has_ticket
needs_adult: bool = age < 18 or not has_ticket
turned_away: bool = not (age >= 13 and has_ticket)

Predict what each variable holds after the program finishes. The program is not run for you. Replace each None in the box below with True or False, and give each variable a type hint, like name: type = value. Then press Run to find out if you are right.

  • final_can_enter is the value of can_enter at the end
  • final_needs_adult is the value of needs_adult at the end
  • final_turned_away is the value of turned_away at the end
Hint 1

Work out each comparison to True or False first, then apply and, or or not to those values.

Quizzes

  1. Print the value of 17 < 5.
  1. Write a function named greater_than_ten that takes one numeric parameter and returns True if the value is greater than 10 and False otherwise.
  1. Write a function named in_range that takes one numeric parameter and returns True if the value is between 50 and 100 (inclusive) and False otherwise.