Python Program for modf() Function

In the previous article, we have discussed Python Program for How To Use While Not
modf() Function in Python:

The modf() math function in Python is used to divide a given value into two arguments. Python modf() function takes a fractional part as the first argument and an integer value as the second argument.

Syntax:

math.modf(Number);

Parameters

Number: It could be a number or a valid numerical expression.

Return Value:

  • If the number argument is positive or negative, the modf() function returns Zero.
  • If the number argument is positive or negative decimal, the modf() function returns decimal values for the first argument and an integer value for the second.
  • If it is not a number, the modf() function throws a TypeError.

For example:

Let the number = 3.75

Then the modf() gives 0.75 as the first argument and 3.00 as the second argument.

Therefore , output = (0.75, 3.00)

Examples:

Example1:

Input:

Given Number = 4.75

Output:

The result after applying modf() function on above given number 4.75  =  (0.75, 4.0)

Example2:

Input:

Given Number = 15

Output:

The result after applying modf() function on above given number 15 = (0.0, 15.0)

Program for modf() Function in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import math module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Give the first number as static input and store it in another variable.
  • Apply math.modf() function to the given first number to divide the given value into arguments(fractional part as the first argument and an integer value as the second argument).
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply math.modf() function to the given list element and print it.
  • The Exit of Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the list as static input and store it in a variable.
gvn_lst = [7, 4.65, 6.5, 14, 10]
# Give the first number as static input and store it in another variable.
gvn_numb1 = 4.75
# Apply math.modf() function to the given first number to divide the given value
# into arguments(fractional part as the first argument and an integer value
# as the second argument).
# Store it in another variable.
fnl_rslt = math.modf(gvn_numb1)
print("The result after applying modf() function on above given first number",
      gvn_numb1, " = ", fnl_rslt)
# similarly do the same for the other number.
gvn_numb2 = 15
print("The result after applying modf() function on above given second number",
      gvn_numb2, " = ", math.modf(gvn_numb2))
# Apply math.modf() function to the given list element and print it.
print(
    "The result after applying modf() function on given list element gvnlst[1] = ", math.modf(gvn_lst[1]))

Output:

The result after applying modf() function on above given first number 4.75  =  (0.75, 4.0)
The result after applying modf() function on above given second number 15  =  (0.0, 15.0)
The result after applying modf() function on given list element gvnlst[1] =  (0.6500000000000004, 4.0)

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import math module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the first number as user input using the float(input()) function and store it in a variable.
  • Apply math.modf() function to the given first number to divide the given value into arguments(fractional part as the first argument and an integer value as the second argument).
  • Store it in another variable.
  • Print the above result.
  • Similarly, do the same for the other number.
  • Apply math.modf() function to the given list element and print it.
  • The Exit of Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(float, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Give the first number as user input using the float(input()) function
# and store it in a variable.
gvn_numb1 = float(input("Enter some random number = "))
# Apply math.modf() function to the given first number to divide the given value
# into arguments(fractional part as the first argument and an integer value
# as the second argument).
# Store it in another variable.
fnl_rslt = math.modf(gvn_numb1)
print("The result after applying modf() function on above given first number",
      gvn_numb1, " = ", fnl_rslt)
# similarly do the same for the other number.
gvn_numb2 = float(input("Enter some random number = "))
print("The result after applying modf() function on above given second number",
      gvn_numb2, " = ", math.modf(gvn_numb2))
# Apply math.modf() function to the given list element and print it.
print(
    "The result after applying modf() function on given list element gvnlst[3] = ", math.modf(gvn_lst[3]))

Output:

Enter some random List Elements separated by spaces = 12 13 14.674 30.238
Enter some random number = 32.49
The result after applying modf() function on above given first number 32.49 = (0.490000000000002, 32.0)
Enter some random number = 59.759
The result after applying modf() function on above given second number 59.759 = (0.7590000000000003, 59.0)
The result after applying modf() function on given list element gvnlst[3] = (0.23799999999999955, 30.0)

Read all the mathematical functions available in Python and understand how to implement them in your program by using the tutorial of Python Mathematical Methods Examples.