How To Code Fibonacci Sequence In Python Using Recursion | Python for Beginners Tutorials

preview_player
Показать описание
This video will demonstrate how to program / code Fibonacci series / sequence in Python with recursion!

Fibonacci Sequence:
Sequence of numbers where a number is the sum
of the 2 numbers that came before it.
The sequence' first digits are 0 and 1.

(0,) 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...

Note: The zero is sometimes not mentioned.

def fibonacci(i):
if i == 0:
return 0
elif i == 1:
return 1
else:
return fibonacci(i-2) + fibonacci(i-1)

for x in range(10):
print(fibonacci(x))

Subscribe for more Python for Beginners Tutorials.

*** SOCIAL MEDIA ***

#Code #Programming #Python
Рекомендации по теме
Комментарии
Автор

Your explanation clicked, after having watched 5 different videos, you did an awesome job. Subscribed to your channel

InayetHadi
Автор

instead of if(i ==0): return 0 and elif(i ==1): return 1, can I use if(n<=1) return n?

Levittt
Автор

Straight to the point. Thank's mate.

bobpage
Автор

How do the system know that the previous two term is which number? Like: fib(7), how did the system automatically know that the previous two number is 5 and 8 when there is no array for the function to assess?

zclee
Автор

Not sure if you will respond to this, since its nearly a year old but i dont understand the method behind this, i understand fib(7) returns fib(5) + fib(6) but how does it know what fib(5) and fib(6) is?

umarpatel
Автор

So at 2:00 its not subtracting the numbers 2 and 1 from the range it is moving backwards from the range.. so it is calculating ranges 3 & 4?

craiggambetta
Автор

The result of fibbonacci(5) would be:
Fibbonacci(4)+ fibbonacci(3) = finbonacci(1) = fibbonacci(2)+fibbonacci(1)+ finbonacci(1)+fibbonacci(0)+ finbonacci(1)+fibbonacci(0)+ 1 = finbonacci(1)+fibbonacci(0)+ 1 +1+0 +1 +0 + 1 = 1+0 + 1 +1+0 +1 +0 + 1 = 5

suicidetrick
Автор

Wonderfully you explained sir thank you I understood clearly 😊

kapuramanisoren
Автор

def fib(n):
if(n==0 or n==1):
return n
else:
return fib(n-1)+fib(n-2)
print(fib(4))
Isn't the elif unnescessary?

HarsithR-jt
Автор

how do you put all of this into a function? I don't want to run a loop outside the function. how can I avoid using for loop outside the function to get the result? it has to be a recursive function.

akzork
Автор

Hiii sir can you help I have a assignment to add 3 previous no. In this video add only two previous no plz help me

MandeepKaur-wxer
Автор

fibonnaci is a reserved word and not needed to be defined in python?

weatherphobia
Автор

the only thing I liked like about the video is the keyboard clicking sound

boomflix
Автор

The code is extremely slow, because of repeated calculations. Compare to this:
def fibonacci_memo(n, memo={}):
if n in memo:
return memo[n]
if n == 0:
return 0
elif n == 1:
return 1
memo[n] = fibonacci_memo(n - 1, memo) + fibonacci_memo(n - 2, memo)
return memo[n]

for x in range(13):
print(fibonacci_memo(x))

ceuw
Автор

U explained very well but not getting out put

BHARATHKUMAR-shxm
Автор

the sound of your keyboard is so fucking loud

adnanyaqobi