Easy Tutorial
❮ Python Copy List Ref Set Issuperset ❯

Python math.fmod() Method

Python math Module

The math.fmod(x, y) method in Python returns the remainder of x/y.

Python Version: 2.7

Syntax

The syntax for the math.fmod() method is as follows:

math.fmod(x, y)

Parameter Description:

Note: If x and y are both 0, it returns a ValueError.

Note: If y is 0, it returns a ValueError.

Note: If either x or y is not a number, it returns a TypeError.

Return Value

A floating-point value representing the remainder of x/y.

Example

The following example calculates the remainder:

Example

# Import math package
import math

# Calculate remainder
print(math.fmod(20, 4))
print(math.fmod(20, 3))
print(math.fmod(15, 6))
print(math.fmod(-10, 3))

# Error, ValueError: math domain error
print(math.fmod(0, 0))

Output result:

0.0
2.0
3.0
-1.0
Traceback (most recent call last):
    File "/Users/tutorialpro/tutorialpro-test/test.py", line 9, in <module>
    print(math.fmod(0, 0))
ValueError: math domain error

Python math Module

❮ Python Copy List Ref Set Issuperset ❯