Contenido del Curso
Python Functions: From Zero to Hero
Python Functions: From Zero to Hero
Recursion
What is recursion
?
Recursion is when a function calls itself. In Python, we can also create recursive functions.
def recursive_function(): ... recursive_function() # Recursive call of our function inside a function recursive_function()
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
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)}')
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
¡Gracias por tus comentarios!
Recursion
What is recursion
?
Recursion is when a function calls itself. In Python, we can also create recursive functions.
def recursive_function(): ... recursive_function() # Recursive call of our function inside a function recursive_function()
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
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)}')
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
¡Gracias por tus comentarios!
Recursion
What is recursion
?
Recursion is when a function calls itself. In Python, we can also create recursive functions.
def recursive_function(): ... recursive_function() # Recursive call of our function inside a function recursive_function()
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
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)}')
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
¡Gracias por tus comentarios!
What is recursion
?
Recursion is when a function calls itself. In Python, we can also create recursive functions.
def recursive_function(): ... recursive_function() # Recursive call of our function inside a function recursive_function()
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
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)}')
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