Python Operators and Assignment Logic: Part 8

The Modulo Operator: Practical Use Case in ATMs 🏧

The Modulo operator (%) is a vital tool in programming when you need to ensure numbers align with specific rules, such as divisibility. This section demonstrates its practical application in designing systems like ATM withdrawals, where it validates if the amount to be dispensed matches the denominations available.


🔢 Recap: Modulo Basics

The Modulo operator returns the remainder of a division operation:

500 % 500  # Output: 0 (500 is divisible by 500 without a remainder)

For systems like ATMs, the modulo operation ensures requested amounts are divisible by available note denominations.


🚀 Example: ATM Withdrawal

Consider an ATM that dispenses only ₹500 and ₹200 notes. To verify the validity of a requested amount, use the modulo operator:

Code:

amount = 1000

if amount % 500 == 0 or amount % 200 == 0:
    print("Amount is withdrawable")
else:
    print("Amount is not withdrawable")

Output:

Amount is withdrawable

Edge Case: Invalid Amount

For an invalid amount like ₹1250, the system rejects the request:

amount = 1250

if amount % 500 == 0 or amount % 200 == 0:
    print("Amount is withdrawable")
else:
    print("Amount is not withdrawable")

Output:

Amount is not withdrawable

🔍 Why This Works:

  • The modulo operator checks if the remainder is 0 for divisibility.

  • The OR operator ensures withdrawal is allowed if the amount is divisible by either ₹500 or ₹200.


❌ The NOT Operator: Reversing Logic

The NOT operator (not) flips the result of a condition:

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

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

Example: Basic NOT Logic

print(not True)   # Output: False
print(not False)  # Output: True

🔄 Practical Use Case: Minimum Withdrawal

An ATM should prevent withdrawals below ₹500. Using the NOT operator, this logic can be expressed as:

amount = 400

if not amount >= 500:
    print("Amount is too low for withdrawal")

Output:

Amount is too low for withdrawal

Explanation:

  • The condition amount >= 500 evaluates to False because the amount is less than ₹500.

  • not reverses this False to True, triggering the if block.


🧩 Combining Logical Operators for Complex Conditions

In real-world scenarios, logical operators like AND, OR, and NOT often work together to handle multiple conditions.


Example: ATM Withdrawal with Multiple Conditions

Suppose the ATM has the following rules:

  1. The amount must be divisible by ₹500 or ₹200.

  2. The amount must not exceed the ATM's balance.

Code:

amount = 1500
atm_balance = 2000

if (amount % 500 == 0 or amount % 200 == 0) and amount <= atm_balance:
    print("Amount is withdrawable")
else:
    print("Amount exceeds ATM balance or is not a valid multiple of 500 or 200")

Output:

Amount is withdrawable

Edge Case: Invalid Amount or Exceeding Balance

If the requested amount exceeds the balance or fails divisibility:

amount = 2100

if (amount % 500 == 0 or amount % 200 == 0) and amount <= atm_balance:
    print("Amount is withdrawable")
else:
    print("Amount exceeds ATM balance or is not a valid multiple of 500 or 200")

Output:

Amount exceeds ATM balance or is not a valid multiple of 500 or 200

How It Works:

  • OR operator ensures divisibility by ₹500 or ₹200.

  • AND operator checks if the divisibility condition and balance condition are both met.


🏁 Key Takeaways

  1. The Modulo operator validates divisibility, making it ideal for applications like ATM withdrawals.

  2. The NOT operator reverses conditions, adding flexibility in logic.

  3. Combining AND, OR, and NOT enables handling complex rules and constraints in real-world systems.


🎯 Next Steps

This concludes our deep dive into the Modulo and Logical operators. In Part 9, we’ll move on to advanced Python topics, such as bitwise operators and their applications in programming. Stay tuned for more insights! 😊

Last updated