sum()
Python Built in functions in Python
sum(iterable , start )
iterable :input iterable
start : Optional , number to be added to after getting the sum of elements.
Return the sum of the elements plus start if available.
Using list
my_list=[1,2,5]
print(sum(my_list)) # 8
Using tuple
my_tuple=(1,2,5)
print(sum(my_tuple)) # 8
Using start ( Optional )
my_list=[2,4,5]
print(sum(my_list,5)) # 16
Data type of output
sum() returns same dtype of input number.
print(type(sum([4,6,1]))) # <class 'int'>
print(type(sum([1.4,6,1]))) # <class 'float'>
Difference between fsum() and sum()
fsum() is included in Python math module so we have to import it before using. Where as sum() is part of built in functions of core Python so no need to import any library.
fsum() returns always float dtype. However sum() returns the same dtype of input number.
« math.fsum()
Iterator »
any() »
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com