Python Program to Generate Fibonacci Series

This python program generates Fibonacci terms up to given maximum number. Fibonacci series is: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ...

Fibonacci series or sequence starts with two numbers 0 and 1, and next number is obtained by adding two numbers before it. For example, third term 1 is found by adding 0 and 1, fourth term 2 is then obtained by third term 1 and second term 1.

Try: Generate Fibonacci Series Online

Python Source Code: Fibonacci Series


# Python program to generate Fibonacci Series

# Defining function to generate Fibonacci series
def generate_fibonacci(n):
    a, b = 0, 1
    while a < n:
        # Print number
        print(a, end=', ')

        # Calculate next term
        next_num = a + b

        # Set a = b & b = next_num
        a, b = b, next_num

# Input
max_term = int(input('Enter maximum term of Fibonacci series: '))

# Function Call
generate_fibonacci(max_term)

Fibonacci Series Python Output

Enter maximum term of Fibonacci series: 5000
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 
233, 377, 610, 987, 1597, 2584, 4181,