In this tutorial, we will learn various methods for generating the Fibonacci series in Python, including using a for
loop, recursion, a function, a list, input from the user, a while
loop, and the range
function.
fibonacci series in python using for loop
One way to generate the Fibonacci series in Python is by using a for
loop. The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. Here’s the Python code example for fibonacci series in python using for loop:
def fibonacci(n):
a, b = 0, 1
for i in range(n):
a, b = b, a+b
print(a)
# Test the function
fibonacci(10) # Output: 1 1 2 3 5 8 13 21 34 55
This code defines a function fibonacci
that takes in an integer n
as an argument and generates the first n
numbers of the Fibonacci series. It uses a for
loop to iterate through the range from 0 to n-1
and calculates the next number in the series using the formula a, b = b, a+b
. It then prints the number.
print fibonacci series in python using recursion
You can also write a recursive function to generate the Fibonacci series in Python. A recursive function is a function that calls itself with a smaller input until it reaches a base case, which is a predefined condition. Here’s the example for print fibonacci series in python using recursion:
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
# Test the function
for i in range(10):
print(fibonacci(i)) # Output: 0 1 1 2 3 5 8 13 21 34
This code defines a function fibonacci
that takes in an integer n
as an argument and generates the first n
numbers of the Fibonacci series using recursion. It has a base case of n <= 1
, which means that when n
is less than or equal to 1, the function returns n
. For larger values of n
, the function calls itself with n-1
and n-2
as arguments and returns their sum.
fibonacci series in python using function
You can also write a function to generate the Fibonacci series in Python and return the series as a list. This method allows you to store the series in a variable and use it for further processing.
def fibonacci(n):
a, b = 0, 1
result = []
for i in range(n):
result.append(a)
a, b = b, a+b
return result
# Test the function
print(fibonacci(10)) # Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
This code defines a function fibonacci
that takes in an integer n
as an argument and generates the first n
numbers of the Fibonacci series using a for
loop. It stores the numbers in a list called result
and returns the list when the loop is finished.
fibonacci series in python using list
You can also generate the Fibonacci series in Python by using a list and a while
loop. This method allows you to store the series in a list and use it for further processing. Here’s the Python code for this method:
def fibonacci(n):
result = [0, 1]
while len(result) < n:
result.append(result[-1] + result[-2])
return result
# Test the function
print(fibonacci(10)) # Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
The above fibonacci series in python using list example code defines a function fibonacci
that takes in an integer n
as an argument and generates the first n
numbers of the Fibonacci series using a list and a while
loop. It initializes a list called result
with the first two numbers of the series, 0 and 1. It then uses a while
loop to iterate until the length of result
is equal to n
. Inside the loop, it appends the sum of the last two elements of the list to the list. It then returns the list when the loop is finished. You can test the function by calling it with different values of n
and printing the output.
fibonacci series in python using input
You can also generate the Fibonacci series in Python by taking input from the user and using a for
loop. This method allows the user to specify the length of the series. Here’s the example for fibonacci series in python using input :
def fibonacci():
n = int(input("Enter the length of the Fibonacci series: "))
a, b = 0, 1
for i in range(n):
a, b = b, a+b
print(a)
# Test the function
fibonacci() # Output depends on user input
The above fibonacci series in python using input code example defines a function fibonacci
that takes no arguments and generates the Fibonacci series by taking input from the user. It prompts the user to enter the length of the series and stores it in a variable called n
. It then uses a for
loop to iterate through the range from 0 to n-1
and calculates the next number in the series using the formula a, b = b, a+b
. It then prints the number.
fibonacci series in python using while loop
You can also generate the Fibonacci series in Python by using a while
loop. This method allows you to specify the length of the series and store it in a list.
def fibonacci(n):
result = [0, 1]
while len(result) < n:
result.append(result[-1] + result[-2])
return result
# Test the function
print(fibonacci(10)) # Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
The above fibonacci series in python using while loop code example defines a function fibonacci
that takes in an integer n
as an argument and generates the first n
numbers of the Fibonacci series using a while
loop. It initializes a list called result
with the first two numbers of the series, 0 and 1. It then uses a while
loop to iterate until the length of result
is equal to n
. Inside the loop, it appends the sum of the last two elements of the list to the list. It then returns the list when the loop is finished.
fibonacci series in python using range
You can also generate the Fibonacci series in Python by using the range
function and a for
loop. This method allows you to specify the length of the series and store it in a list. Here’s the Python code for this method:
def fibonacci(n):
result = [0, 1]
for i in range(2, n):
result.append(result[i-1] + result[i-2])
return result
# Test the function
print(fibonacci(10)) # Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
The above fibonacci series in python using range code example defines a function fibonacci
that takes in an integer n
as an argument and generates the first n
numbers of the Fibonacci series using the range
function and a for
loop. It initializes a list called result
with the first two numbers of the series, 0 and 1. It then uses a for
loop to iterate through the range from 2 to n-1
. Inside the loop, it appends the sum of the last two elements of the list to the list. It then returns the list when the loop is finished.