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

Svep för att visa menyn

book
Recursion

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

Uppgift

Swipe to start coding

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

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 6. Kapitel 1

Fråga AI

expand
ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

book
Recursion

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

Uppgift

Swipe to start coding

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

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 6. Kapitel 1
Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Vi beklagar att något gick fel. Vad hände?
some-alt