Skip to main content

Section 4.4 Logical operators

Combining Conditions.

Remember that question we had in Section 4.1: “Is the amount we want to withdraw from a bank account less than or equal than the account balance?” It turns out we have to also make sure that the customer isn’t trying to take out a negative amount. The question we really need to ask is:
“Is the amount we want to withdraw from a bank account greater than or equal to zero and is it less than or equal to the balance?”
If both of these things are true, the customer gets their money; otherwise, we can’t let them do the transaction.
Here are other questions that combine two conditions:
  • Is the age greater than or equal to 18 and less than 21?
  • Did the roll of two dice add up to either 7 or 11?
We use logical operators to ask these sorts of questions in Python.
Python has three logical operators: and, or, and not. The semantics (meaning) of these operators is similar to their meaning in English. For example, x > 0 and x < 10 is True only if x is greater than 0 and at the same time, x is less than 10. How would you describe this in words? You would say that x is between 0 and 10, not including the endpoints.
n % 2 == 0 or n % 3 == 0 is True if either of the conditions is True, that is, if the number is divisible by 2 or divisible by 3. In this case, one, or the other, or both of the parts has to be true for the result to be true.
Finally, the not operator negates a boolean expression, so not x > y is true if x > y is false, that is, if x is less than or equal to y.

Note 4.4.1. WARNING!

There is a very common mistake that occurs when programmers try to write boolean expressions. For example, what if we have a variable number and we want to check to see if its value is 5, 6, or 7. In words we might say: “number equal to 5 or 6 or 7”. However, if we translate this into Python, number == 5 or 6 or 7, it will not be correct. The or operator must join the results of three equality checks. The correct way to write this is number == 5 or number == 6 or number == 7. This may seem like a lot of typing, but it is absolutely necessary. You cannot take a shortcut.

Checkpoint 4.4.2.

    Q-2: What is a correct Python expression for checking to see if a number stored in a variable x is between 0 and 5?
  • x > 0 and < 5
  • Try again. Each comparison must be between exactly two values. In this case the right-hand expression < 5 lacks a value on its left.
  • x > 0 or x < 5
  • Try again. Although this is legal Python syntax, the expression is incorrect. It will evaluate to true for all numbers that are either greater than 0 or less than 5. Because all numbers are either greater than 0 or less than 5, this expression will always be True.
  • x > 0 and x < 5
  • Correct! With an and keyword both expressions must be true so the number must be greater than 0 an less than 5 for this expression to be true. Although most other programming languages do not allow this mathematical syntax, in Python, you could also write 0 < x < 5.

Checkpoint 4.4.3.

    Q-3: Which of the following is true for both 7 and 24?
  • x < 0 or x > 25
  • Neither value is less than 0 or greater than 25.
  • x > 0 or x < 5
  • This is true when x is greater than 0 or less than 5. Both 7 and 24 are greater than 0.
  • x > 0 and x < 5
  • This is true when the number is both greater than 0 and less than 5.

Checkpoint 4.4.4.

    Q-4: Which of the following is True?
  • not (5 > 71)
  • Correct! 5 > 71 is False, so not (False) is True.
  • not (5 < 71)
  • Try again. 5 < 7 is True, so not (True) is False.
  • not (5 != 71)
  • Try again. 5 != 71 is True, so not (True) is False.