This content was proof-read by Grammarly. You can Try Grammary for FREE
In this Python program, you will learn how to generate and print Fibonacci series using loops and conditional statements.
What is a Fibonacci Sequence ?
A Fibonacci sequence is an sequence of integers where the value of the sequence is determined by adding the preceding two values. The first two values in the sequence is 0 and 1. Then comes Next one with (N-1) + (N-2).
Are you a .NET Developer planning to integrate GraphQL for .NET in your Application? Check out this book
Example of a Fibonacci series is
How to Print Fibonacci Series in Python?
# Author : Abundantcode.com # Program Program to print Fibonacci series No = 10 num1, num2 = 0, 1 count = 0 if No <= 0: print("Invalid Number") elif No == 1: print("Fibonacci sequence for limit of ",No,":") print(num1) else: print("Fibonacci sequence:") while count < No: print(num1) nth = num1 + num2 num1 = num2 num2 = nth count += 1
In the above program, we store the number of Fibonacci sequence to be generated in the variable No and the first two terms are initialized with 0 and 1 in the variables num1 and num2 respectively.
When the number of sequence to be generated is more than 2 , a while loop is used to find the next term in the sequence by adding the previous two sequence variables.