Logical operatorsΒΆ
- There are three logical operators:
and,or, andnot. - The semantics (meaning) of these operators is similar to their meaning in English.
- For example,
x > 0 and x < 10is true only ifxis greater than 0 and at the same time, x is less than 10. n % 2 == 0 or n % 3 == 0is 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
notoperator negates a boolean expression, sonot x > yis true ifx > yis false, that is, ifxis less than or equal toy.
(chp05_3)
Check your understanding
select-2-1: What is the correct Python expression for checking to see if a number stored in a variable x is between 0 and 5.