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 and True and True | True |
False and False and False | False | ||
True and False | False | ||
or |
|
True or True or True | True |
False or False or False | False | ||
True or False | True |
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:
Output:
Explanation:
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.