Python divmod() Method with Examples

In the previous article, we have discussed Python int() Method with Examples
divmod() Method in Python:

When argument1 (dividend) is divided by argument2, the divmod() function returns a tuple containing the quotient and the remainder (divisor).

Syntax:

divmod(dividend, divisor)

Parameter Values:

dividend: It is a number. The number by which you wish to divide

divisor: It is a number. The number you wish to divide by.

Return Value:

The divmod() Function returns

  • (q, r) – a pair of numbers (a tuple) made up of the quotient q and the remainder r.
  • If dividend and divisor are integers, the result of divmod() is (q// r, dividend %divisor ).
  • If either dividend or divisor is a float, the resulting expression is (q, dividend %divisor ). In this case, q represents the entire quotient.

Examples:

Example1:

Input:

Given first number(dividend)= 3
Given second number(divisor) = 5

Output:

A Tuple containing the quotient and the remainder = (0, 3)

Example2:

Input:

Given first number(dividend) = 12.5
Given second number(divisor) = 3.5

Output:

A Tuple containing the quotient and the remainder = (3.0, 2.0)

divmod() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the first number as static input and store it in a variable.
  • Give the second number as static input and store it in another variable.
  • Pass the given first and second numbers as arguments to the divmod() function that returns a tuple containing the quotient and the remainder (divisor).
  • Print the above result i.e, a tuple containing the quotient and the remainder (divisor).
  • The Exit of the Program.

Below is the implementation:

# Give the first number as static input and store it in a variable.
gvn_numb1 = 3
# Give the second number as static input and store it in another variable.
gvn_numb2 = 5
# Pass the given first and second numbers as arguments to the divmod() function
# that returns a tuple containing the quotient and the remainder (divisor).
rslt = divmod(gvn_numb1, gvn_numb2)
# Print the above result i.e, a tuple containing the quotient and the remainder
# (divisor).
print("A Tuple containing the quotient and the remainder =", rslt)

Output:

A Tuple containing the quotient and the remainder = (0, 3)

Similarly, Check it out for other numbers

gvn_numb1 = 12.5
gvn_numb2 = 3.5
rslt = divmod(gvn_numb1, gvn_numb2)
print("A Tuple containing the quotient and the remainder =", rslt)

Output:

A Tuple containing the quotient and the remainder = (3.0, 2.0)

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the first number as user input using the float(input()) function and store it in a variable.
  • Give the second number as user input using the float(input()) function and store it in another variable.
  • Pass the given first and second numbers as arguments to the divmod() function that returns a tuple containing the quotient and the remainder (divisor).
  • Print the above result i.e, a tuple containing the quotient and the remainder (divisor).
  • The Exit of the Program.

Below is the implementation:

# Give the first number as user input using the float(input()) function 
# and store it in a variable.
gvn_numb1 = float(input("Enter some random number = "))
# Give the second number as user input using the float(input()) function 
# and store it in another variable.
gvn_numb2 = float(input("Enter some random number = "))
# Pass the given first and second numbers as arguments to the divmod() function
# that returns a tuple containing the quotient and the remainder (divisor).
rslt = divmod(gvn_numb1, gvn_numb2)
# Print the above result i.e, a tuple containing the quotient and the remainder
# (divisor).
print("A Tuple containing the quotient and the remainder =", rslt)

Output:

Enter some random number = 45.5
Enter some random number = 5
A Tuple containing the quotient and the remainder = (9.0, 0.5)

Fed up with searching various pages for the list of Python Built in Functions? Look at the tutorial linked here and explore all coding samples of built-in functions of python.