Create Fibonacci Series in Python using Decorator

thumbnail
In this tutorial you will learn to create fibonacci series in python. The source code is show below.
def decorator_fibonacci(fibo):
  def wrapper_fibo(terms):
    if terms < 0:
      return []
    else:
      return fibo(terms)
  return wrapper_fibo

@decorator_fibonacci
def fibonacci_series(terms):
  flist = []
  term1, term2 = 0, 1
  for i in range(terms):
    flist.append(term1)
    term1, term2 = term2, term1 + term2
  
  return flist

print(fibonacci_series(1))
print(fibonacci_series(2))
print(fibonacci_series(5))
print(fibonacci_series(10))
print(fibonacci_series(0))
[0]
[0, 1]
[0, 1, 1, 2, 3]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
[]
Back To Top