{"id":24223,"date":"2021-10-14T09:18:38","date_gmt":"2021-10-14T03:48:38","guid":{"rendered":"https:\/\/python-programs.com\/?p=24223"},"modified":"2021-11-05T20:56:25","modified_gmt":"2021-11-05T15:26:25","slug":"python-program-for-modf-function","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-modf-function\/","title":{"rendered":"Python Program for modf() Function"},"content":{"rendered":"
In the previous article, we have discussed Python Program for How To Use While Not<\/a> 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.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n Number:<\/strong> It could be a number or a valid numerical expression.<\/p>\n Return Value:<\/strong><\/p>\n For example:<\/p>\n Let the number = 3.75<\/p>\n Then the modf() gives 0.75 as the first argument and 3.00 as the second argument.<\/p>\n Therefore , output = (0.75, 3.00)<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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<\/a>.<\/p>\n 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: …<\/p>\n
\nmodf() Function in Python:<\/strong><\/p>\nmath.modf(Number);<\/pre>\n
\n
Given Number = 4.75<\/pre>\n
The result after applying modf() function on above given number 4.75 = (0.75, 4.0)<\/pre>\n
Given Number = 15<\/pre>\n
The result after applying modf() function on above given number 15 = (0.0, 15.0)<\/pre>\n
Program for modf() Function in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import math module using the import keyword.\r\nimport math\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [7, 4.65, 6.5, 14, 10]\r\n# Give the first number as static input and store it in another variable.\r\ngvn_numb1 = 4.75\r\n# Apply math.modf() function to the given first number to divide the given value\r\n# into arguments(fractional part as the first argument and an integer value\r\n# as the second argument).\r\n# Store it in another variable.\r\nfnl_rslt = math.modf(gvn_numb1)\r\nprint(\"The result after applying modf() function on above given first number\",\r\n gvn_numb1, \" = \", fnl_rslt)\r\n# similarly do the same for the other number.\r\ngvn_numb2 = 15\r\nprint(\"The result after applying modf() function on above given second number\",\r\n gvn_numb2, \" = \", math.modf(gvn_numb2))\r\n# Apply math.modf() function to the given list element and print it.\r\nprint(\r\n \"The result after applying modf() function on given list element gvnlst[1] = \", math.modf(gvn_lst[1]))\r\n<\/pre>\n
The result after applying modf() function on above given first number 4.75 = (0.75, 4.0)\r\nThe result after applying modf() function on above given second number 15 = (0.0, 15.0)\r\nThe result after applying modf() function on given list element gvnlst[1] = (0.6500000000000004, 4.0)<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import math module using the import keyword.\r\nimport math\r\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(float, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give the first number as user input using the float(input()) function\r\n# and store it in a variable.\r\ngvn_numb1 = float(input(\"Enter some random number = \"))\r\n# Apply math.modf() function to the given first number to divide the given value\r\n# into arguments(fractional part as the first argument and an integer value\r\n# as the second argument).\r\n# Store it in another variable.\r\nfnl_rslt = math.modf(gvn_numb1)\r\nprint(\"The result after applying modf() function on above given first number\",\r\n gvn_numb1, \" = \", fnl_rslt)\r\n# similarly do the same for the other number.\r\ngvn_numb2 = float(input(\"Enter some random number = \"))\r\nprint(\"The result after applying modf() function on above given second number\",\r\n gvn_numb2, \" = \", math.modf(gvn_numb2))\r\n# Apply math.modf() function to the given list element and print it.\r\nprint(\r\n \"The result after applying modf() function on given list element gvnlst[3] = \", math.modf(gvn_lst[3]))\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 12 13 14.674 30.238\r\nEnter some random number = 32.49\r\nThe result after applying modf() function on above given first number 32.49 = (0.490000000000002, 32.0)\r\nEnter some random number = 59.759\r\nThe result after applying modf() function on above given second number 59.759 = (0.7590000000000003, 59.0)\r\nThe result after applying modf() function on given list element gvnlst[3] = (0.23799999999999955, 30.0)<\/pre>\n
\n