Here is a simple Python program that can be used to print the Fibonacci sequence:
#define the fibonacci function def fib(n): #define the base case if n == 0: return 0 elif n == 1: return 1 #define the recursive case else: return fib(n-1) + fib(n-2) #print the first 10 numbers in the fibonacci sequence for i in range(10): print(fib(i))
This program uses a recursive function,
fib(), to calculate each number in the Fibonacci sequence. The function uses two base cases: if
n is 0, it returns 0, and if
n is 1, it returns 1. For all other values of
n, the function returns the sum of the previous two numbers in the sequence (i.e.,
fib(n-1) + fib(n-2)).
for loop at the end of the program uses this function to print the first 10 numbers in the Fibonacci sequence. To print a different number of numbers in the sequence, simply change the value of the
range() function in the
for loop. For example, to print the first 20 numbers in the sequence, you could use
for i in range(20).
Thanks for reading. Happy coding!