Python math.fsum() Method
Python math.fsum(iterable) The method computes the sum of the elements in an iterable (tuple, array, list, etc.).
Python version: 2.6
Syntax
The syntax of the math.fsum() method is as follows:
math.fsum(iterable)
Parameter Description:
- iterable -- required, an iterable, which can be a list, tuple or array. If the elements of the iterable are not numbers, a TypeError is returned.
Return Value
A floating-point value representing the sum of all elements in the iterable.
Example
The following example computes the sum of all elements:
Example
Import the math package
import
math
Calculating the sum of all elements
(
math
.
fsum
(
[
1
,
2
,
3
,
4
,
5
]
)
)
(
math
.
fsum
(
[
100
,
400
,
340
,
500
]
)
)
(
math
.
fsum
(
[
1.7
,
0.3
,
1.5
,
4.5
]
)
)
Output result:
15.0
1340.0
8.0
Python math Module