Python divmod() Function – Tutorial with Examples

The divmod() function in Python is a built-in function that takes two numbers and returns a tuple of two values: the quotient and the remainder when the first number is divided by the second number.

Syntax

divmod(a, b)

Parameters

  • a – The dividend, which is the number being divided.
  • b – The divisor, which is the number the dividend is being divided by.

Return Value

The divmod() function returns a tuple of two values: the quotient and the remainder when the first number is divided by the second number.

Examples

Example 1: Basic Usage

a = 10
b = 3
result = divmod(a, b)
print(result)

Output:

(3, 1)

In this example, the divmod() function is used to divide the number 10 by 3. The result of this division is stored in the tuple result, which contains the quotient (3) and the remainder (1).

Example 2: Using the Result

a = 10
b = 3
quotient, remainder = divmod(a, b)
print("Quotient:", quotient)
print("Remainder:", remainder)

Output:

Quotient: 3
Remainder: 1

In this example, the result of the division is stored in the tuple quotient, remainder, which is then unpacked and the values are printed separately.

Example 3: Division with Large Numbers

a = 1000000000000
b = 3
result = divmod(a, b)
print(result)

Output:

(333333333333, 1)

In this example, the divmod() function is used to divide a large number by 3. The result of this division is stored in the tuple result, which contains the quotient (333333333333) and the remainder (1).

Use Cases

The divmod() function is useful in several scenarios:

  • Division of numbers: The divmod() function can be used to divide two numbers and get both the quotient and the remainder in a single operation.
  • Modulus operation: The divmod() function can be used to perform the modulus operation, where the remainder when the first number is divided by the second number is required.
  • Calculating the floor division and remainder: The divmod() function can be used to calculate the floor division and the remainder at the same time. The floor division of two numbers is the largest integer that is smaller than or equal to the result of the division. The remainder is the difference between the dividend and the product of the divisor and the floor division.
  • Integral division and remainder: In some cases, the exact division and remainder of two numbers is required. The divmod() function provides an easy way to get both values at once, rather than performing the division and then using the modulus operator to get the remainder.

Conclusion

The divmod() function is a useful and efficient tool for performing division and remainder operations in Python. It provides a convenient way to get both the quotient and the remainder in a single operation. Whether you need the floor division and remainder, the exact division and remainder, or just the remainder, the divmod() function has you covered.

Leave a Reply

Your email address will not be published. Required fields are marked *