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.

7 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.

  3. Manibharathi Says:

    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

  4. Alexandre Says:

    This is the better and clean
    Fibonacci code i ever saw…

    Thanks

  5. Sr. Regina Says:

    nice code that i have ever seen for fib series…
    thanks

  6. Nir Soffer Says:

    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()

  7. tc Says:

    the code is beautiful, the multi assigment is a great use in fibonacci. Thx.