Today we are going to talk about recursive calls (or recursion) in Python.
Recursive functions are those that, within their algorithm, refer to themselves.
They tend to be very useful in very specific cases, but because when using them we can fall into beautiful infinite loops, we must take extreme programming and only use them when strictly necessary and there is no other programmable alternative way to avoid recursion.
Python supports recursive calls by allowing the function to call itself in the same way we would when calling another function.
Examples of recursion in Python
In this example, we create a function to know the factorial of the number 7. In case you don't know, the factorial of a number is obtained by multiplying all the integers and positive numbers between the number 7, in this case, and the number 1. That is, , 1 * 2 * 3 * 4 * 5* 6 * 7. Where after checking that the number is not 1, it calls itself again to obtain the factorial of 7.
Let's look at another simpler example.
In this function we program a small game where we have three attempts to guess the color of the lemon. After verifying that we have not passed the attempts, we generate the recursive call to re-launch the function until tried is greater than 3.
Remember that you can learn features like this and much more with our Master in Advanced Programming in Python for BigData, Hacking and Machine Learning.