top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

Fibonacci Sequence Generator using Python

Writer's picture: Aldwin Dave ConahapAldwin Dave Conahap

This blog post demonstrates how to generate a Fibonacci sequence in Python. Fibonacci sequence is a series of numbers wherein the numbers are the sum of its two preceding numbers, i.e.,

It interests mathematicians as it appears in nature, e.g., in flower petals, galaxies, hurricanes, etc. It is named through the claimed discovery of famous Italian mathematician, Leonardo Fibonacci.


Now, let's generate a Fibonacci sequence using Python!


The following line of codes lets the user input a range. If the inputted range is not a positive integer, it lets the user try to input again.

try:
    Range = int(input("Enter a range: 1 to "))
    
except ValueError:
    print("\nInvalid input! Please input a positive integer only.")
    Range = int(input("Enter a range: 1 to "))

Next, the program defines a function that performs the operations to generate the sequence. It defines variables 'previous_number' and 'current_number', and initiates its values to 0 and 1, respectively. After that, it creates a list where the succeeding numbers to append appears. Then, it runs in a loop up until the current value does not exceed the range inputted by the user. Followingly, it adds up the previous number and the current number to produce a new number to append in the sequence. It also creates a condition that determines whether the current number is still in range. Then, it returns the list of the Fibonacci sequence generated.

def fibonacci(Range):                                    
    previous_number, current_number = 0, 1               
    
    fibonacci=[1,]
    
    while current_number < Range:
        
        previous_number += current_number
        current_number += previous_number
        
        fibonacci.append(previous_number)
        
        if current_number < Range:
            fibonacci.append(current_number)
        else:
            break
        
    return fibonacci

Lastly, the program calls the created function that executes to generate the sequence, and display the result.

print("Sequence = " + str(fibonacci(Range)))

That is all! We have generated a Fibonacci sequence using Python.




1 comment

Recent Posts

See All

1 Comment


Data Insight
Data Insight
Sep 19, 2021

Nice! But you didn't test your code. At least generate the first 10 members of the sequence to confirm that it's working as expected.

Like

COURSES, PROGRAMS & CERTIFICATIONS

 

Advanced Business Analytics Specialization

Applied Data Science with Python (University of Michigan)

Data Analyst Professional Certificate (IBM)

Data Science Professional Certificate (IBM)

Data Science Specialization (John Hopkins University)

Data Science with Python Certification Training 

Data Scientist Career Path

Data Scientist Nano Degree Program

Data Scientist Program

Deep Learning Specialization

Machine Learning Course (Andrew Ng @ Stanford)

Machine Learning, Data Science and Deep Learning

Machine Learning Specialization (University of Washington)

Master Python for Data Science

Mathematics for Machine Learning (Imperial College London)

Programming with Python

Python for Everybody Specialization (University of Michigan)

Python Machine Learning Certification Training

Reinforcement Learning Specialization (University of Alberta)

Join our mailing list

Data Insight participates in affiliate programs and may sometimes get a commission through purchases made through our links without any additional cost to our visitors.

bottom of page