How to Use sum() Function in PythonPython Basics


The sum() function returns a number which is the sum of all items in an iterable.

Example

#!/usr/bin/python3

print("sum(4,3,1):  ", sum([4,3,1]))
print("sum(4.8,2):  ", sum([4,8,2]))
print("sum(4.5,11):  ", sum([4.5,11]))
print("sum(-1.8,2):  ", sum([-1,2]))
Output:

sum(4,3,1):   8
sum(4.8,2):   14
sum(4.5,11):   15.5
sum(-1.8,2):   1

Syntax

sum(iterable)

Parameters

iterable — Iterable objects such as lists, tuples, collections.

Return Value

It returns a number which is the sum of all items in an iterable.

© 2024 Cheerlearn.com, all rights reserved. Privacy Policy | Contact Us