Understanding Python Operators: Part 7
Truth Tables for AND and OR Operators
Logical operators like AND, OR, and NOT are essential tools for decision-making in Python. They allow you to evaluate multiple conditions and control the flow of your code based on the results. In this section, we'll focus on the OR operator, its truth table, and practical applications, followed by a review of the NOT operator and combining logical operators.
🔍 The OR Operator
The OR operator (or
) returns True if at least one of the conditions is True. If both conditions are False, it returns False.
Truth Table for OR
X
Y
X OR Y
0
0
0
0
1
1
1
0
1
1
1
1
0 represents
False
1 represents
True
Key Insight:
The OR operator is inclusive, meaning it evaluates to True
as long as one or both conditions are True
.
🧪 Using the OR Operator in Python
Example 1: Simple OR Condition
A = 10
B = 20
print(A > 5 or B < 15) # Output: True
Condition 1:
A > 5
→True
Condition 2:
B < 15
→False
Since one condition is
True
, the OR operator returnsTrue
.
Example 2: Both Conditions False
A = 10
B = 20
print(A > 15 or B < 15) # Output: False
Condition 1:
A > 15
→False
Condition 2:
B < 15
→False
Since both conditions are
False
, the OR operator returnsFalse
.
🏧 Practical Example: ATM Withdrawal Logic
Let’s apply the OR operator to a real-world problem. Imagine an ATM that dispenses cash only in multiples of ₹500 or ₹200.
Code Example:
amount = 1200
if amount % 500 == 0 or amount % 200 == 0:
print("Amount is withdrawable")
else:
print("Amount is not withdrawable")
For
amount = 1200
:Condition 1:
amount % 500 == 0
→False
Condition 2:
amount % 200 == 0
→True
Output: Amount is withdrawable
Edge Case: Invalid Amount
amount = 1250
if amount % 500 == 0 or amount % 200 == 0:
print("Amount is withdrawable")
else:
print("Amount is not withdrawable")
For
amount = 1250
:Condition 1:
amount % 500 == 0
→False
Condition 2:
amount % 200 == 0
→False
Output: Amount is not withdrawable
❌ The NOT Operator
The NOT operator (not
) reverses the result of a condition:
If the condition is
True
,not
makes itFalse
.If the condition is
False
,not
makes itTrue
.
Example 1: Reversing a Condition
A = 24
print(not A < 56) # Output: False
Condition:
A < 56
→True
Applying
not
:not True
→False
.
Example 2: Using NOT with Equality
B = 10
print(not B == 10) # Output: False
Condition:
B == 10
→True
Applying
not
:not True
→False
.
🔗 Combining Logical Operators
Logical operators can be combined to build complex conditions.
Example: ATM Withdrawal with Balance Check
Add a condition to ensure the amount does not exceed the ATM balance.
amount = 1500
atm_balance = 2000
if (amount % 500 == 0 or amount % 200 == 0) and not amount > atm_balance:
print("Amount is withdrawable")
else:
print("Amount is not withdrawable")
For
amount = 1500
andatm_balance = 2000
:Condition 1:
amount % 500 == 0 or amount % 200 == 0
→True
Condition 2:
not amount > atm_balance
→True
Output: Amount is withdrawable
🧮 Summary of Logical Operators
AND (and
):
and
):Returns
True
only if both conditions areTrue
.
OR (or
):
or
):Returns
True
if at least one condition isTrue
.
NOT (not
):
not
):Reverses the result of a condition (
True
↔False
).
📊 Truth Table Review
AND Operator
X
Y
X AND Y
0
0
0
0
1
0
1
0
0
1
1
1
OR Operator
X
Y
X OR Y
0
0
0
0
1
1
1
0
1
1
1
1
🎯 Key Takeaways
The OR operator evaluates to
True
if at least one condition isTrue
.Use the NOT operator to reverse a condition.
Combine logical operators to handle complex scenarios efficiently.
Stay tuned for Part 8, where we’ll explore bitwise operators and their unique applications in Python! 😊
Last updated