Python modf() Method


Contents

Description

The modf() method in Python number is an inbuilt function in Python that returns the fractional as well as integer parts of x into a two-item tuple. Both the parts comprise of the same sign as x. The integer part is thus, returned as a float. Thus, this Python Mathematical Functions is used for dividing the given value into two arguments where Fractional Part is the first argument and the second argument of Python modf() function is the integer value.

Syntax

Following is the syntax for modf() method:

import math

math.modf( x )

Note − This function is not accessible directly, so we need to import math module and then we need to call this function using math static object.

Parameters

  • x − This is a numeric expression.

Return Value

This method returns the fractional and integer parts of x in a two-item tuple. Both parts have the same sign as x. The integer part is returned as a float.

Example

The following example shows the usage of modf() method.

#!/usr/bin/python
import math   # This will import math module

print "math.modf(100.12) : ", math.modf(100.12)
print "math.modf(100.72) : ", math.modf(100.72)
print "math.modf(119L) : ", math.modf(119L)
print "math.modf(math.pi) : ", math.modf(math.pi)

When we run above program, it produces following result:

math.modf(100.12) :  (0.12000000000000455, 100.0)
math.modf(100.72) :  (0.71999999999999886, 100.0)
math.modf(119L) :    (0.0, 119.0)
math.modf(math.pi) :  (0.14159265358979312, 3.0)

Here at Intellinuts, we have created a complete Python tutorial for Beginners to get started in Python.



Navigation
Tutorial
Tutorial
Tutorial