#38 Python Tutorial for Beginners | Fibonacci Sequence

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back aliens my name is Levin Wendy and in this video we'll try to plant Fibonacci series in Python but what is Fibonacci series now the number which you can see here this is a Fibonacci series but can you find a pattern here of course I'd if you want to print something in programming there should be some pattern so you can pause the video guess the pattern and then we'll I will let you know what is the exact pattern here so if you see the first number second number which is zero and one now if you add them you will get one which is that next number then the second number and third number which is one one will make it two and that's the fourth number then we have one and two if you add them you will get three so that's what you have to do you have to add two numbers so to get the next number so basically you have to add the second last number and the last number to get the next number right and that's how you can build a Fibonacci series of course there's a there's a use of this in mathematical world in the world of course but here we are not concerned about that logic we are concerned about how can you print this one in Python so let's do it now of course first of all you want to plain so just to make it simple what I will do is I will call a function named as fib and in this fib I want to specify so you know you can also mention till what point you want it I mean how many numbers you want top five Fibonacci all first ten first 15 so at this point let's go first 5 let's print them now unfortunately we don't have this function inbuilt so we have to build this function by ourselves and the way you can build functions in Python is very simple you say def let's mention the function name as fib itself will give a bracket here and we have to accept that number and let's accept that in n because that makes sense right ok let's define this function so what you want to do here now if you can see the code we first we want to print 0 so let's do that quickly so let's print 0 and then let's print 1 so we are printing 0 and then we are printing one that's what we want right and after that we can print 1 2 3 4 but at this point let's run this code now if you can see on the right hand side we also have a console very see the output so let's run this code and you can see we got 0 1 so that step is done now if you are thinking I will be printing 1 then 2 then 3 then 5 we can do that but then that how many numbers I can do that maybe I can do it for 50 12 5 I'm in first 50 first 100 it should be automated why it should be done by computer and that's where you want to repeat these steps why do you want to add and you have to repeat and in programming if you want to repeat something we have seen how to do that that's why you can use a loop here so what I'm going to do is it's tough having that in 0 & 1 directly we can use two variables I can say a will hold 0 and B will hold 1 and here instead of having 0 1 we can print a and we can print B is that simple but now I also want to print the next number right so after printing 0 1 I want to print next next number and that we should be repeated and to repeat something there is a loop right and famous one we all love which is for loop so let's use fall here we'll say for I that's the counter we normally use for I in and we have to use a range now so I will say range now since I'm printing the first two numbers so we don't have to start the range from 0 let's start this range from 2 because the 1st and 2nd is done so let's start with 2 and then I want to go till end right because that's how many numbers we want to print in fact you know you should be going with 3 because first and second numbers printed we have to go from 3 but unfortunately when you say n here and simply means so if you say 10 it will it will take 9 so if you start from 2 in a show of 3 you can actually go from 9 it will be around 10 numbers okay now once you call that loop you want to print the next number it's actually very simple you can simply print the next number by saying a plus B don't you think about job is done here I feel on this code you will get the output this is one of the easiest let's try first oh okay it's not working because something went wrong here you can see the moment you say a plus B we got one but we are not getting the next number because every time you add you will simply add a zero and one you don't want that so what you do is initially when you say 0 1 that is a and B for the next iteration what if you shift this numbers what if you shipped a and B now what will happen is the new a and B will be one one once you add them then again you have to shift so it will be new a and B would be one and two that's what we want right okay so how do we do that and for that we have to use the third variable right we just see in this case so let's use C and we'll say C is equal to a plus B initially that's what we want and then once we add the value of a and B which is they didn't see let's slap okay so how do we slap so I will say a is equal to B and then we'll say B is equal to C so that's our you shift now once you got shifted we got new values for a and B we can simply okay with let's print see in the next iteration you will add the new values and that will go in C and then this will be continued I hope this will work now so let's run this code and yes it worked so you can see we got first five Fibonacci numbers okay let me just reduce the number of lines here we don't want extra spaces so this is awesome like in fact you can ask for this value of five from the user so you can say hey user enter number so whatever number is entered by the user you can pass it here and then you can pass the same number in the Fibonacci in the fifth function and it will do the calculation for you okay but we have a twist here what if someone says hey I want the first number that's it in this case also you can see we got two values we wanted one which we got two values so in this case also we can apply some if conditions so you can say if you know before printing a and B you can say if if the number is equal to equal to 1 in this case only print a so we'll print a right so this part the next part should be done in the else and then we can give a tab here so that's what you can do so if your number is 1 you can print only 1 and you can see this works you will get only 0 if you say you want to it of course you will print two so you can see we got 0 1 and whatever number you provide so if you provide 10 it will give you first 10 Fibonacci numbers right in fact let me just expand this so that you can see all the numbers okay so you can see we got first 10 few bunch of numbers in fact you can do it for hundred or whatever number you want in fact you can also do one more so what if someone says the value is minus 3 now in this case if you are saying you don't want to do that it should be invalid right so you can see you're still getting 2 we don't want that so this is your assignment if anyone enters a number below 0 you should check that first if the number is negative and then only you should be doing all the other processing so that with the assignment if you complete that in the comment section do that mention your answer that's what you can do ok that's how you do Fibonacci in Python okay so we have some more modification here so what we are asking from the user is the total number of we want number you want to see but what if you don't ask for the total numbers you want to ask for the number 100 let's say if I say hundred now you will get first hundred future numbers we don't want that what we want is I want to print the last number which should be less than 100 so next this time second assignment for you is if I say hundred it is not the number of Fibonacci numbers we want we want the last number should be less than hundred so that means it to the end at 89 that will be an assignment so that's about Fibonacci in Python if you have any questions write me in the comment section and do subscribe for further videos bye bye
Info
Channel: Telusko
Views: 414,187
Rating: 4.905942 out of 5
Keywords: telusko, navin, reddy, tutorial, java, python
Id: 7Sv4NmvdHcw
Channel Id: undefined
Length: 8min 1sec (481 seconds)
Published: Wed Mar 13 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.