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.
# Import the system library. This allows us to access stdin later.
# 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.
print "\nHow many numbers of the sequence would you like?"
n = int(sys.stdin.readline())
# 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.
a,b = 0,1
for i in range(0,n):
a,b, = b,a+b