Python Program for fsum() Function

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 throws a TypeError.

Return Value:

It returns a float value representing the sum of the iterable’s items.

Examples:

Example1:

Input:

Given List = [1, 3, 5, 8]
Given Tuple = (10, 20, 50, 60)

Output:

The sum of all list items after applying fsum() function =  17.0
The sum of all tuple items after applying fsum() function =  140.0

Example2:

Input:

Given List = [15, 20, 40, 10, 30]
Given Tuple = (45, 30, 10, 5, 6)

Output:

The sum of all list items after applying fsum() function =  115.0
The sum of all tuple items after applying fsum() function =  96.0

Program for fsum() 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 tuple as static input and store it in another variable.
  • Apply math.fsum() function to the given list to get the sum of all list items.
  • Store it in another variable.
  • Print the above result list sum.
  • Apply math.fsum() function to the given tuple to get the sum of all tuple items.
  • Store it in another variable.
  • Print the above result tuple sum.
  • 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 = [1, 3, 5, 8]
# Give the tuple as static input and store it in another variable.
gvn_tupl = (10, 20, 50, 60)
# Apply math.fsum() function to the given list to get the sum of all list items.
# store it in another variable.
lst_sum = math.fsum(gvn_lst)
# print the above result list sum.
print("The sum of all list items after applying fsum() function = ", lst_sum)
# Apply math.fsum() function to the given tuple to get the sum of all tuple items.
# store it in another variable.
tupl_sum = math.fsum(gvn_tupl)
# print the above result tuple sum.
print("The sum of all tuple items after applying fsum() function = ", tupl_sum)

Output:

The sum all list items after applying fsum() function =  17.0
The sum all tuple items after applying fsum() function =  140.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 tuple as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Apply math.fsum() function to the given list to get the sum of all list items.
  • Store it in another variable.
  • Print the above result list sum.
  • Apply math.fsum() function to the given tuple to get the sum of all tuple items.
  • Store it in another variable.
  • Print the above result tuple sum.
  • 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(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the tuple as user input using list(),map(),input(),and split() functions.
# Store it in another variable.
gvn_tupl = tuple(map(int, input(
   'Enter some random tuple Elements separated by spaces = ').split()))
# Apply math.fsum() function to the given list to get the sum of all list items.
# store it in another variable.
lst_sum = math.fsum(gvn_lst)
# print the above result list sum.
print("The sum of all list items after applying fsum() function = ", lst_sum)
# Apply math.fsum() function to the given tuple to get the sum of all tuple items.
# store it in another variable.
tupl_sum = math.fsum(gvn_tupl)
# print the above result tuple sum.
print("The sum of all tuple items after applying fsum() function = ", tupl_sum)

Output:

Enter some random List Elements separated by spaces = 15 20 40 10 30
Enter some random tuple Elements separated by spaces = 45 30 10 5 6
The sum of all list items after applying fsum() function = 115.0
The sum of all tuple items after applying fsum() function = 96.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.