Project Description
The Fibonacci sequence is as follows.
0 1 1 2 3 5 8 13 21 34 ...
(continues)
It is generated as follows.
The next number is generated by adding the two last numbers.
0 + 1 = 1
1 + 1 = 2
1 + 2 = 3
2 + 3 = 5
3 + 5 = 8
8 + 13 = 21
13 + 21 = 34
21 + 34 = 55
...
Write a program that prints the Fibonacci sequence.
You will do it in two ways in this project.
Step 1 The direct way
How to solve this?
Well, the first thought is to make it in an iterative way.
def fib(n):
if n <= 1:
return n
i = 0
j = 1
for _ in range(n):
i, j = j, i + j
return i
You try to follow how the sequence is calculated and this can be done, but seems easy to get lost in the code.
To generate the sequence you can run this code.
for i in range(10):
print(fib(i))
Step 2 The simple way (recursive)
The first time you see recursion, it might not be easy to understand.
But after inspecting the following code, you might find it easier to write and follow.
def fib(n):
if n <= 1:
return n
return fib(n - 1) + fib(n - 2)
This actually generates the same sequence.
The code is actually writing the exact formula for generating it.
You can get the sequence as follows.
for i in range(10):
print(fib(i))
Learn Python

Learn Python A BEGINNERS GUIDE TO PYTHON
- 70 pages to get you started on your journey to master Python.
- How to install your setup with Anaconda.
- Written description and introduction to all concepts.
- Jupyter Notebooks prepared for 17 projects.
Python 101: A CRASH COURSE
- How to get started with this 8 hours Python 101: A CRASH COURSE.
- Best practices for learning Python.
- How to download the material to follow along and create projects.
- A chapter for each lesson with a description, code snippets for easy reference, and links to a lesson video.
Expert Data Science Blueprint

Expert Data Science Blueprint
- Master the Data Science Workflow for actionable data insights.
- How to download the material to follow along and create projects.
- A chapter to each lesson with a Description, Learning Objective, and link to the lesson video.
Machine Learning

Machine Learning – The Simple Path to Mastery
- How to get started with Machine Learning.
- How to download the material to follow along and make the projects.
- One chapter for each lesson with a Description, Learning Objectives, and link to the lesson video.