4.2. Logical operators¶
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
.
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.
- 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 agian. 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.
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 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.
Q-3: Which of the following is true for both 7 and 24?
- 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.
Q-4: Which of the following is True?