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 Python modf function. In this section, we discuss how to use modf function in Python Programming language with example. The syntax of the modf Function in Python is

math.modf(Number);

Number: It can be a number or a valid numerical expression.

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

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

Python modf Function Example

The Python modf function divides the given value into two arguments. The Fractional Part as first and integer value as the second Python argument. In this modf example, We are going to return the same for different data types and display the output.

# Python modf Function

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. In the first two statements, We used the modf Function directly on both the Positive and Negative Numbers.
  2. Next two statements, We used the Python modf Function directly on both the Positive decimals and negative decimals.
  3. Next four statements, We used the modf Math function on Tuple and List items. If you observe the above screenshot, modf function is working perfectly on them.
  4. Here, we tried modf Function directly on multiple values
  5. Last, We tried this Python Function on the String value, and it returns TypeError as output