Python, Django, FastAPI Tutorials

# How to Find Remainder in Python

In this post, we will look at various ways to find the remainder of a division operation in Python.

The most popular method for finding a remainder in Python is using the modulo operator, %. It is the official Python remainder operator and it returns the remainder of the division of one number by another. For example, the expression `10 % 3` would evaluate to 1 because 3 goes into 10 three times with a remainder of 1.

However, apart from the modulo operator, %, you can also utilize other techniques, such as the `divmod()` and `math.fmod()` functions.

The ways to find the remainder of a division operation in Python that we will cover in this post are:

1. Using the `%` operator
2. Using the `divmod()` function
3. Using the `math.fmod()` function
4. Using the `math.remainder()` function

We will go over the syntax for each method, some examples of how to use them, and their advantages.

## Method 1: Using the `%` operator

The `%` operator, known as the modulo operator or remainder operator, is a mathematical operator that is used to find the remainder of a division operation. It is similar to the division operator (`/`), but instead of returning the quotient of the division, it returns the remainder.

Here are some examples of using the `%` operator to find the remainder in Python:

``````>>> 7 % 3
1
>>> 9 % 5
4
>>> 12 % 2
0``````

The `%` operator takes in two operands: the dividend and the divisor. The dividend is the number being divided, and the divisor is the number that the dividend is being divided by. The `%` operator returns the remainder of the division operation as a result.

In the first example, the divisor is 3, and the dividend is 7. This division operation’s remainder is 1, hence the % operator returns 1.

Overall, the `%` operator is a simple and straightforward way to find the remainder of a division operation in Python. It is commonly used for this purpose and is easy to understand and use.

## Method 2: Using the `divmod()` function

The Python `divmod()` function is a built-in function that takes two numbers as inputs—the dividend and the divisor—and outputs a tuple that contains the quotient and remainder of the division operation.

Here are some examples of using the `divmod()` function to find the remainder:

``````>>> divmod(8, 2)
(4, 0)
>>> divmod(9, 4)
(2, 1)
>>> divmod(17, 4)
(4, 1)``````

The first example has a divisor of 3, and the dividend is 7. The divmod() method performs a division and returns the quotient and the remainder, which in this case are 2 and 1, as a tuple.

All in all, Python’s built-in `divmond()` function is the best function to use when you want to find both the quotient and the remainder all at the same time.

## Method 3: Using the `math.fmod()` function

Finding the remainder in Python can also be done using the `fmod() `function, which is a component of the math module. It is, of course, similar to the % operator, except it calculates remainders in accordance with the C programming language’s principles.

To use the `math.fmod()` function, you will need to import the `math` module first.

Here are some examples of using the `math.fmod()` function to find the remainder:

``````>>> import math
>>> math.fmod(5, 2)
1.0
>>> math.fmod(11, 7)
4.0
>>> math.fmod(13, 5)
3.0``````

In the first example, the dividend is 5 and the divisor is 2. The `math.fmod()` function returns the remainder of the division operation which is 1.

## Method 4: Using the `remainder()` function

The math module’s `remainder()` method is the fourth function that can be used to determine the remainder of a division operation. It is similar to the `fmod()` function, except it calculates remainders in accordance with IEEE 754 standards.

The `remainder()` method requires the math module to be imported before it can be used, just like the `fmod()` function does. The method can then be used as shown in the following examples:

``````>>> import math
>>> math.remainder(8, 3)
2.0
>>> math.remainder(13, 5)
4.0
>>> math.remainder(11, 3)
2.0``````

In the last example, the dividend is 11 and the divisor is 3. The `math.remainder()` function returns the remainder of the division operation which is 1.

In general, Python’s `math.remainder()` method is also helpful for determining the remainder of a division operation. It is part of the math module and calculates remainders in accordance with IEEE 754 standards. It is similar to the `math.fmod()` function, but in some circumstances, where adhering to the IEEE 754 standard is desired, it might be more preferable.

## Conclusion

In conclusion, finding the remainder is made simple and straightforward by using the % operator, which is why it is widely utilized for this task. The `divmod()` method returns a tuple that contains the division operation’s quotient and remainder. Both the `fmod()` and `remainder()` functions are found in the math module; however, the former follows the C programming language’s rules, while the latter adheres to the IEEE 754 standard.

The methods I’ve mentioned above are all great ways for finding the remainder of a division operation. The one you’ll choose depends on your unique needs and preferences for the project you’ll building.

How to Find Remainder in Python
Scroll to top