Examples
- Hello World Program in Python (How to Print Hello World in Python?)
- Python Program to Add Two Numbers (Sum/Addition Program)
- How to Generate Random Number in Python? (Program)
- Leap Year Program in Python (Check Leap Year or Not in Python)
- Python Program to Check Number is Positive, Negative, or Zero
- Even Odd Program in Python (How to Check Number is Even or Odd?)
- Python Program to Find Largest of 3 Numbers (Greatest of Three Numbers in Python)
- Prime Number Program in Python (Check Prime or Not)
- Python Program to Find Square Root of a Number (Examples)
- Python Factorial Program (How to Find Factorial of a Number in Python?)
- Python Program to Find Prime Numbers in Range
- Python Program to Find Area of Triangle (Different Ways With Examples)
- How to Solve Quadratic Equation in Python? (Program Examples)
- How to Swap Two Numbers in Python? (Program Examples to Swap Variables)
- Python Program to Convert Kilometers to Miles (km to mi) and Vice Versa
- Python Program to Convert Celsius To Fahrenheit (and Fahrenheit to Celsius)
- Program to Check Armstrong Numbers in Python (Code Examples)
- Program for Armstrong Number Between Two Intervals in Python
- Program to Print Multiplication Table in Python (Code Examples)
- Python Program for Fibonacci Series (Print Fibonacci Sequence in Python)
- Program for Sum of n Natural Numbers in Python (Code Examples)
- Program to Print Powers of 2 in Python (Code Examples)
- Python Program to Find Numbers Divisible by Another Number
- Convert Decimal to Binary, Octal and Hexadecimal in Python (Program with Example)
- How to Convert Decimal to Binary in Python? Program with Examples
- How to Convert Decimal to Octal in Python? Program with Examples
- How to Convert Decimal to Hexadecimal in Python? Program and Examples
- How to Convert Binary to Decimal in Python? Program With Examples
- How to Convert Hexadecimal to Octal in Python? Program With Examples
- Program to Find LCM of Two Numbers in Python (Calculate LCM in Python)
- GCD of Two Numbers in Python (Program to Find HCF or GCD)
- GCD of Three Numbers in Python (HCF Program for n Numbers)
- Simple Calculator Program in Python (Basic Calculator Code)
- Find Factors of a Number in Python (Programs and Examples)
- Program to Find Prime Factors of a Number in Python
- How to Find ASCII Value in Python? ASCII Value of Character
Python Program for Fibonacci Series (Print Fibonacci Sequence in Python)
Let’s learn how to write a program to print Fibonacci series in Python with examples and proper explanations. Whether you're a beginner in programming or an experienced coder, understanding and implementing the Fibonacci series program in Python is a fundamental skill for you.
The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones. This series has fascinated mathematicians, scientists, and programmers alike due to its intriguing patterns and widespread applications in various fields, such as mathematics, computer science, and even nature.
Here, we will explore different techniques to generate the Fibonacci series in Python. We will delve into various approaches, including the use of while loops, for loops, recursion, iteration, and even lambda functions.
So, get ready to expand your programming knowledge with Python program for Fibonacci series.
Fibonacci Series Program in Python
Code
# Fibonacci Series in Python Using While Loop
def fibonacci_series(n):
series = []
a, b = 0, 1
while len(series) < n:
series.append(a)
a, b = b, a + b
return series
# Test the program
number = int(input("Enter the number of terms: "))
fib_series = fibonacci_series(number)
print("Fibonacci Series:")
print(fib_series)
Output
Enter the number of terms: 6
Fibonacci Series:
[0, 1, 1, 2, 3, 5]
Explanation
-
We define a function fibonacci_series(n) that takes an integer n as input and returns a list containing the Fibonacci series of n terms.
-
We initialize an empty list series to store the Fibonacci series. We also initialize two variables a and b to 0 and 1, respectively. These variables represent the current and next terms of the series.
-
Using a while loop, we continue generating Fibonacci numbers until the length of the series list reaches the desired number of terms (n). Inside the loop, we append the current Fibonacci number (a) to the series list and update a and b by assigning b to a + b and a to b, respectively. This ensures that we generate the next number in the Fibonacci sequence.
-
Finally, we return the list containing the Fibonacci series.
-
To test the program, we prompt the user to enter the number of terms they want in the Fibonacci series. We then call the fibonacci_series() function, passing the user's input as an argument, and store the resulting Fibonacci series in the fib_series variable.
-
Finally, we print the Fibonacci series to the console.
Fibonacci Series Program in Python Using for loop
Code
# Python program for Fibonacci series
def fibonacci_series(n):
series = [0, 1]
for i in range(2, n):
series.append(series[i-1] + series[i-2])
return series
# Test the program
number = int(input("Enter the number of terms: "))
fib_series = fibonacci_series(number)
print("Fibonacci Series:")
print(fib_series)
Output
Enter the number of terms: 12
Fibonacci Series:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
Explanation
-
We define a function fibonacci_series(n) that takes an integer n as input and returns a list containing the Fibonacci series of n terms.
-
We initialize the series list with the first two Fibonacci numbers, 0 and 1.
-
Using a for loop, we iterate from 2 to n-1. Inside the loop, we append the sum of the previous two Fibonacci numbers (series[i-1] and series[i-2]) to the series list.
-
Finally, we return the series list containing the Fibonacci series.
-
To test the program, we prompt the user to enter the number of terms they want in the Fibonacci series. We then call the fibonacci_series() function, passing the user's input as an argument, and store the resulting Fibonacci series in the fib_series variable.
-
Finally, we print the Fibonacci series to the console.
nth Fibonacci Number in Python
Code
# Using recursion to print fibonacci sequence in Python
def fibonacci_recursive(n):
if n <= 1:
return n
else:
return fibonacci_recursive(n-1) + fibonacci_recursive(n-2)
# Test the program
number = int(input("Enter the value of n: "))
fib_number = fibonacci_recursive(number)
print("The", number, "th Fibonacci number is:", fib_number)
Output
Enter the value of n: 25
The 25 th Fibonacci number is: 75025
Explanation
-
We define a function fibonacci_recursive(n) that calculates the nth Fibonacci number using recursion.
-
If the input n is less than or equal to 1, we return n as it is the base case. Otherwise, we recursively call the fibonacci_recursive() function for n-1 and n-2, summing their results to find the nth Fibonacci number.
Fibonacci Series Program in Python Using Recursion
Code
# Python program for Fibonacci series using recursion
def fibonacci_series(n):
if n <= 0:
return []
elif n == 1:
return [0]
elif n == 2:
return [0, 1]
else:
series = fibonacci_series(n-1)
series.append(series[-1] + series[-2])
return series
# Test the program
number = int(input("Enter the number of terms: "))
fib_series = fibonacci_series(number)
print("Fibonacci Series:")
print(fib_series)
Output
Enter the number of terms: 15
Fibonacci Series:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]
Explanation
We define a function fibonacci_series(n) that takes an integer n as input and returns a list containing the Fibonacci series of n terms.
The function uses a recursive approach to generate the Fibonacci series. We define three base cases:
-
If n is less than or equal to 0, an empty list is returned.
-
If n is equal to 1, a list containing only the first Fibonacci number, 0, is returned.
-
If n is equal to 2, a list containing the first two Fibonacci numbers, 0 and 1, is returned.
For n greater than 2, we call the fibonacci_series() function recursively, passing n-1 as the argument. We store the result in the series variable.
To generate the next Fibonacci number, we append the sum of the last two numbers in the series list (series[-1] and series[-2]) to the series list.
Finally, we return the series list containing the Fibonacci series
Fibonacci Series in Python Using Iteration
Code
# Program for Fibonacci sequence in Python
def fibonacci_series(n):
series = [0, 1]
if n <= 2:
return series[:n]
for i in range(2, n):
series.append(series[i-1] + series[i-2])
return series
# Test the program
number = int(input("Enter the number of terms: "))
fib_series = fibonacci_series(number)
print("Fibonacci Series:")
print(fib_series)
Output
Enter the number of terms: 8
Fibonacci Series:
[0, 1, 1, 2, 3, 5, 8, 13]
Explanation
-
We define a function fibonacci_series(n) that takes an integer n as input and returns a list containing the Fibonacci series of n terms.
-
We initialize the series list with the first two Fibonacci numbers, 0 and 1.
-
If the requested number of terms (n) is less than or equal to 2, we return a slice of the series list that contains the appropriate number of terms. This avoids unnecessary iterations in such cases.
-
For n greater than 2, we use a for loop that iterates from 2 to n-1. Inside the loop, we calculate the next Fibonacci number by summing the previous two numbers (series[i-1] and series[i-2]) and append it to the series list.
-
Finally, we return the series list containing the Fibonacci series.
Practice More Python Programs:
- Hello World Program in Python
- Python Program to Add Two Numbers
- Generate Random Number in Python
- Leap Year Program in Python
- Even Odd Program in Python
- Python Program to Find Largest of 3 Numbers
- Prime Number Program in Python
- Python Program to Find Square Root of a Number
- Python Factorial Program
- Python Program to Find Area of Triangle
- Solve Quadratic Equation in Python
- Swap Two Numbers in Python
- Python Program to Convert Kilometers to Miles
- Python Program to Convert Celsius To Fahrenheit
- Check Armstrong Numbers in Python
- Print Multiplication Table in Python
- Convert Decimal to Binary, Octal and Hexadecimal in Python
- Find LCM of Two Numbers in Python
- GCD of Two Numbers in Python
- Simple Calculator Program in Python
- Python Matrix Addition Program
- Transpose of Matrix in Python
- Python Matrix Multiplication
- Python Calendar Program