+ INFORMATION

Share on social networks!

Recursion in Python

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

AiwXiWpjDjGF2KGpmhN4SZ56E9iX6VkGWMqVD0uq2 jUcVop5lyQoWKnDgAVvcn QdXL7iqaditg re5xU2Mm tEhSA

yKkA0YnLRH gGtTiuhqu2WV6QQQsBO0wKJCVIqBm81OkA JBrRgYRpDDm8maSjFicPXIh6DerdB0NchRHtEFRtU cZPmtWfv8R3QgMtdEsyvAtRpsdrmYpJD9YWRFtE ZcMfvmZl4RN85F3j TA=s0

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.

0JjwACAcjH8 rc4ghpCexT86unrEJ737qa9tu1Ldh84aElOYhtCp16SQE6yvzs2HrMu

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.

0OZxuNCB6gZ6RCEYv8ZLDGxTZn OXatghO8v udWWLSLs9mdMfWjJW1rsTERl6 94CmEvHpiDoR UGA9xSmUajrLCX y5OICt4rBhpC8wMPOtDiDL4v3ur5nK4j 1hMTfh527h2ZN6pY5UMYQ= s0

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.

Subscribe to our newsletter to stay up to date with all the news

EIP International Business School informs you that the data in this form will be processed by Mainjobs Internacional Educativa y Tecnológica, SA as the person responsible for this website. The purpose of collecting and processing personal data is to respond to the query made as well as to send information about the services of the data controller. Legitimation is the consent of the interested party.
You can exercise your rights of access, rectification, limitation and deletion of data in compliance@grupomainjobs.com as well as the right to file a claim with the supervisory authority. You can consult additional and detailed information on Data Protection in the Privacy Policy that you will find in our Web page
marter-in-python

Leave a comment