Don,
That line of code isn't one line, it is two, both
of which are indented within a while loop - the whole code is
here :
# Fibonacci series:
# the sum of two elements defines the next
a, b = 0, 1while a < 10:print(a) a, b = b, b+a
The line
a,b = b, b+a
is admittedly not well explained in the following text, but this is an example of a compound assignment in Python - ie a single statement that assigns to multiple variables at once.
A very simple form of this compound assignment can be found in the statement
a,b = 0,1
which results in a being assigned 0, and b being assigned 1
To understand how the more complex form works is to first look at
the two sides of the = separately : the right hand side is
evaluated as a tuple (a special structure within Python), where
the first element has the value of the variable 'a', and the 2nd
element has the value of the expression 'a+b'.
The left hand side of the '=' is the target for the assignment - so here the variable 'a' is assigned the value from the first element of the right hand tuple, and 'b' is assigned the value from the 2nd element of the right hand tuple.
So lets assume that going into the loop a = 0 and b = 1
so the tuple (b, b+a) is evaluated as being (1, 1) - so after the assignment a = 1 and b = 1
when a = 2 and b = 3 for example :
the right hand side tuple (b, b+a) is evaluated as (3, 5) - so after the assignment a = 3 and b = 5
I hope this helps.
https://docs.python.org/3/tutorial/introduction.html
this isn't explained
print(a)a, b = b, a+b
why have a?why have b = b ?why aren't 3 values printed?why only a+b is printed?
don
_______________________________________________ docs mailing list -- docs@python.org To unsubscribe send an email to docs-leave@python.org https://mail.python.org/mailman3/lists/docs.python.org/ Member address: anthony.flury@btinternet.com
-- Anthony Flury email : anthony.flury@btinternet.com