Fibonacci in Python

In our latest installment of the Fibonacci project, we’ll write our simple program in Python. For those of you who are unfamiliar with what we are doing here, please read the first post here. Python is a newer, but increasingly popular scripting language. I suppose the most interesting difference here is that Python is dependent on indentation to define blocks, rather than braces or other language constructs.

#!/usr/bin/python

# Import the system library. This allows us to access stdin later.
import sys

# Here’s our main function. Python is pretty efficient here. You
# should notice that there are no braces. Python is dependant on
# whitespace to define blocks.

def main():
  print "\nHow many numbers of the sequence would you like?"
  n = int(sys.stdin.readline())
  fibonacci(n)

# Here’s the fibonacci function. Like in Perl, you can assign multiple
# variables on a line without using a temporary variable. Also, the for 
# loop here works more like a foreach loop by setting a range from 0 to n.

def fibonacci(n):
  a,b = 0,1
  for i in range(0,n):
    print a
    a,b, = b,a+b

main()

HTML code generated by vim-color-improved v.0.3.2.

2 Responses to “Fibonacci in Python”

  1. Vivid Vortex Says:

    Hi, neat idea, sadly, it’s been done: http://en.literateprograms.org/Category:Fibonacci_numbers

  2. Zachary Fox Says:

    The link you posted is currently unavailable, but I believe you. There are also some examples here:

    http://www.scriptol.org/fibonacci-any-programming-language.html

    But they aren’t all fully functioning programs, and they use different algorithms, and there isn’t any explanation about the differences. I thought I might be able to explain some of the differences and similarities that I noticed while doing the same thing in multiple languages.

    I chose Fibonacci because it gives a really simple function that demonstrates loops, simple arithmetic, and then created a program that allows us to do the same thing in multiple languages. In addition, I wanted to create two functions, to show you how to call a function with and without a parameter. Things that we might take for granted in languages that we are familiar with, but may be very different syntactically in others.

    Anyway, this is as much for me as it is for anyone reading, and it’s a good example of a challenge you can take on to expand your knowledge. Perhaps it will give someone a different perspective on the great language debate. I’m not picking sides, just trying to learn. For the most part, the hardest problems to solve in programming are algorithmic. Once you can tackle those, languages are mostly about syntax. Obviously, there are some serious differences in methodology between functional, procedural programming and object oriented, but that probably is beyond the scope of this comment.

    I do plan to do some analytical posts comparing the programs I’ve wrote here, in addition to comparing different algorithms for generating Fibonacci sequence numbers.

Leave a Reply