Python - Function Function Recursion

Introduction

To sum a list of numbers, we can use recursive function.

Demo

def mysum(L): 
   if not L: 
       return 0 # from w  w w.  j a v a 2s .  com
   else: 
       return L[0] + mysum(L[1:])           # Call myself recursively 
d=mysum([1, 2, 3, 4, 5]) 
print( d )

Result

Here, this function calls itself recursively to compute the sum of the rest of the list.