Reputation: 151
I'm trying to read through the following code, but I'm having a bit of a tough time:
def fib2(n): # return Fibonacci series up to n
"Return a list containing the Fibonacci series up to n"
result = []
a, b = 0, 1
while b < n:
result.append(b) # see below
a, b = b, a+b
return result
f100 = fib2(100) # call it
f100 # write the result
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
We start with:
def fib2(n)
As I understand it, we are simply creating the definition, where n will be the parameter to specify. Then we move onto:
result = []
This to me seems like we are going to be posting the output into an empty list. From there we move onto:
a, b = 0, 1
I'm having a bit of difficult understanding why we specify "a = 0" and "b = 1"? Is it simply to specify an initial value, whose value doesn't really matter? For instance, could I specify:
a, b = 2,3
and simply start the series at this point? If we then look at this section of code:
while b < n:
result.append(b)
a, b = b, a+b
return result
I'm assuming this while look simply satisfies the goal of the function, to give us the series until B is not less than n, all while appending to the empty list. Is the return result simply there to give us the printout? Sorry, I know it's basic, but I'm having a bit of difficult understanding functions, so I figured I would choose a pretty basic example.
Upvotes: 3
Views: 295
Reputation: 740
The function returns a list of the numbers in the Fibonacci sequence that are less than n
.
a
and b
, as you correctly pointed out, are initialized to 0
and 1
respectively as those are the first two numbers in the sequence. These values do matter because they are the first two numbers of the sequence by definition.
Technically, using 2
and 3
does still produce the same pattern as the Fibonacci sequence, but it will start at 2 instead of 0. So the resulting sequence would be {2, 3, 5, 8, 13, [...]}
. That said, the Fibonacci sequence starts with either a 1 or a 0 (again, by definition), thus, the sequence starting with 2 is only an "impartial" Fibonacci sequence.
2
and 3
happen to be the only (other) two numbers (that are 1 greater than or less than each other*) for which this will work, too. For instance, if you were to use 3 and 4, the sequence would go {3, 4, 7, 11, [...]}
which is clearly not the Fibonacci sequence, it merely takes the same pattern.
The reason you're returning the list depends on the problem that the code is trying to solve. If you want to print a list of the Fibonacci sequence containing numbers less than n
, your function will help you do that. How you use the list that the fib2(n)
function returns depends on the application of the code.
*Note that you could also start the series with 2 known Fibonacci numbers, and get a partial series just as before. For example,
a,b = (3,5)
Thus the resulting series would then be {3, 5, 8, 13, 21, [...]}
which is another partial Fibonacci sequence.
Upvotes: 0
Reputation: 188
Answers to your questions below:
"result = []
This to me seems like we are going to be posting the output into an empty dict."
Yes, this is to initialize the output variable, which is a list
, not dictionary.
"a, b = 0, 1
I'm having a bit of difficult understanding why we specify "a = 0" and "b = 1"? Is it simply to specify an initial value, whose value doesn't really matter? For instance, could I specify:
a, b = 2,3
and simply start the series at this point?"
Yes again. 0 and 1 are used to initialize your fibonacci variables. Note that 0 does not belong in the series, but is used since it is the difference between the first two numbers in the series. It is never appended to the output. You are right that you can use 2 and 3 to initialize the series too, which will begin the series at 3
. So these values do matter as such.
"If we then look at this section of code:
while b < n:
result.append(b)
a, b = b, a+b
return result
I'm assuming this while look simply satisfies the goal of the function, to give us the series until B is not less than n, all while appending the empty dictionary. Is the return result simply there to give us the printout?"
I think that the middle two statements here should be indented by a different number of spaces for your program to work. Indentation is how python identifies code blocks. Did you miss this while copying your code here?
Once that is taken care of, you're correct that the loop works to increment the fibonacci variables until the input parameter is exceeded. The return
statement is not meant to "printout", but to return the output list to the calling code; where it is assigned to your variable f100
.
Let us know if you have more questions; and good work for thinking this over yourself!
Upvotes: 1
Reputation: 4071
def fib2(n)
You are correct, it just defined a function
named fib2
that takes n
as a parameter.
result = []
result
will be of type list
a, b = 0, 1
For the Fibonacci sequence it is important to understand that the sequence adds n-1
and n-2
to the value its about to create, that is how 1 1 2 3 5 8...
is created.
Look at it like this:
0 1 1 2 3 5 8...
a b....
where a is 0 and b is 1, hence b
is the start of the sequence
while b < n:
result.append(b)
a, b = b, a+b
return result
Python is a great language, but it can be confusing.
a, b = b, a+b
Let's examine this. Python allows the assignment of variables such that you can assign variables without affecting the initial value.
That is, if a
is 0 and b
is 1, then a, b = b, a+b
will look like this:
a
will become b
, but will store it as a temp value, 1. It's important to know that a does not actually get assigned b
until this line of code is done.
b
will become a+b
or 0+1
and store that as the new value.
Now if b
is 2 and a
is 1 let's see what happens:
a, b = b, a+b
a
will become b
but not until after the line of code, so a
will become 2
b
will become a+b
, or in this case 1+2
. As you can see, a
being reassigned first does not actually change the value of a
in this expression. Pretty neat!
An easier way to think about this single line assignment is like this:
a, b = b, a+b
a1 = b
b1 = a+b
a = a1
b = b1
And yes, while b < n
is the conditional to check for to satisfy the while
loop. Once b
is no longer smaller than n
the loop will stop and result
will be returned
Upvotes: 2