Python Code Example - Fibonacci Series in Python

7 comments

Python Code Example - Fibonacci Series in Python


Today we'll show how to get the Fibonacci number using Python Programming language. Fibonacci Series is on e of the most common data structure in Computer Science. We;ll just do it using Python programming language.


Code Part


n_string = input('How many fibonacci number do want to get = ')
n = int(n_string)

fib0 = 0
fib1 = 1
fib = 0
i = 0

while i < n:
    fib = fib0+fib1
    print(fib)
    fib0 = fib1
    fib1 = fib
    i = i+1


Logic behind the Fibonacci Series

Two fixed Fibonacci numbers are = 0 1

And next will be generated from this.

0
1
0 + 1 = 1
1 + 1 = 2
2 + 1 = 3
3 + 2 = 5
5 + 3 = 8
....

That means the Fibonacci number will be first_number + second number.
Then, the first number will be second number and the second number will be Fibonacci number.

Code Explanation Line By Line

n_string = input('How many gibonacci number do want to get = ')
n = int(n_string)
In first two line, take input from user by input() method. Which returns a string. Then in second line just convert it to integer using int() 


Now Define the first two fibonacci number and variables

fib0 = 0
fib1 = 1
fib = 0
i = 0

Now start loop of while until total number of taken from user reaches. and then just do what I've said in logic part.

That means the Fibonacci number will be first_number + second number.
Then, the first number will be second number and the second number will be Fibonacci number.


while i < n:
    fib = fib0+fib1
    print(fib)
    fib0 = fib1
    fib1 = fib
    i = i+1

Full Code + Output(pic)



Having any problem, just comment out here

7 comments :

  1. A Computer Science portal for geeks. It contains well written, well thought and well
    explained computer science and programming articles, quizzes and practice/competitive
    programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  2. A Computer Science portal for geeks. It contains well written, well thought and well
    explained computer science and programming articles, quizzes and practice/competitive
    programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  3. A Computer Science portal for geeks. It contains well written, well thought and well
    explained computer science and programming articles, quizzes and practice/competitive
    programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  4. A Computer Science portal for geeks. It contains well written, well thought and well
    explained computer science and programming articles, quizzes and practice/competitive
    programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  5. A Computer Science portal for geeks. It contains well written, well thought and well
    explained computer science and programming articles, quizzes and practice/competitive
    programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  6. A Computer Science portal for geeks. It contains well written, well thought and well
    explained computer science and programming articles, quizzes and practice/competitive
    programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  7. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. I was exactly searching for. Thanks for such post and please keep it up. Great work. single customer view,

    ReplyDelete