Python code to generate Fibonacci Sequence

The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones. It starts with 0, 1 and then follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, … and so on. The next number in the sequence is found by adding the two numbers before it. The sequence is named after Leonardo Fibonacci, an Italian mathematician who introduced the sequence to the Western world through his book, “Liber Abaci”, in 1202. The Fibonacci sequence has several interesting properties and appears in many different areas of mathematics, science, and nature, such as in the growth patterns of plants and animals.

Following code generates Fibonacci Sequence:

def fibonacci(n):
    if n <= 0:
        return []
    elif n == 1:
        return [0]
    elif n == 2:
        return [0, 1]
    else:
        fib = [0, 1]
        for i in range(2, n):
            fib.append(fib[i-1] + fib[i-2])
        return fib

n = 10
print(fibonacci(n))

This code defines a function fibonacci that generates a list of the first n Fibonacci numbers. The function takes an integer n as an argument and returns an empty list if n is less than or equal to 0, a list with a single value of 0 if n is 1, and a list with two values, 0 and 1, if n is 2. If n is greater than 2, the function generates a list fib with the first two values 0 and 1, and then uses a for loop to iterate from 2 to n, adding the sum of the previous two values in the list to fib in each iteration. The final result is the list fib.

In this example, n is set to 10 and the result of calling fibonacci(n) is printed to the console. The output should be the first 10 numbers in the Fibonacci sequence: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34].

Unit test for this code:

import unittest

class TestFibonacci(unittest.TestCase):
    def test_fibonacci(self):
        # Test n = 0
        result = fibonacci(0)
        self.assertEqual(result, [])

        # Test n = 1
        result = fibonacci(1)
        self.assertEqual(result, [0])

        # Test n = 2
        result = fibonacci(2)
        self.assertEqual(result, [0, 1])

        # Test n = 10
        result = fibonacci(10)
        self.assertEqual(result, [0, 1, 1, 2, 3, 5, 8, 13, 21, 34])

def fibonacci(n):
    if n <= 0:
        return []
    elif n == 1:
        return [0]
    elif n == 2:
        return [0, 1]
    else:
        fib = [0, 1]
        for i in range(2, n):
            fib.append(fib[i-1] + fib[i-2])
        return fib

if __name__ == '__main__':
    unittest.main()

This code defines a test class TestFibonacci that inherits from unittest.TestCase. This class contains a single test method, test_fibonacci, which verifies that the fibonacci function generates the correct sequence of numbers for n equal to 0, 1, 2, and 10. The tests are run by calling unittest.main() at the end of the code. If all tests pass, you will see an output like this:

Ran 4 tests in 0.001s

OK

Following code creates a GUI for our program

import tkinter as tk

def fibonacci(n):
    if n <= 0:
        return []
    elif n == 1:
        return [0]
    elif n == 2:
        return [0, 1]
    else:
        fib = [0, 1]
        for i in range(2, n):
            fib.append(fib[i-1] + fib[i-2])
        return fib

def generate_sequence():
    n = int(entry.get())
    result = fibonacci(n)
    output_text.delete('1.0', tk.END)
    output_text.insert(tk.END, str(result))

root = tk.Tk()
root.title("Fibonacci Sequence Generator")

label = tk.Label(root, text="Enter the number of terms:")
entry = tk.Entry(root)
generate_button = tk.Button(root, text="Generate", command=generate_sequence)
output_text = tk.Text(root, height=10, width=30)

label.pack()
entry.pack()
generate_button.pack()
output_text.pack()

root.mainloop()

This code imports the tkinter library and defines a function fibonacci to generate the Fibonacci sequence. It also defines a function generate_sequence that is triggered by clicking the “Generate” button. This function retrieves the value entered into the text box, generates the Fibonacci sequence, and displays the result in a text widget.

The GUI is created by creating an instance of the tk.Tk class and setting the title of the window. Labels, entry fields, buttons, and text widgets are created and packed into the window using the pack method. Finally, the mainloop method is called to start the GUI event loop.

When you run this code, a window should appear that allows you to enter the number of terms in the Fibonacci sequence, and a button to generate the sequence. The result will be displayed in a text widget.