def sum_of_list(lst):
return sum(lst)
print(sum_of_list([1, 2, 3, 4, 5]))
print(sum_of_list([10, 20, 30]))
print(sum_of_list([-5, 5, 10]))
#source code --> clcoding.com
Code Explanation:
Define the Function:
def sum_of_list(lst): → Defines a function named sum_of_list that takes a list lst as input.
Calculate the Sum:
return sum(lst) → Uses the built-in sum() function to calculate the total of all numbers in the list.
Calling the Function:
sum_of_list([1, 2, 3, 4, 5]) → Returns 1 + 2 + 3 + 4 + 5 = 15.
sum_of_list([10, 20, 30]) → Returns 10 + 20 + 30 = 60.
sum_of_list([-5, 5, 10]) → Returns -5 + 5 + 10 = 10.
Example Output
15
60
10
0 Comments:
Post a Comment