202 http://inventwithpython.com/hacking
Email questions to the author: [email protected]
Figure 14 - 6. Cuisenaire rods demonstrating Greatest Common Divisor.
More information about Cuisenaire rods can be found at http://invpy.com/cuisenaire.
Practice Exercises, Chapter 14, Set B
Practice exercises can be found at http://invpy.com/hackingpractice14B.
Multiple Assignment
Our GCD function will use Python’s multiple assignment trick. The multiple assignment trick lets
you assign more than one variable with a single assignment statement. Try typing the following
into the interactive shell:
spam, eggs = 42, 'Hello'
spam
42
eggs
'Hello'
a, b, c, d = ['Alice', 'Bob', 'Carol', 'David']
a
'Alice'
b
'Bob'
c
'Carol'
d
'David'
The variable names on the left side of the = operator and the values on the right side of the =
operator are separated by a comma. You can also assign each of the values in a list to its own
variable, if the number of items in the list is the same as the number of variables on the left side
of the = operator.