math.remainder()

PiergiulioAtzori's avatar
Published Feb 26, 2024
Contribute to Docs

The math.remainder() method is the remainder of x divided by y. It will return a float value, representing the remainder. The remainder is the difference between x and the closest integer multiple of y.

Syntax

math.remainder(x,y)
  • x: It is the number to be divided.
  • y: It is the number by which x is divided by.

Note: The Parameter y has to be a non-zero number or will return a ValueError.

Example

This example will return the remainder between two Integer values.

# Import math Library
import math
# Return the remainder of x/y
print(math.remainder(10, 2))
print(math.remainder(34, 3))

The output for the above code will be:

0.0
1.0

The example below will return the remainder between two Float values.

# Import math Library
import math
print(math.remainder(25.5, 5.7))
print(math.remainder(58.9, 5.5))

The output for the above line of code will be:

2.6999999999999993
-1.6000000000000014

The use of multiple Methods can then convert the result to an Integer value.

# Import math Library
import math
print(math.floor(math.remainder(25.5, 5.7)))
print(math.ceil(math.remainder(25.5, 5.7)))

The output for the above code will be:

2
3

Codebyte Example

The following code is runnable and demonstrates a few examples of the use of the math.remainder() method:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python on Codecademy