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.
Python Tutorial
Python Tutorial
Python Introduction
Python Environment Setup
Python Chinese Encoding
Python VS Code
Python Basic Syntax
Python Variable Types
Python Operators
Python Conditional Statements
Python Loops
Python While Loop
Python For Loop
Python Nested Loops
Python Break Statement
Python Continue Statement
Python Pass Statement
Python Number (Numbers)
Python Strings
Python Lists
Python Tuples
Python Dictionary
Python Date and Time
Python Functions
Python Modules
Python File I/O
Python File Methods
Python Exception Handling
Python OS File/Directory Methods
Python Built-in Functions
Advanced Python
Python Object-Oriented Programming
Python Regular Expressions
Python CGI Programming
Python MySQL Database
Python Network Programming
Python SMTP Email Sending
Python Multithreading
Python XML Parsing
Python GUI Programming (Tkinter)
Python 2.x vs 3.x Differences
Python IDE
Python JSON
Python AI Drawing
Python 100 Examples
Python Quiz
YouTip