Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 21 Online
OpenStudy (anonymous):

Can anyone explain what the commas do, and when 'a' and 'b' are defined in this code http://dpaste.com/557932/? It's actually one of the soln on the Project Euler problem 5 thread which I don't understand. Using it to complement my 6.00 study. Thanks!

OpenStudy (anonymous):

def gcd(a, b): while b != 0: a, b = b, a % b ## this is the same thing as ## a = b ## b = a % b ## this code decided to put it on one line by separating with a ',' return a def lcm(a, b): return a*b/gcd(a, b) print reduce(lcm, range(1, 20+1))

OpenStudy (anonymous):

it's called tuple unpacking: see the bottom of this, http://docs.python.org/release/1.5.1p1/tut/tuples.html

OpenStudy (anonymous):

here is another good one: http://en.wikibooks.org/wiki/Python_Programming/Tuples#Packing_and_Unpacking

OpenStudy (anonymous):

sorry bwCA, but i don't understand where's the tuple unpacking, and how a and b get their values?

OpenStudy (anonymous):

ok bwCA, i thought about it again and know where the tuple unpacking is. i'm still trying to get by head around why it works because of the order of statements, but i should be fine. thanks again!

OpenStudy (anonymous):

a,b = c,d Is equivalent to a=c b=d

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!