Logical Operators
Logical operators combine multiple Boolean expressions to produce a single Boolean result. They are essential for complex decision-making.
1. and
Returns True only if both operands are True.
| A | B | A and B |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
python age = 25 country = 'USA'
result_and = (age >= 18) and (country == 'USA') print(result_and) # True
2. or
Returns True if at least one operand is True.
| A | B | A or B |
|---|---|---|
| True | True | True |
| True | False | True |
| False | True | True |
| False | False | False |
python day = 'Sunday'
is_weekend = (day == 'Saturday') or (day == 'Sunday') print(is_weekend) # True
3. not
Negates the Boolean value (inverts the result).
| A | not A |
|---|---|
| True | False |
| False | True |
python is_raining = False print(not is_raining) # True