{"id":51283,"date":"2021-09-23T14:53:05","date_gmt":"2021-09-23T12:53:05","guid":{"rendered":"https:\/\/eiposgrados.com\/?p=43857"},"modified":"2021-11-16T17:30:47","modified_gmt":"2021-11-16T16:30:47","slug":"recursion-in-python","status":"publish","type":"post","link":"https:\/\/eiposgrados.com\/eng\/python-blog\/recursion-in-python\/","title":{"rendered":"Recursion in Python"},"content":{"rendered":"<p>Today we are going to talk about recursive calls (or recursion) in Python.&nbsp;<\/p>\n\n\n\n<p>Recursive functions are those that, within their algorithm, refer to themselves.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>Python supports recursive calls by allowing the function to call itself in the same way we would when calling another function.<\/p>\n\n\n\n<h2 class=\"gb-headline gb-headline-9ce2446f gb-headline-text\">Examples of recursion in Python<\/h2>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/AiwXiWpjDjGF2KGpmhN4SZ56E9iX6VkGWMqVD0uq2_jUcVop5lyQoWKnDgAVvcn-QdXL7iqaditg_re5xU2Mm-tEhSA-PMvgp0Uud2aRELNLnhY587u2q4xOpybdXJuMxThxxuBq67tx4u0caw=s0\" width=\"100%\" alt=\"\" title=\"\"><\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-bottom is-layout-flow wp-block-column-is-layout-flow\">\n<p><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/yKkA0YnLRH-gGtTiuhqu2WV6QQQsBO0wKJCVIqBm81OkA_JBrRgYRpDDm8maSjFicPXIh6DerdB0NchRHtEFRtU-cZPmtWfv8R3QgMtdEsyvAtRpsdrmYpJD9YWRFtE-ZcMfvmZl4RN85F3jTA=s0\" width=\"175\" height=\"38\" alt=\"\" title=\"\"><\/p>\n<\/div>\n<\/div>\n\n\n\n<p>In this example, we create a function to know the factorial of the number 7. In case you don&#039;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.<\/p>\n\n\n\n<p>Let&#039;s look at another simpler example.<\/p>\n\n\n\n<p><img decoding=\"async\" width=\"100%\" src=\"https:\/\/lh4.googleusercontent.com\/0JjwACAcjH8_rc4ghpCexT86unrEJ737qa9tu1Ldh84aElOYhtCp16SQE6yvzs2HrMu-LgGS8HAphiM7batF__oRusyt70flqPM0sXRBqvwA7yI0SiDpTREJzCLsgKR5dD_Vr0txwp5pLXMzFQ=s0\" alt=\"\" title=\"\"><\/p>\n\n\n\n<p>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 <strong><em>tried<\/em><\/strong> is greater than 3.<\/p>\n\n\n\n<p><img decoding=\"async\" width=\"100%\" src=\"https:\/\/lh5.googleusercontent.com\/_0OZxuNCB6gZ6RCEYv8ZLDGxTZn-OXatghO8v-udWWLSLs9mdMfWjJW1rsTERl6-94CmEvHpiDoR-UGA9xSmUajrLCX-y5OICt4rBhpC8wMPOtDiDL4v3ur5nK4j-1hMTfh527h2ZN6pY5UMYQ=s0\" alt=\"\" title=\"\"><\/p>\n\n\n\n<p>Remember that you can learn features like this and much more with our <a href=\"https:\/\/eiposgrados.com\/eng\/master-in-python\/\">Master in Advanced Programming in Python for BigData, Hacking and Machine Learning<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>In Python, recursive functions are those that, within their algorithm, refer to themselves. Find out all the details about them on our blog.<\/p>","protected":false},"author":1,"featured_media":52416,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[407],"tags":[],"class_list":["post-51283","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-python"],"acf":[],"_links":{"self":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/posts\/51283","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/comments?post=51283"}],"version-history":[{"count":0,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/posts\/51283\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/media\/52416"}],"wp:attachment":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/media?parent=51283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/categories?post=51283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/tags?post=51283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}