Question

I am trying to understand Python, but I still don't get it. I am new to the language, and wants to understand it properly. This is a line from a Fibonacci sequence using loops. Please explain the meaning of this code. I am trying to get the pattern by hand. I got the pattern up to 3, but after 3 I am not getting the answer.

a, b = 0, 1
while b < 50:
    print(b)
    a, b = b, a + b
Was it helpful?

Solution

a, b = b, a + b

This is known as multiple assignment. It's basically an atomic version of:

a = b
b = a + b

By atomic, I mean everything on the right is calculated before pacing it into the variables on the left. So a becomes b and b becomes the old version of a plus b, equivalent to the non-atomic:

old_a = a
a = b
b = old_a + b

So, in terms of what you see:

        a                        b               output
================    =========================    ======
(initial values)        (initial values)
        0                        1                  1
(becomes prev b)    (becomes sum of prev a,b)
        1                        1                  1
        1                        2                  2
        2                        3                  3
        3                        5                  5
        5                        8                  8

That exact code (along with the explanation of multiple assignment) can be found here in the tutorial.

OTHER TIPS

It's multiple assigment (or tuple unpacking).

According to Python Tutorial:

>>> # Fibonacci series:
... # the sum of two elements defines the next
... a, b = 0, 1
>>> while b < 10:
...     print(b)
...     a, b = b, a+b
...
1
1
2
3
5
8

This example introduces several new features.

The first line contains a multiple assignment: the variables a and b simultaneously get the new values 0 and 1. On the last line this is used again, demonstrating that the expressions on the right-hand side are all evaluated first before any of the assignments take place. The right-hand side expressions are evaluated from the left to the right.

How about multiple answers?

def fib(num):
    a = 0
    b = 1
    while b <= num:
        prev_a = a
        a = b
        b = prev_a +b
        #print b                                                                                                          
    return a

print fib(13)

def pythonic_fib(num):
    a,b = 0,1
    while b <= num:
        a,b = b, a+b
    return a

print pythonic_fib(13)

def recursive_fib(num, a, b):
    if (b >= num):
        return b
    else:
        return recursive_fib(num, b, a+b)

print recursive_fib(13, 0, 1)

I know this is an old question, but just thought I'd through in my 2-cents since a lot of these seem a bit overly complicated for just the fibonacci sequence (outside the given answer), in case someone was still looking. You could do this:

a=1
b=0
while b<400:
    a=a+b
    b=a+b
    print(a)
    print(b)

This will give the desired output of the sequence(to whatever you set b less than).

#The easy way to generate Fibonacci series in python is 
user = input('Please enter the integer range for Fibonacci series: '); # take input from user form the range of Fibonacci series.
try:# to ignore wrong inputs and be aware from error we use try and except block
    d=int(user);# converts the user input to type integer.
    a=0; #initialization``
    b=1; #initialization
    print(a,b,end=' '); # print initial value of a and b
    for c in range(0,d): # here c is iterable variable and in range o is the starting point and d is the ending range which we get from user
        c=a+b;
        a=b;
        b=c;
        print(c,end=' ');

except Exception as e:
    print(e); # if any error occurred in the input here it shows which error occurs
a= int(input("Enter the first number:"));
Enter the first number:0
b= int(input("enter the second number:"));
enter the second number:1
n= int (input("enter the number of terms:"));
enter the number of terms:10
print(a,b);
0 1
while(n>2):
      c=a+b;
      a=b;
      b=c;
      print(c);
      n=n-1;


1
2
3
5
8
13
21
34
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top