GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask
  • Introduction1 min read
  • Input & Output2 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — modf() function - Python - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask

modf() function - Python

Last Updated : 21 Feb, 2025

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))

Output
math.modf(100.12) :  (0.12000000000000455, 100.0)
math.modf(-100.72) :  (-0.7199999999999989, -100.0)
math.modf(2) :  (0.0, 2.0)

Explanation

  • math.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)

Parameter

  • There is only one mandatory parameter which is the number.

Return Type

  • This 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() function

1. Using invalid String type

This 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 required

2. Using modf() with Lists and Tuples

This 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]))

Output
modf() 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 Parts

This 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])

Output
0.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

Explore

    Python Fundamentals
    • Introduction1 min read
    • Input & Output2 min read
    • Variables4 min read
    • Operators4 min read
    • Keywords2 min read
    • Data Types4 min read
    • Conditional Statements3 min read
    • Loops3 min read
    • Functions4 min read
    Python Data Structures
    • String4 min read
    • List3 min read
    • Tuples3 min read
    • Dictionary3 min read
    • Set2 min read
    • Arrays5 min read
    Advanced Python
    • OOP Concepts3 min read
    • Exception Handling5 min read
    • File Handling4 min read
    • Python Database2 min read
    • MongoDB3 min read
    • MySQL7 min read
    • Packages4 min read
    • Modules3 min read
    • DSA Libraries5 min read
    • Python GUI2 min read
    Data Science with Python
    • Numpy3 min read
    • Pandas4 min read
    • Matplotlib3 min read
    • Seaborn3 min read
    • StatsModel2 min read
    • Model Building6 min read
    • TensorFlow2 min read
    • PyTorch5 min read
    Web Development with Python
    • Flask3 min read
    • Django5 min read
    • Django ORM4 min read
    • Jinja2 Templating5 min read
    • Django Templates5 min read
    • REST API3 min read
    • Build API with DRF4 min read
    Python Practice
    • Quiz1 min read
    • Practice Problems2 min read
    • Interview Q & A15+ min read
    Courses
    • Placement 360 Course2 min read
    • Data Science 360 Course2 min read
    • AI Engg Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.