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 fmod function in Python with example.

Python fmod Function Syntax

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

math.fmod(x, y);
  • X: Please specify the X value here.
  • Y: Please specify the Y value here. The fmod Function uses these X and Y arguments to find the Module.

NOTE:

  • If both the X and Y arguments are Zero, the fmod function returns the output as ValueError.
  • If the Y argument (second argument) is Zero, the fmod function returns the output as ValueError.
  • And if the X value or Y value is not a number, fmod function returns TypeError.

Python fmod Function Example

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

# Python fmod Function

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
  1. Within the first two Python statements, We passed both the Positive integer and negative integer as the Python fmod Function arguments. From the above screenshot, see that the fmod Function is returning output.
  2. Within the next two statements, we passed both the Positive and negative decimals as the fmod Function arguments. As we said before, the Python fmod Function is returning the output.
  3. Next four statements, We used the Tuple and List items as first arguments and Positive and negative integer values as the second argument for fmod Math function. If you observe the above screenshot, the fmod function is working perfectly on them.
  4. Next statement, We assigned multiple values as the first arguments, and the fmod Function worked without any issue.
  5. Next, We tried fmod Function on String value, and it returns TypeError as output.