bool Operators¶
Python provides logical operators for combining and transforming Boolean expressions.
The three Boolean operators are:
andornot
These operators are essential for building compound conditions.
flowchart TD
A[Boolean expressions]
A --> B[and]
A --> C[or]
A --> D[not]
````
---
## 1. The `and` Operator
`and` returns `True` only if **both operands are true**.
```python
print(True and True)
print(True and False)
Output:
True
False
Truth table:
| A | B | A and B |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
Example:
age = 20
has_id = True
if age >= 18 and has_id:
print("Entry allowed")
2. The or Operator¶
or returns True if at least one operand is true.
print(True or False)
print(False or False)
Output:
True
False
Truth table:
| A | B | A or B |
|---|---|---|
| True | True | True |
| True | False | True |
| False | True | True |
| False | False | False |
Example:
is_weekend = True
is_holiday = False
if is_weekend or is_holiday:
print("No work today")
3. The not Operator¶
not reverses a Boolean value.
print(not True)
print(not False)
Output:
False
True
Truth table:
| A | not A |
|---|---|
| True | False |
| False | True |
Example:
logged_in = False
if not logged_in:
print("Please sign in")
4. Short-Circuit Evaluation¶
Python’s Boolean operators use short-circuit evaluation.
This means evaluation stops as soon as the result is known.
and¶
If the left side is false, Python does not need to evaluate the right side.
False and print("hello")
The print() call is never executed.
or¶
If the left side is true, Python does not need to evaluate the right side.
True or print("hello")
Again, print() is not executed.
flowchart LR
A[left operand] --> B{result already known?}
B -->|yes| C[stop]
B -->|no| D[evaluate right operand]
Short-circuiting is useful for guard conditions.
x = None
if x is not None and x > 0:
print("positive")
5. Operators Return Values¶
In Python, and and or return one of their operands, not necessarily True or False.
print(0 or 5)
print("" or "default")
print(3 and 7)
Output:
5
default
7
This behavior is often used for fallback values.
name = user_input or "Guest"
6. Worked Examples¶
Example 1: combined condition¶
age = 25
has_ticket = True
print(age >= 18 and has_ticket)
Example 2: fallback value¶
username = ""
display_name = username or "Anonymous"
print(display_name)
Output:
Anonymous
Example 3: negation¶
is_busy = False
print(not is_busy)
Output:
True
7. Common Pitfalls¶
Assuming and and or always return bool¶
They often return one of the original operands.
Forgetting precedence¶
not has higher precedence than and, which has higher precedence than or.
Use parentheses when clarity matters.
8. Summary¶
Key ideas:
andrequires both conditions to be trueorrequires at least one condition to be truenotreverses truth value- Python uses short-circuit evaluation
andandormay return operands, not just Booleans
Boolean operators let programs express complex logic clearly and efficiently.