The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones. The sequence starts with 0 and 1.
def fibonacci(n):
fib_series = [0, 1]
for i in range(2, n):
next_num = fib_series[-1] + fib_series[-2]
fib_series.append(next_num)
return fib_series
# Example usage:
n = 10
print("Fibonacci Series up to", n, "terms:")
print(fibonacci(n))
Fibonacci Series up to 10 terms:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
The function fibonacci(n)
takes an integer n
as input and returns the Fibonacci series up to the n
th term.
It initializes fib_series
with the first two Fibonacci numbers, 0 and 1. Then, it iterates from the third term up to the n
th term, calculating each term as the sum of the previous two terms and appending it to fib_series
.
The example usage section demonstrates how to call the function with n = 10
and prints the Fibonacci series up to 10 terms.