In: Computer Science
#This is the code to calculate sum of list elements:
def my_sum_algo(list1): #here the function is declared
sum=0 # a variable sum is taken and intially declared 0
for i in range(0,len(list1)): # loop is runned from 0 to last index of list; len() funcion returns list size
sum=sum+list1[i] # at each step each element's value is added in sum variable
return sum #sum is returned here
# here we write code to test our function:
list1=[9,4,5] #list is intialised
answer=my_sum_algo(list1) #our created function my_sum_algo is called and its return value is stores in answer
print(answer) #answer is printed
For code identation refer the screenshot.
Output as per my list taken.
----------------------------------------------------------------------------------------------------------------------------------------------
Pseudocode :
for i in range(0,len(list1))
sum=sum+list1[i]
# add each element and store in variable sum.