From c4e57a9e1fb08acdeb49f4e232a94a79b747ead8 Mon Sep 17 00:00:00 2001 From: musk-program <69678390+musk-program@users.noreply.github.com> Date: Fri, 30 Oct 2020 14:27:38 +0530 Subject: [PATCH] Python 3 code to find sum --- python sum | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 python sum diff --git a/python sum b/python sum new file mode 100644 index 00000000..34f858f5 --- /dev/null +++ b/python sum @@ -0,0 +1,31 @@ +# Python 3 code to find sum +# of elements in given array +def _sum(arr): + + # initialize a variable + # to store the sum + # while iterating through + # the array later + sum=0 + + # iterate through the array + # and add each element to the sum variable + # one at a time + for i in arr: + sum = sum + i + + return(sum) + +# driver function +arr=[] +# input values to list +arr = [12, 3, 4, 15] + +# calculating length of array +n = len(arr) + +ans = _sum(arr) + +# display sum +print ('Sum of the array is ', ans) +