Re: [Tutor] Understanding code line
On 3/21/2014 3:40 PM, Steven D'Aprano wrote: a = b This assigns the value to b. So if b was 4, now a is also 4. Steven means 'assigns the value to a' here. For anyone looking down the line... ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Understanding code line
On 03/21/2014 06:14 PM, Gary wrote: Pythonists I am trying to understand the difference between a = b b = a + b and a,b = b, a+ b When used in my Fibonacci code the former generates 0,1,2,4,8,16,32 and the later Generates 0,1,1,2,3,5,8,13,21,34,55,89. The second is the sequence I want, but I would Like to understand the second code sequence better so I can write the code in R and Scilab as well as python. To understand a, b = b, a+ b correctly, think of it operating in parallel, each assignment independantly. So, it does what you think (and need to compute a fibonacci sequence). A better explanation, maybe, is that python has *tuples*, which are groups of values held together; and are better written inside parens (), like (1,2,3) or (x,y,z). But () are not compulsary, a comma is enough to make a tuple. Here we have two 2-tuples, also called pairs, meaning tuples of 2 values. The assignment thus actually means: (a, b) = (b, a+b) So, python *first* constructs the right side tuple, *then* assigns it to the left side; however, since we don't have on the left side a (named) tuple variable, it actually assigns to the local variables a & b, in //. Hope it's clear. You can consider this last step as nice, little magic. Rarely needed, but very handy when needed. d ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Understanding code line
On Fri, Mar 21, 2014 at 01:14:22PM -0400, Gary wrote: > > Pythonists > > I am trying to understand the difference between > > a = b > b = a + b > and > > a,b = b, a+ b Try to evaluate the code in your head, as if you were the Python interpreter. Starting with the first version: a = b This assigns the value to b. So if b was 4, now a is also 4. b = a + b This takes the value of a and the value of b, adds them together, and assigns the result to b. Since the previous line set a to b, this is exactly the same as: b = b + b so if b was 4, it then becomes 8. The end result of these two lines is that b gets doubled each time. The important thing to realise here is that a gets its new value, the old value being lost, before it gets used to calculate b. Now for the second version: a, b = b, a+b Here, Python evaluates the right hand side of the = sign first, then does the assignments. On the RHS, it evaluates b, and a+b. Then it matches them with the names on the LHS, so that a gets the old value of b, and b gets the value of (a+b). The important thing here is that the values on the RHS are calculated before the assignments, so it works the way you expect. Most programming languages do not allow code like this, so you would have to use a temporary variable to get the same result: temp = a # remember the old value of a a = b # set the new value of a b = temp + b # set the new value of b -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Understanding code line
Gary writes: > Pythonists > > I am trying to understand the difference between > > a = b Retrieves the value referenced by ‘b’, then binds the name ‘a’ to the value. > b = a + b Evaluates the expression ‘a + b’, then binds the name ‘b’ to the result. > and > > a,b = b, a+ b Evaluates the expression ‘b, a + b’ – which will be a two-value tuple – then binds the names ‘a’ and ‘b’ to the two values from that tuple. Since the right side of the assignment, in each case, is evaluated before bindling the left side, the resulting values will depend on what the names are *currently* bound to at the time of evaluation. -- \“When in doubt tell the truth. It will confound your enemies | `\ and astound your friends.” —Mark Twain, _Following the Equator_ | _o__) | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Understanding code line
On 21/03/14 17:14, Gary wrote: Pythonists I am trying to understand the difference between a = b b = a + b This does exactly what it says. It first makes a and b identical then makes b equal their sum, that is, b+b and a,b = b, a+ b The second form is not always available in programming languages so R may not support it directly. The more general version would be: temp = a a = b b = temp + b Like to understand the second code sequence better so I can write the code in R and Scilab as well as python. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Understanding code line
On Mar 21, 2014 1:16 PM, "Gary" wrote: > > > Pythonists > > I am trying to understand the difference between > > a = b You have overwitten a > b = a + b > and > > a,b = b, a+ b This one evaluates the right side first, then assigns the result to a and b > When used in my Fibonacci code the former generates 0,1,2,4,8,16,32 and the later > Generates 0,1,1,2,3,5,8,13,21,34,55,89. The second is the sequence I want, but I would > Like to understand the second code sequence better so I can write the code in R and Scilab as well as python. > G > > ___ > 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