Table of Contents
Introduction:
In mathematics, the Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. The sequence goes like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on. In this tutorial, we will learn how to print the Fibonacci series in Python using a while loop.
What is a Fibonacci Series?
A Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones. The series starts with 0 and 1, and the next number in the sequence is the sum of the previous two numbers. The sequence goes like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on. This sequence is named after the Italian mathematician Leonardo Fibonacci, who introduced it to the western world.
Printing Fibonacci Series Using a While Loop
a = 0
b = 1
while b <= 55:
print(b)
a, b = b, a + b
print(“Done”)
Explanation of the Code:
The program first initializes the variables a
and b
to 0 and 1, respectively. The while
loop is then used to iterate through the sequence until the value of b
reaches 55 or greater.
Inside the loop, the program prints the current value of b
which is the current term in the Fibonacci sequence. It then updates the values of a
and b
such that a
is assigned the current value of b
, and b
is assigned the sum of the previous values of a
and b
.
Once b
reaches 55 or greater, the program exits the loop and prints the message “Done”.
OUTPUT
1
1
2
3
5
8
13
21
34
55
Done
Conclusion
In this tutorial, we learned how to print the Fibonacci series in Python using a while loop. We started by explaining what a Fibonacci series is and then showed you how to print it using a while loop in Python. We hope this tutorial was helpful to you!