{"id":24140,"date":"2021-10-14T09:09:58","date_gmt":"2021-10-14T03:39:58","guid":{"rendered":"https:\/\/python-programs.com\/?p=24140"},"modified":"2021-11-05T20:53:50","modified_gmt":"2021-11-05T15:23:50","slug":"python-program-for-fsum-function","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-fsum-function\/","title":{"rendered":"Python Program for fsum() Function"},"content":{"rendered":"
In the previous article, we have discussed Python Program for frexp() Function<\/a> The math.fsum() method returns the sum of all iterable items (tuples, arrays, lists, etc.).<\/p>\n Syntax:<\/strong><\/p>\n Parameters:<\/strong><\/p>\n iterable: This is required. It may be a tuple, list, arrays, etc to sum. If the iterable does not contain numbers, it throws a TypeError.<\/p>\n Return Value:<\/strong><\/p>\n It returns a float value representing the sum of the iterable’s items.<\/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 <\/p>\n","protected":false},"excerpt":{"rendered":" In the previous article, we have discussed Python Program for frexp() Function fsum() Function in Python: The math.fsum() method returns the sum of all iterable items (tuples, arrays, lists, etc.). Syntax: math.fsum(iterable) Parameters: iterable: This is required. It may be a tuple, list, arrays, etc to sum. If the iterable does not contain numbers, it …<\/p>\n
\nfsum() Function in Python:<\/strong><\/p>\nmath.fsum(iterable)<\/pre>\n
Given List = [1, 3, 5, 8]\r\nGiven Tuple = (10, 20, 50, 60)<\/pre>\n
The sum of all list items after applying fsum() function = 17.0\r\nThe sum of all tuple items after applying fsum() function = 140.0<\/pre>\n
Given List = [15, 20, 40, 10, 30]\r\nGiven Tuple = (45, 30, 10, 5, 6)<\/pre>\n
The sum of all list items after applying fsum() function = 115.0\r\nThe sum of all tuple items after applying fsum() function = 96.0<\/pre>\n
Program for fsum() 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 = [1, 3, 5, 8]\r\n# Give the tuple as static input and store it in another variable.\r\ngvn_tupl = (10, 20, 50, 60)\r\n# Apply math.fsum() function to the given list to get the sum of all list items.\r\n# store it in another variable.\r\nlst_sum = math.fsum(gvn_lst)\r\n# print the above result list sum.\r\nprint(\"The sum of all list items after applying fsum() function = \", lst_sum)\r\n# Apply math.fsum() function to the given tuple to get the sum of all tuple items.\r\n# store it in another variable.\r\ntupl_sum = math.fsum(gvn_tupl)\r\n# print the above result tuple sum.\r\nprint(\"The sum of all tuple items after applying fsum() function = \", tupl_sum)\r\n<\/pre>\n
The sum all list items after applying fsum() function = 17.0\r\nThe sum all tuple items after applying fsum() function = 140.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(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give the tuple as user input using list(),map(),input(),and split() functions.\r\n# Store it in another variable.\r\ngvn_tupl = tuple(map(int, input(\r\n 'Enter some random tuple Elements separated by spaces = ').split()))\r\n# Apply math.fsum() function to the given list to get the sum of all list items.\r\n# store it in another variable.\r\nlst_sum = math.fsum(gvn_lst)\r\n# print the above result list sum.\r\nprint(\"The sum of all list items after applying fsum() function = \", lst_sum)\r\n# Apply math.fsum() function to the given tuple to get the sum of all tuple items.\r\n# store it in another variable.\r\ntupl_sum = math.fsum(gvn_tupl)\r\n# print the above result tuple sum.\r\nprint(\"The sum of all tuple items after applying fsum() function = \", tupl_sum)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 15 20 40 10 30\r\nEnter some random tuple Elements separated by spaces = 45 30 10 5 6\r\nThe sum of all list items after applying fsum() function = 115.0\r\nThe sum of all tuple items after applying fsum() function = 96.0<\/pre>\n
\n