Python modf

The Python modf math function is used to divide the given value into two arguments. Fractional Part as the first argument and integer value as the second argument of modf function.

In this section, we discuss how to use modf function in Python Programming language with example. The syntax of the modf Function is

math.modf(Number);
  • If it is a positive or negative number, the modf function returns the first argument as Zero.
  • If it is a decimal, it returns the first argument as decimal values, and the second argument is an integer value.
  • And if it is not a number, it returns TypeError.

For example, math.modf(2.45) returns 0.45 as the first argument and 2.00 as the second argument. The Final output = (0.45, 2.00)

Python modf Function Example

In this Python modf example, we are going to return the same for different data types and display the output.

import math

Tup = (10.98, 20.26, -30.05, -40.95 , 50.85) # Tuple Declaration
Lis = [-10.98, 32.65, -39.29, -42.15 , 39.97] # List Declaration

print('MODF() Function on Positive Number = ', math.modf(10))
print('MODF() Function on Negative Decimal = ', math.modf(-15))

print('MODF() Function on Positive Decimal = ', math.modf(10.9))
print('MODF() Function on Negative Decimal = ', math.modf(-15.487))

print('MODF() Function on Tuple Item = ', math.modf(Tup[2]))
print('MODF() Function on Tuple Item = ', math.modf(Tup[4]))
print('MODF() Function on List Item = ', math.modf(Lis[2]))
print('MODF() Function on List Item = ', math.modf(Lis[4]))

print('MODF() Function on Multiple Number = ', math.modf(10 + 20 - 40.6578))

print('MODF() Function on String Value = ', math.modf('2.95'))
Python MODF Function
  1. Here, we tried this Math function on Tuple and List items.
  2. Last, We tried this Python Function on the String value, and it returns TypeError as output