Write a python function that calculates Fibonacci sequence of length n and returns it as…
- User
Write a python function that calculates Fibonacci sequence of length n and returns it as a list.
Assistant
Sure, here's a Python function that calculates the Fibonacci sequence of length
nand returns it as a list:def fibonacci_sequence(n): # Initialize the first two numbers in the sequence a, b = 0, 1 # Initialize the list to store the sequence sequence = [a] # Calculate the rest of the sequence up to the length n for i in range(1, n): c = a + b a = b b = c sequence.append(c) # Return the calculated sequence as a list return sequenceHere's how you can use this function:
# Calculate the Fibonacci sequence of length 10 sequence = fibonacci_sequence(10) # Print the calculated sequence print(sequence)This will output:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]You can adjust the value of
nto calculate the Fibonacci sequence of a different length.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.