YouTip LogoYouTip

Func Number Modf

Python modf() Function | Rookie Tutorial .bd-placeholder-img { font-size: 1.125rem; text-anchor: middle; -webkit-user-select: none; -moz-user-select: none; user-select: none; } @media (min-width: 768px) { .bd-placeholder-img-lg { font-size: 3.5rem; } }

Python modf() Function | Rookie Tutorial

Python modf() Function

The modf() function is a built-in function in Python that splits a floating-point number into its fractional and integer parts.

Syntax

math.modf(x)

Parameters

  • x: A numeric value (float or integer).

Return Value

Returns a tuple containing two values:

  • The fractional part of the number.
  • The integer part of the number.

Example

import math

print(math.modf(3.14))
print(math.modf(-2.7))
print(math.modf(5))

Output

(0.14000000000000012, 3.0)
(-0.7000000000000002, -2.0)
(0.0, 5.0)

The modf() function returns the fractional and integer parts as floats, even if the input is an integer.

Notes

  • The returned fractional part has the same sign as the input number.
  • If the input is zero, both parts are zero.
  • This function is useful for extracting components of a floating-point number.

Related Functions

  • math.floor(x): Returns the largest integer less than or equal to x.
  • math.ceil(x): Returns the smallest integer greater than or equal to x.
  • math.trunc(x): Returns the truncated integer part of x.

Page 1 of 1
← Func Number PowFunc Number Min β†’