Skip to content

Python Divmod Function & Examples

Python Divmod Function and Examples Cover Image

In this tutorial, you’ll learn how to use the Python divmod() function. The divmod() function takes two numbers are its arguments and returns their quotient and remainder. The function provides a number of incredibly helpful applications, such as being able to check divisibility and whether or not numbers are prime.

By the end of this tutorial, you’ll have learned:

  • The syntax of the Python divmod() function
  • How to use the divmod() function with integers and floating point values
  • How to prevent errors when using the divmod() function
  • How to check if numbers are divisible using the divmod() function

Understanding the Python divmod() Function

Before diving into how the Python divmod() function works, it’s important to understand how the parameters of the function work. The function takes two numbers (either floats or integers) and returns a tuple containing the quotient and the remainder when using integer division.

Let’s take a look a look at how the function looks:

# Understanding the Python divmod() Function
divmod(
   a,  # The numerator
   b   # The denominator
)

As shown in the code block above, the divmod() function takes a numerator and a denominator. The function will apply, as shown in a later function, will follow math rules. For example, passing in a 0 as the second argument will result in an error.

To make the function a little clearer to understand, you can think of it as being written as this:

# Rewriting divmod() From Scratch
results = (a // b, a % b)

In the code above, we use integer division and the modulo operator to calculate the result of the divmod() function. The divmod() function, however, provides a much more intentional way of getting this result.

Using the Python divmod() Function with Integers

Now that you have a strong understanding of the syntax of the Python divmod() function, let’s take a look at an example. When the Python divmod() function is used with two integers, a tuple of integers is returned.

# Using the Python divmod() Function with Integers
print(divmod(7,3))

# Returns
# (2, 1)

We can see that the function returns a tuple of integers. This tuple represents:

  1. The quotient, in this case is 2
  2. The remainder, in this case is 1

In the following section, you’ll learn how to use the function with floating point values.

Using the Python divmod() Function with Floating Point Values

When the Python divmod() function is used with either a single or two floating point values, the function returns a tuple of floating point values. While this doesn’t modify the resulting underlying math, it modifies the data type of the values in the tuple.

Let’s recreate our earlier example using floating point values:

# Using the Python divmod() Function with Floating Point Values
print(divmod(7.0,3))

# Returns
# (2.0, 1.0)

We can see that the function now returns a tuple with floating point values.

Preventing Errors with the Python divmod() Function

In this section, we’ll learn how to prevent errors when using the Python divmod() function. There are two main reasons that may cause an error when using the function:

  1. Dividing by zero
  2. Using non-numeric values

Let’s see what these errors look like, to see how we can prevent them:

# Raising ZeroDivisionError using the Python divmod() Function
divmod(3, 0)

# Raises: ZeroDivisionError: integer division or modulo by zero

The code below raises a TypeError when using a string data type:

# Raising TypeError using the Python divmod() Function
divmod(3, 'hello')

# Raises: TypeError: unsupported operand type(s) for divmod(): 'int' and 'str'

Let’s see how we can use error handling to safely handle these errors:

# Using Error Handling with the Python divmod() Function
try:
    result = divmod('hello', 0)
except ZeroDivisionError:
    print('Cannot divide by zero')
except TypeError:
    print('Must use numeric data types')

In the following section, you’ll use the divmod() function to see if two numbers are divisible.

How to Check if Numbers are Divisible with Python divmod()

The Python divmod() function can be used to check if two numbers are divisible, meaning that their remainder is zero. While you could easily use the modulo operator for this, the benefit of using divmod() is that you get access to the quotient and remainder.

Let’s see how we can use this to check if two numbers are easily divisible:

# Checking if Numbers are Divisible
def divisible(a, b):
    quotient, remainder = divmod(a, b)
    if remainder == 0:
        print(f'{a} is divisible by {b} and returns {quotient}.')
    else:
        print(f'{a} is not divisible by {b}.')

divisible(3, 2)

# Returns:
# 3 is not divisible by 2.

In the example above, the function will check whether two numbers are divisible. If they are, the function alerts the user and returns the quotient.

Conclusion

In this tutorial, you learned how to use the Python divmod() function. The function returns a tuple of a quotient and its remainder of the two values passed into the function. You first learned the syntax of the function. Then, you learned how to use the function with integers and floating point values. Finally, you learned how to use the function with a practical example, checking if two numbers are divisible.

Additional Resources

To learn more about related topics, check out the tutorials below:

Tags:

Leave a Reply

Your email address will not be published.