# Python Tutorial – 20 – Functions[4/6] recursion

///Python Tutorial – 20 – Functions[4/6] recursion

## Python Tutorial – 20 – Functions[4/6] recursion

Learn recursion with Python!

By |2017-05-10T20:30:13+00:00May 10th, 2017|Python Video Tutorials|22 Comments

1. Carson Cook May 10, 2017 at 8:33 pm - Reply

You deserve more likes. Take mine.

2. ahyaok100 May 10, 2017 at 8:35 pm - Reply

This is great. My professor doesn’t explain jack.

3. Rayhan bintoro May 10, 2017 at 8:35 pm - Reply

24 minutes of this video is much better than my 2 hours of lecture. well done my lecturer.

4. William Ansong May 10, 2017 at 8:39 pm - Reply

What kind of python text reader are you using?

5. William Ansong May 10, 2017 at 8:45 pm - Reply

Is it Sublime Text

6. Rhaego May 10, 2017 at 8:48 pm - Reply

How are you not getting decimal digits for your function at 12:00?

7. Enodi Audu May 10, 2017 at 8:49 pm - Reply

Wonderful tutorial. Now I can never forget the concept of recursion. Thank you

8. ramiro galvez May 10, 2017 at 8:50 pm - Reply

very useful! thanks!!

9. sombiefilmz May 10, 2017 at 8:55 pm - Reply

Great explanation. I tend to over think things. Now I understand. Thanks.

10. Ekin UĞUZ May 10, 2017 at 8:59 pm - Reply

Thank you from Turkey

11. Fikre Adera May 10, 2017 at 9:01 pm - Reply

Very nice explanation!!!!!!!

12. GabrieGliukaz May 10, 2017 at 9:04 pm - Reply

Base case for factorial could be rewritten: if n < 2: return 1

13. Wini Wiw May 10, 2017 at 9:07 pm - Reply

is there some purpose in naming the argument of the function "str_" with an underscore?

14. David Green May 10, 2017 at 9:08 pm - Reply

I don’t quite understand what the first program is doing to x. Why does the function return a 2 for x = 4, 3, 2, 1 etc? I might have skipped ahead in my python a bit. =P

15. Snehotosh Banerjee May 10, 2017 at 9:09 pm - Reply

Nicely explained.Thanks!!!

16. lujia May 10, 2017 at 9:15 pm - Reply

Thank you for posting this video, specially about the counting string part. Without your help I would of never finished this problem. Best regards.

17. Alex Nesta May 10, 2017 at 9:15 pm - Reply

Nice job! I was trying to teach myself some python techniques and became totally lost learning recursion. You made this clear for me to learn and provided ample examples. Thanks!

18. Spencer Sanderlin May 10, 2017 at 9:23 pm - Reply

def fib(n):
if n == 0:
return 0
elif n ==1:
return 1

else:
return (fib(n-1)+fib(n-2))

def main():
print("This program calculates numbers in the Fibonacci sequence.")
n = int(input("Which place in the sequence do you want to calculate? "))

print("Fibonacci("+str(n)+") is",fib(n))

main()

19. algernop krieger May 10, 2017 at 9:23 pm - Reply

(4-1)+2 =5 not 2
why 2?

20. ChiarloneD May 10, 2017 at 9:24 pm - Reply

Hello! It’s me again. During this video I was experimenting what happens if you slightly change the code and I realized that if you don’t put the brackets when calling the function you don’t receive an error but a weird outcome. My code was:

def triangle_numbers( n ):
if n == 0: return 0

return triangle_numbers(n – 1) + n

for n in range(20):
print triangle_numbers

The outcome is:

<function triangle_numbers at 0x1018e8d70>
<function triangle_numbers at 0x1018e8d70>
<function triangle_numbers at 0x1018e8d70>
<function triangle_numbers at 0x1018e8d70>
<function triangle_numbers at 0x1018e8d70>
<function triangle_numbers at 0x1018e8d70>
<function triangle_numbers at 0x1018e8d70>
<function triangle_numbers at 0x1018e8d70>
<function triangle_numbers at 0x1018e8d70>
<function triangle_numbers at 0x1018e8d70>
<function triangle_numbers at 0x1018e8d70>
<function triangle_numbers at 0x1018e8d70>
<function triangle_numbers at 0x1018e8d70>
<function triangle_numbers at 0x1018e8d70>
<function triangle_numbers at 0x1018e8d70>
<function triangle_numbers at 0x1018e8d70>
<function triangle_numbers at 0x1018e8d70>
<function triangle_numbers at 0x1018e8d70>
<function triangle_numbers at 0x1018e8d70>
<function triangle_numbers at 0x1018e8d70>

What does this mean?

Also, I realized there is a disadvantage for using the recursion method for the factorial function. That is, it can only calculate until the factorial of 998. If the user inputs 999 the program crashes saying "maximum recursion depth exceeded." Is that correct? Is there a way to fix and extend the boundaries of the recursion method?

Also, I realized that the time it takes for the computer to print out the Fibonacci numbers grows exponentially with a function more or less like:

T = 1.8774239815885*10^-6*(1.5383223043224^X)

Where T is the time it takes and X is the Fibonacci number to be calculated. This shows that to calculate the 40th Fibonacci number it will take approximately 1 minute. Is there a way to increase the performance of the program? If yes, how?

Sorry for asking so many questions. I am a curious person.

I will be anxiously awaiting your answer,

DC.

21. Andy Stubbins May 10, 2017 at 9:27 pm - Reply

I noticed on the Triangular numbers you needed a For-Loop to make it work. Is there a purely recursive way of doing it?

22. Christian Keusch May 10, 2017 at 9:29 pm - Reply

thank you