Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Recursion | Recursion
Python Functions: From Zero to Hero
course content

Contenido del Curso

Python Functions: From Zero to Hero

Python Functions: From Zero to Hero

1. What is a Function?
2. Arguments or Parameters
3. Assigning a Default Value to a Parameter
4. An Unknown Number of Arguments
5. Functions within Functions
6. Recursion

bookRecursion

What is recursion? Recursion is when a function calls itself. In Python, we can also create recursive functions.

12345
def recursive_function(): ... recursive_function() # Recursive call of our function inside a function recursive_function()
copy

This is easier to understand by example. Let's consider how it is possible to realize search of a factorial of any integer by means of recursion. For example, factorial 7 is

7! = 1*2*3*4*5*6*7 = 5040

Remember:

0! = 1

1! = 1

0! = 1 and 1! = 1

1234567
def factorial(number): if number == 0 or number == 1: return 1 else: return (number * factorial(number-1)) number = 4 print(f'the factorial of {number} is {factorial(number)}')
copy

Explanation: Let's say we called the factorial(4) function. The function will be called, in which the value of the parameter n is equal to 4. It will check the condition n == 0, since the condition is false, the statement return n * factorial(n - 1) will be executed. But to calculate this value, the factorial(3) function will be called, since the parameter n has a value of 4. Now there will be two factorial functions in memory - one with the value of the parameter n equal to 4, and the other with the value of 3. In this case, the last function will be active. This function will in turn call factorial(2), which will call factorial(1), then factorial(0). In the case of this function, nothing else will be called, the function will simply return the value 1, and control will return to the factorial (1) function. It will multiply the value n = 1 by the value 1 returned by the factorial(0) function and return the resulting product equal to 1. Control returns to factorial(2), which will multiply n = 2 by the value of 1 returned by factorial(1) and return the product of 2. Factorial(3) will return 3x2=6, and factorial(4) will return 4×6==24.

Simply put, we have such a picture:

factorial(4)

4*factorial(3)

4*3*factorial(2)

4*3*2*factorial(1)

4*3*2*1*factorial(0)

4*3*2*1*1

4*6

Tarea

You have to implement a function, named sum_of_numbers, which calculates the sum of natural numbers from 1 to n. For example:

if number = 5, you will get such a result 5 + 4 + 3 + 2 + 1 = 15

To check, take number = 7

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 6. Capítulo 1
toggle bottom row

bookRecursion

What is recursion? Recursion is when a function calls itself. In Python, we can also create recursive functions.

12345
def recursive_function(): ... recursive_function() # Recursive call of our function inside a function recursive_function()
copy

This is easier to understand by example. Let's consider how it is possible to realize search of a factorial of any integer by means of recursion. For example, factorial 7 is

7! = 1*2*3*4*5*6*7 = 5040

Remember:

0! = 1

1! = 1

0! = 1 and 1! = 1

1234567
def factorial(number): if number == 0 or number == 1: return 1 else: return (number * factorial(number-1)) number = 4 print(f'the factorial of {number} is {factorial(number)}')
copy

Explanation: Let's say we called the factorial(4) function. The function will be called, in which the value of the parameter n is equal to 4. It will check the condition n == 0, since the condition is false, the statement return n * factorial(n - 1) will be executed. But to calculate this value, the factorial(3) function will be called, since the parameter n has a value of 4. Now there will be two factorial functions in memory - one with the value of the parameter n equal to 4, and the other with the value of 3. In this case, the last function will be active. This function will in turn call factorial(2), which will call factorial(1), then factorial(0). In the case of this function, nothing else will be called, the function will simply return the value 1, and control will return to the factorial (1) function. It will multiply the value n = 1 by the value 1 returned by the factorial(0) function and return the resulting product equal to 1. Control returns to factorial(2), which will multiply n = 2 by the value of 1 returned by factorial(1) and return the product of 2. Factorial(3) will return 3x2=6, and factorial(4) will return 4×6==24.

Simply put, we have such a picture:

factorial(4)

4*factorial(3)

4*3*factorial(2)

4*3*2*factorial(1)

4*3*2*1*factorial(0)

4*3*2*1*1

4*6

Tarea

You have to implement a function, named sum_of_numbers, which calculates the sum of natural numbers from 1 to n. For example:

