Now I got it, thanks :-)

a, b = b, b + a

... I was was wrongly assuming that "a" and "b" on the left side "talk" to
each other and that  "a" tells "b" something like: "Hey 'b' ... I just
assigned another value to you, make sure you execute it." But "a" and "b"
don't talk to each other. Each of them executes what it is supposed to and
doesn't take notice of what its neighbor variable does. The more
sophisticated explanation (from my view point as an absolute beginner who's
not familiar with most programming concepts yet) is that "a" and "b" on the
left side are unchangable tuples and they simply get unpacked on the right
side.

I wrote an even more primitive program which helped me understand what
*exactly* happens with "a" and "b" with each run:

# 1st run
a, b = 1, 5
print(a) # 1
print(b) # 5

# 2nd run
a, b = b, b + a
print(a) # a = b = 5
print(b) # b + a = 5 + 1 = 6

# 3rd run
a, b = b, b + a
print(a) # a = b = 6
print(b) # b + a = 6 + 5 = 11

# 4th run
a, b = b, b + a
print(a) # a = b = 11
print(b) # b + a = 11 + 6 = 17

# 5th run
a, b = b, b + a
print(a) # a = b = 17
print(b) # b + a = 17 + 11 = 28

# 6th run
a, b = b, b + a
print(a) # a = b = 28
print(b) # b + a = 28 + 17 0 45

All the best,

Raf


On Sun, Nov 24, 2013 at 12:33 PM, Dave Angel <da...@davea.name> wrote:

> On Sun, 24 Nov 2013 11:24:43 +0100, Rafael Knuth <rafael.kn...@gmail.com>
> wrote:
>
>>     a, b = b, a +b
>>
>
>
>  a = b = 1
>> b = a + b = 1 + 1 = 2
>>
>
> I suggest you play with the statement a bit. Print out both values each
> time through the loop.
>
> The expression b, a+b produces a tuple. The left side a, b *unpacks* that
> tuple into the two variables.a and b.
>
> Perhaps a simpler case might help. Try a, b = b, a   What would you expect
> it to do and why?
>
> --
> DaveA
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to