filmov
tv
Write a Python Program to Print Fibonacci Series

Показать описание
Welcome to this tutorial on printing the Fibonacci series in Python.
The 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.
Initialize the first two terms of the series.
In this line of code, we initialize n1 to 0 and n2 to 1. These two values represent the first two terms of the Fibonacci series.
Set the number of terms to be printed.
In this line of code, we set the variable num_terms to 10. This variable represents the number of terms of the Fibonacci series we want to print.
Check if the number of terms is valid.
In these lines of code, we check if the num_terms variable is a positive integer. If num_terms is less than or equal to zero, we print a message asking the user to enter a positive integer. If num_terms is equal to 1, we print the first term of the series.
Print the Fibonacci series.
In this block of code, we print the Fibonacci series using a while loop. We start by printing the value of n1. We then calculate the next term of the series as the sum of n1 and n2 and store it in the variable nth. We update the values of n1 and n2 for the next iteration of the loop by setting n1 to the value of n2 and n2 to the value of nth. We continue the loop until we have printed the desired number of terms.
Let's run the program.
And that's it! You've successfully printed the Fibonacci series in Python.
Thank you for watching this tutorial, and happy coding!
The 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.
Initialize the first two terms of the series.
In this line of code, we initialize n1 to 0 and n2 to 1. These two values represent the first two terms of the Fibonacci series.
Set the number of terms to be printed.
In this line of code, we set the variable num_terms to 10. This variable represents the number of terms of the Fibonacci series we want to print.
Check if the number of terms is valid.
In these lines of code, we check if the num_terms variable is a positive integer. If num_terms is less than or equal to zero, we print a message asking the user to enter a positive integer. If num_terms is equal to 1, we print the first term of the series.
Print the Fibonacci series.
In this block of code, we print the Fibonacci series using a while loop. We start by printing the value of n1. We then calculate the next term of the series as the sum of n1 and n2 and store it in the variable nth. We update the values of n1 and n2 for the next iteration of the loop by setting n1 to the value of n2 and n2 to the value of nth. We continue the loop until we have printed the desired number of terms.
Let's run the program.
And that's it! You've successfully printed the Fibonacci series in Python.
Thank you for watching this tutorial, and happy coding!