1

I am new to python and I have really poor expiriences with other codes. For the most of you a stupid question but somewhere I should start.

 def fib(n):
 a, b = 0, 1
 while a < n:
     print(a, end=' ')
     a, b = b, a+b
 print()

I don't understand why one should enter a, b = b, a+b I see and understand the result and I can conclude the basic algorithm but I don't get the real understanding of what is happening with this line and why we need it.

Many thanks

3
  • 1
    Do you understand what the answers below are saying about tuple assignment / packing / unpacking? If so, and you still don't get why your code works then it might help if you try acting like the Python interpreter and work through your code on paper, step by step. Commented Jan 22, 2016 at 10:03
  • Thank you, I will keep this advice for my next problems in mind. It's also good to know how these things are called.. Commented Jan 22, 2016 at 11:18
  • Understood. It's hard to ask the right question when you don't know the names of things! A good tutorial should tell you the names of things, but I realise that it can be hard to keep track of all that stuff when you start learning to code, and it's hard to know what information is important to memorize and what's just trivia. All I can suggest is to keep studying your book / tutorial and playing with the example code, and eventually it will sink in. Commented Jan 22, 2016 at 11:26

2 Answers 2

3

This line is executed in the following order:

  1. New tuple is created with first element equal to b and second to a + b
  2. The tuple is unpacked and first element is stored in a and the second one in b

The tricky part is that the right part is executed first and you do not need to use temporary variables.

Sign up to request clarification or add additional context in comments.

Comments

2

The reason you need it is because, if you update a with a new value, you won't be able to calculate the new value of b. You could always use temporary variables to keep the old value while you calculate the new values, but this is a very neat way of avoiding that.

It's called sequence unpacking.

In your statement:

a, b = b, a + b

the right side b, a + b creates a tuple:

>>> 8, 5 + 8
(8, 13)

You then assign this to the left side, which is also a tuple a, b.

>>> a, b = 8, 13
>>> a
8
>>> b
13

See the last paragraph the documentation on Tuples and Sequences:

The statement t = 12345, 54321, 'hello!' is an example of tuple packing: the values 12345, 54321 and 'hello!' are packed together in a tuple. The reverse operation is also possible:

>>> x, y, z = t

This is called, appropriately enough, sequence unpacking and works for any sequence on the right-hand side. Sequence unpacking requires the list of variables on the left to have the same number of elements as the length of the sequence. Note that multiple assignment is really just a combination of tuple packing and sequence unpacking.

3 Comments

The Siftables TED talk has a nice example generating the Fibonacci sequence at 2 mins 20 sec
Thank you so much! I got it now. That's sounds quite logical to me.
@AlexNewmiller That's great. Thanks for the feedback.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.