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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#!/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() |
January 6th, 2008 at 1:09 am
Hi, neat idea, sadly, it’s been done: http://en.literateprograms.org/Category:Fibonacci_numbers
January 6th, 2008 at 11:28 am
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.
April 5th, 2011 at 11:18 am
thanks for the code!!
the one i tried with while loop shows me the following error:
‘int’ object cannot be iterated
this one works PerfecT
August 22nd, 2011 at 7:41 am
This is the better and clean
Fibonacci code i ever saw…
Thanks
November 20th, 2011 at 1:26 am
nice code that i have ever seen for fib series…
thanks
September 28th, 2012 at 3:26 pm
Few notes:
1. To get input from the user, you don’t need to use sys.stdin. You can use the builtin function raw_input(). Using this function I can get the user answer on the same line, separating the output from the input.
2. Since sys is not used, I removed the import
3. When iterating using range, you create a list of values. This is unneeded since you only need a counter. Using xrange you get a counter without creating all the values. This is not relevant in Python 3.
4. Your spacing is hard to read – use space after comma.
5. You had an extra comma in the last line
6. It is better to call main() only if the script is running, but not when you import the script. For example, for testing your fibonacci function, you would like to import this module inside a test script. You will not be able to do this if main() run each time you import this module. The __name__ == ‘__main__’ idiom is the way solve this.
Here is a corrected version:
#!/usr/bin/python
def main():
n = int(raw_input(“\nHow many numbers of the sequence would you like? “))
fibonacci(n)
def fibonacci(n):
a, b = 0, 1
for i in xrange(n):
print a
a, b = b, a + b
if __name__ == ‘__main__’:
main()
October 5th, 2012 at 9:36 pm
the code is beautiful, the multi assigment is a great use in fibonacci. Thx.