Search⌘ K
AI Features

Solution: Generate the First 20 Fibonacci Numbers

Understand how to generate the first 20 Fibonacci numbers efficiently by using Python list comprehensions. This lesson guides you through initializing the sequence, applying iterative logic, and outputting the complete set of numbers, helping you grasp fundamental programming constructs in Python.

We'll cover the following...

The solution to the problem of generating the first 20 Fibonacci numbers is given below.

Solution

Python 3.8
lst = [0, 1]
[lst.append(lst[k - 1] + lst[k - 2]) for k in range(2, 20)]
print('First 20 Fibonacci numbers:', lst)
...