The Task - create a program that prints the fibonacci numbers

Fibonacci numbers are a set of numbers where each sequential number is the sum of the previous two numbers... i.e. 1, 1, 2, 3, 5, 8, 13, etc.

I explore three different ways to code a solution.
The first solution shown below takes an input x, and generates all fibonacci numbers between 1 and x.

First solution - switching the values around

In [1]:
"""My first solution was to use two variables: i and j
    in order to shuffle values around, and update a third value: result
    which is the actual fibonacci number printed with each iteration"""
def fibo(x):
    a, b, result = 0, 1, 1
# where result is the finbonacci number
# b is used to store the current finbonacci iteration while the next is calculated
# a stores the value required to calculate the next fibonacci number i..e the previous fibo number
    while result <= x:
        print result,
# This part temporarily stores the current fibonacci number,
# calculates the next one by adding i, and finally
# updates i so it can be used to calculate the next number if another iteration is made
        b = result
        result = result + a
        a = b
In [2]:
fibo(1000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987

Streamlined version:

In [3]:
"""Here I have removed the result variable, moved i and j into
    the function definition as default values, and consolidated
    the juggling of figures onto one line"""
def fibo(x, a = 0, b = 1):
    while b <= x:
        print b,
        a, b = b, a + b
In [4]:
fibo(1000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987

What if we wanted to produce a set number of fibonacci numbers, as opposed to all the fibo numbers up to a particular value?

Second solution - set number of iterations:

In [5]:
def fibo(x):
# first we define a list to hold the fibonacci numbers
    nums = [0,1]
# then we append the next fibonacci number to the list for the defined number of iterations x
    for i in range(1, x):
# by summing the two previous numbers, which are referenced by their index
        nums.append(nums[-1] + nums[-2])
# finally we print a string of the numbers from the list avoiding the starting 0 by using an index range
    print " ".join(str(i) for i in nums[1:])
In [6]:
fibo(20)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765

Third solution - recursive:

In [7]:
"""This will return the nth fibonacci number"""
def fibo(n):
# these two values of n act to end the recursion of the function
# and stand as the first two fibonacci numbers as they are both 1
    if n == 1 or n == 2:
        return 1
# this line recursively calls the function in a factorial manner, summing the two fibonacci numbers before it
    return fibo(n - 1) + fibo(n - 2)
In [8]:
fibo(20)
Out[8]:
6765

I would like to use the above recursive function to print x iterations, so I will define a separate function to do so:

In [9]:
def fiboPrint(x):
    for i in range(1, x + 1):
        print fibo(i),
In [10]:
fiboPrint(20)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765

And finally...

I wish to make the output slightly more pratical for the user.
To clarify what the nth value is, I have slightly altered the FiboPrint function:

In [11]:
def fiboPrint(x):
    for i in range(1, x + 1):
        print "Iteration " + str(i) + " is: " + str(fibo(i))
In [12]:
fiboPrint(10)
Iteration 1 is: 1
Iteration 2 is: 1
Iteration 3 is: 2
Iteration 4 is: 3
Iteration 5 is: 5
Iteration 6 is: 8
Iteration 7 is: 13
Iteration 8 is: 21
Iteration 9 is: 34
Iteration 10 is: 55