Motive There are different types of modules in python which not everyone knows so we thought of introducing them to you guys. Let's start! Note: This is not a tutorial of how to use these but is to show that these modules also exist and if you want to know more you can visit there own page and if you want us to explain leave a comment below. Links to there page are also given below. Hope you found something useful. 1.Wikipedia Yes, there is a module called Wikipedia in python, you can easily install Wikipedia by pip install wikipedia . We cannot rely on this module for advanced use. It was designed for ease of use and simplicity. Learn More Here . Here is a simple example: import wikipedia wikiPython = wikipedia.summary("Python", sentences=2) print(wikiPython) One big disadvantage is that it works only if the interne...
Motive
To get a job as a programmer you need to pass the coding interviews in which certain questions/algorithms are asked to be solved.
The Project
What is the Fibonacci Sequence?
The Fibonacci Numbers are a series of numbers in which each number is the sum of the proceedings numbers for example 1,1,2,3,5,8,13 etc. Learn more Here.
1. We are going to create a function that takes the nth number i.e F1=1, F2=1, F3=2, F4=3, F5=5, F6=8, F7=13 etc. In F4=3, 4 is the nth number and in F6=8, 6 is the nth number.
But before that, we will make a list of the Fibonacci numbers and we can access them with its index.
Let's begin, paste this code into your python ide:
Run the code for about 2 sec and then press ctrl+c you will get something like this:
First, we make a variable called a and give it the value of 1
Then, we make a list called list in which we put 0 and 1
Now, we make a while loop and make a variable called fib we take the last two numbers in the list and add them and then append the new number to the list, fib = list [a-1] + list [a], here we set a as the index of the last number in the list at the beginning it is set to 1 i.e the second number in the lists (1)then after we find the third number we change the value of a + 1 and add the new number to the list.
This is how the code run:
a=1 | a=2 | a=3
list=[0,1] | list = [0,1,1] | list =[0,1,1,2]
while True: | while True: | while True:
fib = 0 + 1 | fib = 1+1 | fib = 1+2
a +=1 | a+=1 | a+=1
list.append(1) | list.append(2) | list.append(3)
print(list) | print(list) | print(list)
and continues like this. So now we have all the numbers of the Fibonacci sequence. Instead of printing the whole list, we could take the nth number and print only the corresponding value.
For that let's make a function
Here I made a function called Fib and give a parameter called num then we check if num lesser than 0
is so we print Give a value greater than or equal to 0.
if num is equal to zero then we will print 0
and if num is greater then 0 we will do the same as we did above but here we will append the list only till a == num i.e until the index is equal to the nth number and the print the nth from the list.
Here we tried Fib(6) and got 8 printed on the console. So this works, now let's make some changes to make it more perfect.
I have made small changes look down below:
If you have any doubts or errors in the program feel free to comment down below and I will respond as early as possible. And if I have made any mistake please correct me in the comment box below.
Comments
Post a Comment