modf() function - Python Last Updated : 21 Feb, 2025 Comments Improve Suggest changes Like Article Like Report modf() function is an inbuilt function in Python that returns the fractional and integer parts of the number in a two-item tuple. Both parts have the same sign as the number. The integer part is returned as a float. Example: Python import math # modf() function used with a positive number print("math.modf(100.12) : ", math.modf(100.12)) # modf() function used with a negative number print("math.modf(-100.72) : ", math.modf(-100.72)) # modf() function used with an integer print("math.modf(2) : ", math.modf(2)) Outputmath.modf(100.12) : (0.12000000000000455, 100.0) math.modf(-100.72) : (-0.7199999999999989, -100.0) math.modf(2) : (0.0, 2.0) Explanationmath.modf(100.12): fractional part is 0.12000000000000455, and the integer part is 100.0.math.modf(-100.72): fractional part is -0.7199999999999989, and the integer part is -100.0.math.modf(2): Since 2 is an integer, the fractional part is 0.0, and the integer part is 2.0.Syntax of modf() function:modf(number) ParameterThere is only one mandatory parameter which is the number. Return TypeThis method returns the fractional and integer parts of number in a two-item tuple. Both parts have the same sign as the number. The integer part is returned as a float.TypeError: If anything other than a float number is passed, it returns a type error. Examples of modf() function1. Using invalid String typeThis code demonstrates the error when attempting to use modf() with an invalid type (string instead of a float). It returns a TypeError. Python import math # modf() function used with a positive number print("math.modf(100.12) : ", math.modf("100.12")) Output : Traceback (most recent call last): File "/home/fa6d7643de17bafe9a0e0693458e4bdb.py", line 9, in print("math.modf(100.12) : ", math.modf("100.12"))TypeError: a float is required2. Using modf() with Lists and TuplesThis code demonstrates the usage of the modf() function on elements of a list and a tuple, printing the fractional and integer parts for each. Python 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])) Outputmodf() on First list element : (0.1200000000000001, 3.0) modf() on third list element : (0.25, 13.0) modf() on Second tuple element : (-0.25, -15.0) modf() on Fourth tuple element : (-0.1999999999...Explanation:modf(lst[0]) splits 3.12 into 0.11999999999999922 (fractional) and 3.0 (integer).modf(lst[2]) splits 13.25 into 0.25 (fractional) and 13.0 (integer).modf(tpl[1]) splits -15.25 into -0.25 (fractional) and -15.0 (integer).modf(tpl[3]) splits -31.2 into -0.20000000000000284 (fractional) and -31.0 (integer).3. Multiplying Fractional PartsThis code demonstrates the application of the modf() function by multiplying the fractional parts of two floating-point numbers. Python # Python3 program to demonstrate the # application of function modf() # This will import math module import math # modf() function to multiply fractional part a = math.modf(11.2) b = math.modf(12.3) # Multiply the fractional part as is stored # in 0th index of both the tuple print(a[0]*b[0]) Output0.05999999999999993 Explanation:The program uses modf() to extract the fractional parts of 11.2 and 12.3.It multiplies the fractional parts and prints the result: 0.05999999999999949. Comment More infoAdvertise with us Next Article modf() function - Python S Striver Follow Improve Article Tags : Misc Python Python-Built-in-functions Practice Tags : Miscpython Similar Reads pow() Function - Python pow() function in Python is a built-in tool that calculates one number raised to the power of another. It also has an optional third part that gives the remainder when dividing the result. Example:Pythonprint(pow(3,2))Output9 Explanation: pow(3, 2) calculates 32 = 9, where the base is positive and t 2 min read Python oct() Function Python oct() function takes an integer and returns the octal representation in a string format. In this article, we will see how we can convert an integer to an octal in Python. Python oct() Function SyntaxSyntax : oct(x) Parameters: x - Must be an integer number and can be in either binary, decimal 2 min read Python map() function The map() function is used to apply a given function to every item of an iterable, such as a list or tuple, and returns a map object (which is an iterator). Let's start with a simple example of using map() to convert a list of strings into a list of integers.Pythons = ['1', '2', '3', '4'] res = map( 4 min read Python Lambda Functions Python Lambda Functions are anonymous functions means that the function is without a name. As we already know the def keyword is used to define a normal function in Python. Similarly, the lambda keyword is used to define an anonymous function in Python. In the example, we defined a lambda function(u 6 min read Python Functions Python Functions is a block of statements that does a specific task. The idea is to put some commonly or repeatedly done task together and make a function so that instead of writing the same code again and again for different inputs, we can do the function calls to reuse code contained in it over an 9 min read Python int() Function The Python int() function converts a given object to an integer or converts a decimal (floating-point) number to its integer part by truncating the fractional part.Example:In this example, we passed a string as an argument to the int() function and printed it.Pythonage = "21" print("age =", int(age) 3 min read Python len() Function The len() function in Python is used to get the number of items in an object. It is most commonly used with strings, lists, tuples, dictionaries and other iterable or container types. It returns an integer value representing the length or the number of elements. Example:Pythons = "GeeksforGeeks" # G 2 min read Functools module in Python The functools module offers a collection of tools that simplify working with functions and callable objects. It includes utilities to modify, extend, or optimize functions without rewriting their core logic, helping you write cleaner and more efficient code.Let's discuss them in detail.1. Partial cl 5 min read Python | math.gcd() function In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.gcd() function compute the greatest common divisor of 2 numbers mentioned in its arguments. Syntax: math.gcd(x, y) Parameter: x : Non-negative integer whose gcd has to be comp 2 min read Like