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.
Please find below code with explanation in each line as comments. Dont forget to Give a Like.
Please refer below screenshot for indentation and output.
Code:
def sum_of_list(list_integers):
total=0
#for each element in the list
for element in list_integers:
#we are adding each element to total one by one
total+=element
#returning the total value
return total
#taking the input from user
#user should give integers with spaces it will convert into list with name list1
list1=list(map(int,input("Enter integers with spaces:").split()))
#calling the sum_of_list function by passing the list of integers as parameters
print("The sum of elements in the list:",sum_of_list(list1))
