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 > 5True

  • Condition 2: B < 15False

  • Since one condition is True, the OR operator returns True.


Example 2: Both Conditions False

A = 10
B = 20

print(A > 15 or B < 15)  # Output: False
  • Condition 1: A > 15False

  • Condition 2: B < 15False

  • Since both conditions are False, the OR operator returns False.


🏧 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 == 0False

    • Condition 2: amount % 200 == 0True

    • 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 == 0False

    • Condition 2: amount % 200 == 0False

    • 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 it False.

  • If the condition is False, not makes it True.


Example 1: Reversing a Condition

A = 24

print(not A < 56)  # Output: False
  • Condition: A < 56True

  • Applying not: not TrueFalse.


Example 2: Using NOT with Equality

B = 10

print(not B == 10)  # Output: False
  • Condition: B == 10True

  • Applying not: not TrueFalse.


🔗 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 and atm_balance = 2000:

    • Condition 1: amount % 500 == 0 or amount % 200 == 0True

    • Condition 2: not amount > atm_balanceTrue

    • Output: Amount is withdrawable


🧮 Summary of Logical Operators

AND (and):

  • Returns True only if both conditions are True.

OR (or):

  • Returns True if at least one condition is True.

NOT (not):

  • Reverses the result of a condition (TrueFalse).


📊 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

  1. The OR operator evaluates to True if at least one condition is True.

  2. Use the NOT operator to reverse a condition.

  3. 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