Thursday, February 27, 2014

Week 7 -- Recursion

Recursion is a very useful method in python. It's basically using itself in its own function. By using recursion, programmers can write shorter and easier functions.
For example, when we want to sum up the numbers in a list:
We can write:





This function works when s is just a simple list with numbers. But when s is a nested list, the problem appears:





And this is when we can use recursion:











We can see that when recursion is used, the function become easier to built.