if number = 5, you will get such a result 5 + 4 + 3 + 2 + 1 = 15

To check, take number = 7

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 6. Capítulo 1
toggle bottom row

bookRecursion

What is recursion? Recursion is when a function calls itself. In Python, we can also create recursive functions.

12345
def recursive_function(): ... recursive_function() # Recursive call of our function inside a function recursive_function()
copy

This is easier to understand by example. Let's consider how it is possible to realize search of a factorial of any integer by means of recursion. For example, factorial 7 is

7! = 1*2*3*4*5*6*7 = 5040

Remember:

0! = 1

1! = 1

0! = 1 and 1! = 1

1234567
def factorial(number): if number == 0 or number == 1: return 1 else: return (number * factorial(number-1)) number = 4 print(f'the factorial of {number} is {factorial(number)}')
copy

Explanation: Let's say we called the factorial(4) function. The function will be called, in which the value of the parameter n is equal to 4. It will check the condition n == 0, since the condition is false, the statement return n * factorial(n - 1) will be executed. But to calculate this value, the factorial(3) function will be called, since the parameter n has a value of 4. Now there will be two factorial functions in memory - one with the value of the parameter n equal to 4, and the other with the value of 3. In this case, the last function will be active. This function will in turn call factorial(2), which will call factorial(1), then factorial(0). In the case of this function, nothing else will be called, the function will simply return the value 1, and control will return to the factorial (1) function. It will multiply the value n = 1 by the value 1 returned by the factorial(0) function and return the resulting product equal to 1. Control returns to factorial(2), which will multiply n = 2 by the value of 1 returned by factorial(1) and return the product of 2. Factorial(3) will return 3x2=6, and factorial(4) will return 4×6==24.

Simply put, we have such a picture:

factorial(4)

4*factorial(3)

4*3*factorial(2)

4*3*2*factorial(1)

4*3*2*1*factorial(0)

4*3*2*1*1

4*6

Tarea

You have to implement a function, named sum_of_numbers, which calculates the sum of natural numbers from 1 to n. For example:

if number = 5, you will get such a result 5 + 4 + 3 + 2 + 1 = 15

To check, take number = 7

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

What is recursion? Recursion is when a function calls itself. In Python, we can also create recursive functions.

12345
def recursive_function(): ... recursive_function() # Recursive call of our function inside a function recursive_function()
copy

This is easier to understand by example. Let's consider how it is possible to realize search of a factorial of any integer by means of recursion. For example, factorial 7 is

7! = 1*2*3*4*5*6*7 = 5040

Remember:

0! = 1

1! = 1

0! = 1 and 1! = 1

1234567
def factorial(number): if number == 0 or number == 1: return 1 else: return (number * factorial(number-1)) number = 4 print(f'the factorial of {number} is {factorial(number)}')
copy

Explanation: Let's say we called the factorial(4) function. The function will be called, in which the value of the parameter n is equal to 4. It will check the condition n == 0, since the condition is false, the statement return n * factorial(n - 1) will be executed. But to calculate this value, the factorial(3) function will be called, since the parameter n has a value of 4. Now there will be two factorial functions in memory - one with the value of the parameter n equal to 4, and the other with the value of 3. In this case, the last function will be active. This function will in turn call factorial(2), which will call factorial(1), then factorial(0). In the case of this function, nothing else will be called, the function will simply return the value 1, and control will return to the factorial (1) function. It will multiply the value n = 1 by the value 1 returned by the factorial(0) function and return the resulting product equal to 1. Control returns to factorial(2), which will multiply n = 2 by the value of 1 returned by factorial(1) and return the product of 2. Factorial(3) will return 3x2=6, and factorial(4) will return 4×6==24.

Simply put, we have such a picture:

factorial(4)

4*factorial(3)

4*3*factorial(2)

4*3*2*factorial(1)

4*3*2*1*factorial(0)

4*3*2*1*1

4*6

Tarea

You have to implement a function, named sum_of_numbers, which calculates the sum of natural numbers from 1 to n. For example:

if number = 5, you will get such a result 5 + 4 + 3 + 2 + 1 = 15

To check, take number = 7

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 6. Capítulo 1
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
some-alt