Python fmod

The Python fmod math function is used to calculate the Module of the specified given arguments. In this section, we discuss how to use the fmod function with example.

The syntax of the math fmod Function in Python Programming Language is

math.fmod(x, y);
  • The fmod Function uses these X and Y arguments to find the Module. If both the X and Y arguments are Zero, it returns the output as ValueError.
  • If the Y argument (second argument) is Zero, it returns the output as ValueError.
  • And if the X value or Y value is not a number, it returns TypeError.

Python fmod Function Example

The Python fmod function calculates the Module of the specified given arguments. In this example, we are going to find the module of different data types and display the output

import math

Tup = (10, 20, 12, -40 , 50) # Tuple Declaration
Lis = [-98, 32, -39, -42 , 15] # List Declaration

print('Calculating MOD of Positive Number = %.2f' %math.fmod(2, 3))
print('Calculating MOD of Negative Number = %.2f' %math.fmod(-14, 3))

print('Calculating MOD of Positive Decimal = %.2f' %math.fmod(22.45, 4.5))
print('Calculating MOD of Negative Decimal = %.2f' %math.fmod(-110, 14.78))

print('Calculating MOD of Tuple Item = %.2f' %math.fmod(Tup[2], 5))
print('Calculating MOD of Tuple Item = %.2f' %math.fmod(Tup[2], -5))
print('Calculating MOD of List Item = %.2f' %math.fmod(Lis[4], 4))
print('Calculating MOD of List Item = %.2f' %math.fmod(Lis[0], -15))

print('Calculating MOD of Multiple Number = %.2f' %math.fmod(10 + 20 - 12, 40))

print(' Calculating MOD of String Value = ', math.fmod('2', 3))
Python FMOD Function

We tried it on String value, and it returns TypeError as output. Please refer to the TupleList, and Math functions in Python.