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 < 10
is true only ifx
is greater than 0 and at the same time, x is 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 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, sonot x > y
is true ifx > y
is false, that is, ifx
is less than or equal toy
.
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.