sum()

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


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer