<jupyter_start><jupyter_text>All the IPython Notebooks in this lecture series by Dr. <NAME> are available @ **[GitHub](https://github.com/milaan9/04_Python_Functions)**  Python RecursionIn this class, you will learn to create a recursive function (a function that calls itself again and again).  What is recursion?Recursion is the process of defining something in terms of itself.A physical world example would be to **place two parallel mirrors facing each other**. Any object in between them would be reflected recursively.  Python Recursive FunctionIn Python, we know that a **[function](https://github.com/milaan9/04_Python_Functions/blob/main/001_Python_Functions.ipynb)** can call other functions. It is even possible for the function to call itself. These types of construct are termed as recursive functions.The following image shows the working of a recursive function called **`recurse`**.Following is an example of a recursive function to find the factorial of an integer.Factorial of a number is the product of all the integers from 1 to that number. For example, the factorial of 6 (denoted as 6!) is ```python1*2*3*4*5*6 = 720.```<jupyter_code># Example 1:

def factorial(n):
    """This is a recursive function to find the factorial of an integer"""

    if n == 1:
        return 1
    else:
        return (n * factorial(n-1))  # 3 * 2 * 1 = 6


num = 3
print("The factorial of", num, "is", factorial(num))<jupyter_output>The factorial of 3 is 6<jupyter_text>In the above example, **`factorial()`** is a recursive function as it calls itself. When we call this function with a positive integer, it will recursively call itself by decreasing the number.Each function multiplies the number with the factorial of the number below it until it is equal to one. This recursive call can be explained in the following steps.```pythonfactorial(3)           1st call with 33 * factorial(2)       2nd call with 23 * 2 * factorial(1)   3rd call with 13 * 2 * 1              return from 3rd call as number=13 * 2                  return from 2nd call6                      return from 1st call```Let's look at an image that shows a step-by-step process of what is going on: Our recursion ends when the number reduces to 1. This is called the base condition.Every recursive function must have a base condition that stops the recursion or else the function calls itself infinitely.The Python interpreter limits the depths of recursion to help avoid infinite recursions, resulting in stack overflows.By default, the maximum depth of recursion is **`1000`**. If the limit is crossed, it results in **`RecursionError`**. Let's look at one such condition.<jupyter_code>def recursor():
    recursor()
recursor()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0723155588020453, 'fraction_numerical': 0.02154857560262966, 'mean_word_length': 4.091078066914498, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '244832', 'license': 'MIT', 'n_tokens_mistral': 778, 'n_tokens_neox': 701, 'n_words': 374, 'path': '005_Python_Function_Recursion.ipynb', 'repo_name': '0525serendipity/04_Python_Functions', 'size': '2744'}