Logical Operators

Logical Operators return boolean result which makes them suitable for if statements.

Operator Description Examples Result
not Inverse the value. not True False
not False True
not (2 + 3 == 5) False
and
  • True if all statements are True.
  • False - if one or more statements are False.
True and True and True True
False and False and False False
True and False False
or
  • True if one or more statements are True.
  • False - if all statements are False.
True or True or True True
False or False or False False
True or False True

Example

Let's look at the following example. Depending on a test score, determine whether the score is low, medium, high or invalid following these rules:

  • 0 to 50 - low
  • 51 to 75 - medium
  • 76 to 100 - high
  • less than 0 or more than 100 - invalid

Output:

Explanation:

  • To check whether a number is between 2 values and operator can be used.
  • Line 3 checks whether number is greater or equal to 0 and if it's less or equal to 50.
  • Line 12 checks whether number is less than 0 or if it's greater than 100.