Certainly! You can create a Python program to generate Fibonacci numbers using various methods. Here’s a simple example using a loop:
def fibonacci(n):
fib_sequence = [0, 1]
while len(fib_sequence) < n:
next_number = fib_sequence[-1] + fib_sequence[-2]
fib_sequence.append(next_number)
return fib_sequence
# Change the value of 'n' to generate a different number of Fibonacci numbers
n = 10
result = fibonacci(n)
print(result)
In this code:
- We define a function
fibonacci(n)
that takes an integern
as an input and generates the firstn
Fibonacci numbers. - We initialize a list
fib_sequence
with the first two Fibonacci numbers, 0 and 1. - We use a
while
loop to calculate the next Fibonacci numbers until the length offib_sequence
reachesn
. - The next Fibonacci number is calculated by adding the last two numbers in
fib_sequence
. - The generated Fibonacci numbers are stored in the list
fib_sequence
, and the list is returned as the result. - You can change the value of
n
to generate a different number of Fibonacci numbers.
When you run this code with n = 10
, it will print the first 10 Fibonacci numbers:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
You can adjust the value of n
as needed to generate more or fewer Fibonacci numbers.