Reading this Fibonacci function

I'm trying to read the following code, but it's a little hard for me:

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]

      

Let's start with:

def fib2(n) 

      

As I understand it, we are just creating a definition where n will specify the parameter. Then move on to:

result = [] 

      

It looks to me like we're going to post the output to an empty list. From there we go to:

a, b = 0, 1 

      

I have a bit of a difficult understanding, why do we specify "a = 0" and "b = 1"? Is it just to provide an initial value, the value of which doesn't really matter? For example, I can specify:

a, b = 2,3 

      

and just start the streak at this point? If we look at this section of code:

while b < n: 
    result.append(b) 
    a, b = b, a+b
return result

      

I assume this, although the look just serves the purpose of the function to give us a series until B is less than n, all when added to an empty list. Is the return result just there to give us a printout? Sorry, I know this is basic, but I have some complex understanding functions, so I figured I'd go with a fairly simple example.

+3


source to share


3 answers


def fib2(n)


You are right, it just defined function

with a name fib2

that takes n

as a parameter.

result = []


result

will be type list

a, b = 0, 1


For the Fibonacci sequence is important to understand that the sequence of adds n-1

and n-2

the value that it is going to create, that is, how to create 1 1 2 3 5 8...

.

Look at it like this:
0 1 1 2 3 5 8...


a b....

where a is 0 and b is 1, so it 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 take a look at this. Python allows you to assign variables in such a way that you can assign variables without affecting the initial value.

That is, if a

equal to 0, but b

equal to 1, then it a, b = b, a+b

will look like this:



a

will become b

, but will store it as a temporary value. 1. It's important to know that a doesn't actually receive b

until after that line of code is executed.
b

will become a+b

or 0+1

and will save it as a new value.

Now, if b

equals 2 and a

equals 1, let's see what happens:
a, b = b, a+b

a

will become b

, but only after a line of code, so a

will 2


b

become a+b

, or in this case 1+2

. As you can see, a

being reassigned first doesn't actually change the value a

in that expression. Pretty neat!

An easier way to think about this separate task is as follows:
a, b = b, a+b

a1 = b
b1 = a+b
a = a1
b = b1

      

And yes, it while b < n

is a prerequisite for matching for a loop while

. If it is b

already at least n

, the loop will stop and result

return.

+2


source


The answers to your questions are below:

"result = []

      

It looks to me like we are going to post the output to an empty dict.

Yes, this is for initializing the output variable, which is list

not a dictionary.

"a, b = 0, 1 

      

I have a bit of a difficult understanding, why do we specify "a = 0" and "b = 1"? Is it just to provide an initial value, the value of which doesn't really matter? For example, I can specify:

a, b = 2,3 

      

and just start the streak at this point? "



Yes again. 0 and 1 are used to initialize fibonacci variables. Note that 0 is not included in the series, but is used because it is the difference between the first two numbers in the series. He never joins the exit. You are correct that you can use 2 and 3 to initialize the series, which will start the series in 3

. Hence, these meanings have meaning as such.

"If we look at this section of code:

while b < n: 
result.append(b) 
a, b = b, a+b
return result

      

I assume this, although the look just serves the purpose of the function to give us a series until B is less than n, all while adding an empty dictionary. Is the return result just there to give us a printout? "

I think there should be two-line padding here for different programs. Indentation is how python identifies blocks of code. Did you miss this when copying the code here?

Once taken care of, you are correct that the loop works to increase the fibonacci variables until the input parameter is exceeded. The statement is return

not meant to "print", 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 to reflect on yourself!

+1


source


The function returns a list of numbers in the Fibonacci sequence that are smaller n

.

a

and b

, as you correctly pointed out, are initialized to 0

and 1

respectively, since these are the first two numbers in the sequence. These values are done because they are the first two sequence numbers by definition.

Technically, using 2

and 3

still creates the same pattern as the Fibonacci sequence, but starts at 2 instead of 0. So the resulting sequence will be {2, 3, 5, 8, 13, [...]}

. However, the Fibonacci sequence starts at 1 or 0 (again by definition), thus the sequence starting at 2 is only the "impartial" Fibonacci sequence.

2

and 3

are the only (other) two numbers (1 is greater or less than each other *) for which this will work too. For example, if you have to use 3 and 4, a sequence will go {3, 4, 7, 11, [...]}

that is clearly not a Fibonacci sequence, it just takes the same pattern.

The reason why you return a list depends on the problem the code is trying to solve. If you want to print a Fibonacci sequence list containing numbers less than n

, your function will help you do that. How you use the list returned by the function fib2(n)

depends on the application of the code.

* Note that you can also run a series with two known Fibonacci numbers and get a partial series as before. For example,

a,b = (3,5)

      

Thus, the resulting series will then {3, 5, 8, 13, 21, [...]}

be another partial Fibonacci sequence.

0


source







All Articles