4

I am not sure what would be an appropriate heading for this question and this can be a repeated question as well. So please guide accordingly.

I am new to python programming. I have this simple code to generate Fibonacci series.

1: def fibo(n):
2:    a = 0
3:    b = 1
4:    for x in range(n):
5:        print (a, end=' ')
6:        #a, b = b, a+b
7:        a = b
8:        b = a+b
9:    print()
10: num = int(input("enter n value: "))
11: print(fibo(num))

If I execute the above code as-is the result I get is as follows

enter n value: 10
0 1 2 4 8 16 32 64 128 256 

If uncomment #6 and comment lines #7 and #8 the result I get is the actual fibo series.

enter n value: 10
0 1 1 2 3 5 8 13 21 34 

I would like to know what is the difference between

a, b = b, a + b 

and

a = b
b = a + b

Programming IDE used: PyCharm Community 2017.3

aioracle
  • 371
  • 2
  • 7
  • 20
  • both are same in python it is more easy to assign multiple values in a single line – Midhun Mohan Jan 03 '18 at 06:18
  • @MidhunMohan That should be an answer, not a comment. (Well in any case it's not correct, but things like that which answer the question should be posted as answers.) – David Z Jan 03 '18 at 06:25
  • @DavidZ i thought to give the author a hint so that he could come up with more specific question since the author is a newbie i thought like giving the author some hints will help to solve the issue by himself – Midhun Mohan Jan 03 '18 at 06:35
  • @MidhunMohan That's a reasonable thought, but the thing is, what you posted is an outright answer to the question, not just a hint. Besides, the goal here is not just to help askers get their questions answered, but also to have those answers documented for other people who come along with the same question. Hints are not that helpful for those other people. – David Z Jan 03 '18 at 06:47
  • @DavidZ So you are suggesting me to make it as a answer – Midhun Mohan Jan 03 '18 at 06:48
  • 1
    @MidhunMohan Yes. At least, next time you find yourself in a similar situation, make it an answer. (This one time, it doesn't really matter.) – David Z Jan 03 '18 at 06:51
  • @MidhunMohan and David Z thank you both for the inputs. I does makes scene but at times have confusions when I search for answers on internet I was not getting proper answers. Also, based on the multiple assign feature in Python this made me more confused hence posted this. I have the answer to a certain level but still searching for more clarity. Overall Thank you both for the concern and assistance. – aioracle Jan 03 '18 at 06:52
  • @aioracle are you clear with the assignment operation now – Midhun Mohan Jan 03 '18 at 07:02
  • @MidhunMohan yes now clear. Have received more answers will go through this. – aioracle Jan 03 '18 at 07:10

5 Answers5

6
a = b
b = a + b

is actually:

a = b
b = b + b

what you want is:

a = b
b = old_value_of_a + b

When you do a, b = b, a + b it really is doing:

tmp_a = b
tmp_b = a + b
a = tmp_a
b = tmp_b

which is what you want

excalibur1491
  • 1,201
  • 2
  • 14
  • 29
  • this is interesting. So a,b=b,a+b the interpreter will do this temporary allocation of the respective values and then processes it. But one part is still confusing, what is the order or sequence of execution in this? If a=b is applied first then the value gets replaced correct? – aioracle Jan 03 '18 at 06:46
  • It will evaluate the right hand side of the `=` first, as usual. The right hand side will create a tuple (a pair basically) of values that will then be assigned ti the left hand side. So it literally translates to what I wrote in the answer – excalibur1491 Jan 03 '18 at 07:07
  • You may want to check this out: https://wiki.python.org/moin/TupleSyntax . It is building a tuple on the right hand side, then assigning it to the left hand side. – excalibur1491 Jan 03 '18 at 07:10
  • Actually this is probably a better link: http://interactivepython.org/runestone/static/thinkcspy/Lists/TupleAssignment.html – excalibur1491 Jan 03 '18 at 07:11
4

In line 7, you've already assigned the value in b to a, so in line 8, new value for b is actually double the old b's value.

While in line 6, the values on the right side of = will be using the old values, that's why you could get Fibo series.

nevets
  • 4,631
  • 24
  • 40
2

Assignment Statements assigns reference of source variable to target variable. Let walk through an example to understand more

>>> a = 5
>>> b = 6
>>> a = b

In this example b is source variable and a is the target variable. Now memory address for both of these variables are same. We can confirm this as well

>>> hex(id(a)), hex(id(b))
>>> ('0x1002739e0', '0x1002739e0')

Another test to confirm this is to use is operator

>>> a is b
>>> True

Now coming back to your example. First statement

>>> a, b = b, a + b 

Assignes b to a and (a+b) to b. This happens as a single operation so both variables are different. We can apply above tests to confirm this

>>> a is b
>>> False

>>> hex(id(a)), hex(id(b))
>>> ('0x1002739e0', '0x2008739t0')

The second statement

 >>> a = b
 >>> b = a + b

Assignes b to a and then (a+b) to b. These are two different statements, so at first step a and b are already identical. Thus the second statement is equivalent to b = b + b.

Sohaib Farooqi
  • 5,457
  • 4
  • 31
  • 43
  • your answer has give me a full explanation and understanding. Thank you very much. Sorry cannot mark your response as answer too. – aioracle Jan 03 '18 at 07:31
0

Thought make it simple so anyone can understand it if you use this kind of syntax

a = 10
b = 20

a = b
b = a+b

print (a)
print (b)

after initially assigning a = 10 it will be assigning a = 20 since python is dynamically typed language it will change the value of variable a from 10 to 20 so the result will be like

a=20
b=40

but if we use

a = 10
b = 20

a,b = b,a+b

print (a)
print (b)

this will be assigning the values in a single line so the values of a and b will be exactly used from what it is initialised above it and the result will be like which is the correct solution

a=20
b=30
Midhun Mohan
  • 552
  • 5
  • 18
0

I think the # line is pythonic solution. But if you got confused,you can you use a variable which is temporary. you can assign the value temp before, then you can change the values

gokhan_ozeloglu
  • 134
  • 1
  • 7