Scroll to Top

Virtual Math Learning Center

Virtual Math Learning Center Texas A&M University Virtual Math Learning Center

For Loops and Recursive Sequences in Python

Author: David Manuel

In this video, we use for loops in Python to generate a recursively defined sequence. In particular, we will generate the first fifty Fibonacci numbers.

Transcript 27

Problem: Generate and print the first 50 Fibonacci numbers, a recursive sequence defined by
\(a_1=1,\) \(a_2=2,\) and \(a_n=a_{n-1}+a_{n-2}.\)

Python Code: 
Generate and print the first 50 Fibonacci numbers

a=[1,1]
for n in range(2,50): 
    a.append(a[n-2]+a[n-1])
print('The first 50 Fibonacci numbers are',a)

Open Python Notebook File

See more videos from this section