[Tutor] Beginner Exercise: Why Didn't I Break It?

Jerry Hill malaclypse2 at gmail.com
Tue Dec 20 19:00:14 CET 2011


On Tue, Dec 20, 2011 at 12:31 PM, Homme, James <james.homme at highmark.com> wrote:
> So far, the data types this little book has talked about are strings and numbers.
...
> return jelly_beans, jars, crates
...
> # It returns three things in parentheses, which, I guess is one group of things. I thought it would complain.


You book may not have talked about it yet, but there is a third data
type involved here, a tuple.  The secret_formula() function doesn't
actually return three separate things.  It returns a single object
that contains a sequence of three other objects.  In this case, it
specifically returns a tuple (rather than a list or other sequence
type).

Take a look here
http://docs.python.org/tutorial/datastructures.html#tuples-and-sequences
for more about tuples in particular, and the rest of that page for an
overview of sequence types in general.

>beans, jars, crates = secret_formula(start_point)

This line performs what python calls "sequence unpacking" (sometimes
also called "tuple unpacking").  That's described in the same link I
referenced above.

Hopefully that gives you a few places to read more without explaining
the whole thing.

--
Jerry


More information about the Tutor mailing list