you are in good company when you struggle with recursion. Recursion in computer science means, that a function calls itself.
To explain it, I will start with a more easy function:
items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def add_list(items):
if len(items) == 1:
return items[0]
op1 = items[0]
op2 = add_list(items[1:])
return op2 + op1;
print(add_list(items))
This function shall add all elements of the list called "items". The term op1 = items[0] assignes the first list item to op1. After that the function calls itself (the recursion) with a reduced set of list items namely items without 1. The term items[1:] cuts of the first item which results in [2, 3, 4, 5, 6, 7, 8, 9, 10]. The procedure is waiting here until the recursive call finishes. You can assume this as a new function instance of add_list is called.
Now the same procedure begins again with the reduced list. This goes on until the list only contains one element. Then the magic happens. The whole recursion chain rolls back.
The term
if len(items) == 1:
return items[0]
is hit. The last recursion call returns the last item of the list items. Now the chain returns to the former call of add_list. op2 gets the last item of items, here 10. This chain element will return (10 + 9). Then the recursion rolls back to the former layer and op2 will get the value (10 + 9). This chain element will return (10 + 9) + 8; And so on.
The same happens to the function find_max. But instead of summing the values the chain of recursion will return the highest value by :
if op1 > op2:
return op1
else:
return op2
instead of return op2 + op1.