since the list may be arbitrarily longyou need loop hence you must find pattern so that you can keep reusing the same statements in the loop obviously you are using each number in the sequence in order you also generate sum in each stepwhich you reuse in the next step the pattern is differenthoweverin the first line + is there is no previous sumand you use two elements from the list the is not added to previous sum although it is not the shortest way to do the calculation by hand is sum of we can make the pattern consistent and calculatestart with sum of is is is is is the answer then the second part of each sum is number from the listnums if we call the number from the list numthe main calculation line in the loop could be nextsum sum num the trick is to use the same line of code the next time through the loop that means what was nextsum in one pass becomes the sum in the next pass one way to handle that issum for num in numsnextsum sum num sum nextsum do you see the patternagain it is initialization loop headingmain work to be repeated preparation for the next time through the loop sometimes the two general loop steps can be combined this is such case since nextsum is only used oncewe can just substitute its value (sumwhere it is used and simplify tosum for num in numssum sum num so the whole functionwith the return statement is def sumlist(nums)'''return the sum of the numbers in the list nums ''sum for num in numssum sum num return sum the example program sumnums py has the code above with the following line added at the end to test the function (not indentedrun sumnums py print(sumlist([ ])the pattern used here is certainly successive modification (of the sum variableit is useful to give more specialized name for this version of the pattern here it follows an accumulation pattern beginning with python