Skip to main content

Section 4.2 Logical operators

Combining Conditions.

Remember that question we had in the previous section: “Is the amount we want to withdraw from a bank account greater 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 less than zero or is it greater than the account balance?” If either one of those things happens, we can't let the customer 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.
There are 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 less than 10.
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 3.
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.

Checkpoint 4.2.1.

    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.2.2.

    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.2.3.

    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.

Note 4.2.4.

Strictly speaking, the operands of the logical operators should be boolean expressions, but Python is not very strict. Any nonzero number is interpreted as “true.”
This flexibility can be useful, but there are some subtleties to it that might be confusing. You might want to avoid it until you are sure you know what you are doing.