Handling Modulo and Integer Division in Python

In this section, we dive deeper into integer division, modulo operations, and their combined use in practical scenarios like ATM systems. These operators allow developers to implement robust logic for numerical calculations, especially when working with constraints like fixed denominations.


🔢 Integer Division in Python

In Python, division (/) always returns a floating-point number, even if the result is a whole number. For instance:

result = 10 / 2
print(result)  # Output: 5.0

To obtain only the integer part of the division (without a decimal point), use floor division (//), which rounds the result down to the nearest whole number:

Examples:

result = 10 // 2
print(result)  # Output: 5

result = 10 // 3
print(result)  # Output: 3  (rounds down)

🏧 Combining Modulo and Integer Division for ATM Logic

Let’s revisit the ATM system. Using a combination of integer division (//) and modulo (%) allows for more complex withdrawal scenarios, such as dispensing a mix of denominations.

Example: Handling Mixed Notes

If a customer requests ₹900, the ATM can dispense it using a mix of ₹500 and ₹200 notes. Here's how to handle such logic:

amount = 900

if amount % 500 == 0:
    print("Withdrawable with ₹500 notes only")
elif amount % 200 == 0:
    print("Withdrawable with ₹200 notes only")
else:
    num_500_notes = amount // 500
    remaining_amount = amount % 500
    num_200_notes = remaining_amount // 200

    if remaining_amount % 200 == 0:
        print(f"Withdrawable with {num_500_notes} ₹500 notes and {num_200_notes} ₹200 notes")
    else:
        print("Amount cannot be withdrawn with available notes")

Explanation:

  1. Step 1: Check if the amount is divisible by ₹500 or ₹200.

  2. Step 2: If not, calculate the number of ₹500 notes needed using integer division.

  3. Step 3: Use the remainder to calculate the required ₹200 notes.

  4. Step 4: Ensure the remainder fits into ₹200 notes; otherwise, reject the request.


🧪 Example Scenarios

Scenario 1: ₹900

Output:

Withdrawable with 1 ₹500 notes and 2 ₹200 notes

Scenario 2: ₹1250

Output:

Amount cannot be withdrawn with available notes

🛠 Handling Special Cases

Some amounts require a mix of denominations, while others might not fit any combination of notes. For example, ₹1200 can be withdrawn using both ₹500 and ₹200 notes:

amount = 1200

if amount % 500 == 0:
    print("Withdrawable with ₹500 notes only")
elif amount % 200 == 0:
    print("Withdrawable with ₹200 notes only")
else:
    num_500_notes = amount // 500
    remaining_amount = amount % 500
    num_200_notes = remaining_amount // 200

    if remaining_amount % 200 == 0:
        print(f"Withdrawable with {num_500_notes} ₹500 notes and {num_200_notes} ₹200 notes")
    else:
        print("Amount cannot be withdrawn with available notes")

Output:

Withdrawable with 2 ₹500 notes and 1 ₹200 note

🔍 Using int() for Precision Control

In cases where floor division (//) is not preferred, use the int() function to truncate decimals:

result = int(10 / 3)
print(result)  # Output: 3

This approach ensures precision when converting floating-point results to integers without using explicit floor rounding.


🤔 Logical Operators in ATM Systems

Logical operators like AND, OR, and NOT enhance ATM logic by adding constraints and flexibility.

Example: Adding Balance Constraints

An ATM must ensure the requested amount:

  1. Is divisible by ₹500 or ₹200.

  2. Does not exceed the available balance.

amount = 1500
atm_balance = 5000

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 divisible by ₹500 or ₹200")

Key Points:

  • OR (or): Ensures divisibility by either ₹500 or ₹200.

  • AND (and): Verifies that the requested amount is within the ATM's balance.


🚀 Conclusion

In this section, we explored:

  1. Integer Division (//): Useful for obtaining whole-number results.

  2. Modulo (%): Essential for checking divisibility.

  3. Combining Operators: A powerful method for solving complex problems like ATM withdrawals.

These tools ensure that your logic is flexible and robust, capable of handling various scenarios with precision.


Next Steps

In Part 9, we’ll explore error handling and techniques to optimize Python code, ensuring both reliability and efficiency in real-world systems. Stay tuned! 😊

Last updated