Python Math modf() Method
The Python Math modf() method returns the integer and fractional parts of x. Both parts have the same sign as x, and the integer part is represented as floating point.
Python Math modf() Syntax
Here is the syntax of the modf() method:
import math
math.modf( x )
Note: modf() is not directly accessible, you need to import the math module and call the method via a static object.
Python Math modf() Parameters
- x – The numeric expression.
Python Math modf() Return Value
returns the integer part of x with the fractional part.
Python Math modf() Example#1
The following shows an example of using the modf() method.
#!/usr/bin/python3
import math
print("math.modf(100.12) : ", math.modf(100.12))
print("math.modf(-100.72) : ", math.modf(-100.72))
print("math.modf(2) : ", math.modf(2))
Output:
Python Math modf() Example#2
#!/usr/bin/python3
import math
print("math.modf(100.12) : ", math.modf("100.12"))
Output:
Python Math modf() Example#3
#!/usr/bin/python3
from math import modf
lst = [3.12, -5.14, 13.25, -5.21]
tpl = (33.12, -15.25, 3.15, -31.2)
# modf() function on elements of list
print("modf() on First list element : ", modf(lst[0]))
print("modf() on third list element : ", modf(lst[2]))
# modf() function on elements of tuple
print("modf() on Second tuple element : ", modf(tpl[1]))
print("modf() on Fourth tuple element : ", modf(tpl[3]))
Output: