Join Regular Classroom : Visit ClassroomTech

Python Set – sum() Function

It is a function in python that returns the sum of all the elements in the set. It doesn’t modifies the given set. It returns a integer type value.

Syntax:
sum(set)

Also Read: Python Set – min() Function

Example 1:

# Python code to understand sum() Function in set
# www.codewindow.in

s1 = {2, 5, 8, 7, 9}
s = {10, 5, 1}
print(sum(s1))
print(sum(s))

Output:

31
16

Explanation:
Here, the sum of all the elements in set s1 is 31 and the sum of all the elements in set s is 16.

Example 2:

# Python code to understand sum() Function in set
# www.codewindow.in

s1 = {7, 100}
s = {7, 8, 9}
print(sum(s1))
print(sum(s))

Output:

107
24

Explanation:
Here, the sum of all the elements in set s1 is 107 and the sum of all the elements in set s is 24.


Follow Us

You Missed

Also Checkout