The Fibonacci series is complicated using a programming language, and writing a Python function that takes the limit as an argument of the Fibonacci series and prints until that limit given as an argument — is very challenging. After reading this article, you will know how to write a Python function that will generate a Fibonacci series till the limit you set as a function argument.
But what’s a Fibonacci series? A Fibonacci series is a series where the first term starts from 0 and the 2nd term is 1. For the third term, it’s simply the addition of the 1st and 2nd terms, so 0+1 = 1. For the 4th term, it’s the addition of the 2nd and 3rd terms, so 1+1 = 2. So this goes on and on, making an infinite series of numbers. The first 10 terms of a Fibonacci series are 0,1,1,2,3,5,8,13,21,34.
So, let’s start with the code. At first, let’s just simply put on the logic without making a function.
first_number = 0
second_number = 1
#here as we know the 1st and 2nd term is always 0 and 1 thats why
#we will set variables with 0 and 1
print(first_number, end= " ")
print(second_number, end= " ")
#we will use end parameters for both the print statements
#so that our outputs come in a single line.
Next, we set up another variable for our while loop. We named it counter and set its value to 0 because our loop will work from 0. Remember that our objective is to make a function which will produce a Fibonacci series, and we have to set our parameter as the range or term of the series. As we didn’t set any function yet, we will carry out code logic with the alphabet “X”.
counter = 0
#as you can see i have set the counter to 0 as our loop will start from 0
while counter < X:
#here the X will be just acting as a dummy variable we will
#change it after we have learnt the logic.
#counter is < X because the loop will run till it meets X,
#so X is the argument we will use for a function.
new_number = first_number + second_number
#new number would be the number which adds the first number
#and the second number.
Now we will verify if the new_number
is less than the argument.
if new_number < X:
print(new_number, end=" ")
first_number = second_number
second_number = new_number
#note here that we are swapping and switching the variables.
else:
print()
count = new_number
All we have to do is put all of these into a function.
Congratulations you have successfully made the Fibonacci series with a function in Python.
The final code:
def fibonacci(X):
#notice that i used the X as an argument in the function.
first_number = 0
second_number = 1
print(first_number, end = " ")
print(second_number, end = " ")
counter = 0
while counter < X :
new_number = first_number + second_number
if new_number < X:
print(new_number, end = " ")
first_number = second_number
second_number = new_number
else:
print()
counter = new_number
fibonacci(10)
#i have used 10 as an argument, the output will be 0 1 1 2 3 5 8
#it's till 8 because after 8 the series would be greater than 10.
Note for the reader
This is my first time writing in Dev.to blogs. But you know you can press the reaction button maybe for 10 times 💜? The higher you go, the more it motivates me to write more stuff for you!
Top comments (0)