In: Computer Science
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
we can sum directly by using default 'sum (list)' or by looping through the code. Below I attatched the code by looping. Name of the function is list_sum, parameter is integers , and also did sample run using list [1,2,3,4] . It should return 10 = 1+2+3+4
code:
output :
raw_code :
def list_sum(integers):
total_sum = 0
#iterating list to sum elements
for i in integers:
total_sum += i #summing elements
return total_sum #returning sum
integers = [1,2,3,4]
print(list_sum(integers)) #calling function
**do comment for queries and rate me up****