Python Programming Examples Tutorial Index

Python Number Programs

The Fibonacci series is a sequence of numbers in which each is the sum of the two preceding ones, usually starting with 0 and 1. The series is named after the Italian mathematician Leonardo Fibonacci, who introduced it to the Western World in his 1202 book, "Liber Abaci."



Here is the mathematical definition of the Fibonacci series:

F(0) = 0 F(1) = 1 F(n) = F(n-1) + F(n-2) (for n > 1)

In other words, each number in the series is the sum of the previous two numbers. For example, the third number in the series is the sum of the first and second numbers; the fourth is the sum of the second and third numbers, and so on.

Here is a simple example of how to generate the Fibonacci series in Python:

Example:

def fibonacci(n):
  if n == 0:
    return 0
  elif n == 1:
    return 1
  else:
    return fibonacci(n-1) + fibonacci(n-2)

# Generate the first 10 numbers in the Fibonacci series
for i in range(10):
  print(fibonacci(i))

Program Output:

The code above will print out the first ten numbers in the Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.

Loops can also be used to generate the Fibonacci series. Here's an example of how to do this:

Example:

def fibonacci(n):
  a, b = 0, 1
  for i in range(n):
    a, b = b, a+b
  return a

# Generate the first ten numbers in the Fibonacci series
for i in range(10):
  print(fibonacci(i))

Program Output:

The code above will print the first ten numbers in the Fibonacci series using a loop.

The Fibonacci series can be stored in a list and then printed out. Here's an example of how to do this:

Example:

def fibonacci(n):
  a, b = 0, 1
  result = []
  for i in range(n):
    result.append(a)
    a, b = b, a+b
  return result

# Generate the first ten numbers in the Fibonacci series
print(fibonacci(10))

The code above will print out the first ten numbers in the Fibonacci series as a list: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34].

Python Program That Demonstrates the Use of the Fibonacci Series

# define a function to generate the Fibonacci series
def fibonacci(n):
  # base case: the first two numbers in the series are 1
  if n == 1 or n == 2:
    return 1
  # recursive case: the next number is the sum of the previous two
  return fibonacci(n-1) + fibonacci(n-2)

# prompt the user for the number of terms to generate
num_terms = int(input("Enter the number of terms to generate: "))

# generate and print the Fibonacci series
for i in range(1, num_terms+1):
  print(fibonacci(i), end=" ")

Program Output:

D:\Python-Example>python test.py
Enter the number of terms to generate: 6
1 1 2 3 5 8

This program defines a function fibonacci() that takes a single argument n and returns the nth term in the Fibonacci series. The function uses a recursive approach, where each term is the sum of the previous two terms.

The program then prompts the user for the number of terms to generate and uses a for loop to call the fibonacci() function and print the results.

For example, if the user enters 6, the program will output the first 6 terms of the Fibonacci series: 1 1 2 3 5 8.

Conclusion

In conclusion, the Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones. It is a well-known series with many applications in mathematics, computer science, and other fields. There are several ways to generate the Fibonacci series in Python, including using recursion, a loop, or a list.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram