# prompt: python program to generate fibonacci series

 # prompt: python program to generate fibonacci series


def fibonacci_sequence(n):

  """

  Generates a Fibonacci sequence up to n terms.


  Args:

    n: The number of terms to generate.


  Returns:

    A list containing the Fibonacci sequence.

  """

  if n <= 0:

    return []

  elif n == 1:

    return [0]

  else:

    list_fib = [0, 1]

    while len(list_fib) < n:

      next_fib = list_fib[-1] + list_fib[-2]

      list_fib.append(next_fib)

    return list_fib


# Get input from the user

num_terms = int(input("Enter the number of terms for the Fibonacci sequence: "))


# Generate and print the Fibonacci sequence

fib_sequence = fibonacci_sequence(num_terms)

print("Fibonacci Sequence:", fib_sequence)


Understanding the Fibonacci Sequence

The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones. It starts with:

0,1,1,2,3,5,8,13,21,34,0, 1, 1, 2, 3, 5, 8, 13, 21, 34, \dots

Mathematically, it follows:

F(n)=F(n1)+F(n2)F(n) = F(n-1) + F(n-2)

where:

  • F(0)=0F(0) = 0
  • F(1)=1F(1) = 1

Code Breakdown

1. Function Definition

python
def fibonacci_sequence(n):
  • Defines a function named fibonacci_sequence that takes an integer n (the number of terms to generate).

2. Docstring (Explanation for Function)

python
""" Generates a Fibonacci sequence up to n terms. Args: n: The number of terms to generate. Returns: A list containing the Fibonacci sequence. """
  • This provides information about the function’s purpose, input (n), and output (a list of Fibonacci numbers).

3. Handling Edge Cases

python
if n <= 0: return []
  • If n is 0 or negative, it returns an empty list since a sequence can't have negative or zero terms.
python
elif n == 1: return [0]
  • If n == 1, it returns [0] because the first Fibonacci number is 0.

4. Generating the Fibonacci Sequence

python
else: list_fib = [0, 1]
  • If n is greater than 1, it initializes the list with the first two Fibonacci numbers: [0, 1].
python
while len(list_fib) < n: next_fib = list_fib[-1] + list_fib[-2] list_fib.append(next_fib)
  • A while loop runs until the list contains n terms.
  • list_fib[-1] (last element) and list_fib[-2] (second last element) are added to compute the next Fibonacci number.
  • The new number is appended to list_fib.
python
return list_fib
  • Once the sequence reaches n terms, the function returns the list.

5. Getting User Input

python
num_terms = int(input("Enter the number of terms for the Fibonacci sequence: "))
  • Prompts the user to enter a number.
  • Converts the input to an integer (int).

6. Calling the Function & Printing the Result

python
fib_sequence = fibonacci_sequence(num_terms) print("Fibonacci Sequence:", fib_sequence)
  • Calls the fibonacci_sequence() function with the user-provided number of terms.
  • Prints the generated Fibonacci sequence.

Comments

Popular posts from this blog

Ecommerce website

Yes, Python is an object-oriented programming (OOP) language, but it is also a multi-paradigm language

Your task is to find the missing number.