[Tutor] multiple objects with one assignment?

Ben Finney ben+python at benfinney.id.au
Fri Jan 2 12:08:48 CET 2015


Brandon Dorsey <brandontdr at gmail.com> writes:

> I know there is are easier ways to assign multiple objects to a
> variable,

Not really. Every name binds to exactly one value.

Values can themselves be collections of other values, which might be
what you're thinking of.

> Why does it return a tuple versus a list? I know it has something to
> do with the semi-colon, but I didn't know it wouldn't raise an error.
>
> greetings = "hello,", "what's", "your", "name?"

The value defined on the right-hand side of the assignment operation
(the ‘=’) is a literal tuple. That one value then gets the name
‘greetings’ bound to it.

> print(greetings)

The ‘print’ function implicitly creates a string representation of its
parameter; the string representation of a tuple shows all the values in
that tuple.

> x = 1, 2, 3, 4, 5, 6, 7

Another literal tuple is created on the right-hand side, and the name
‘x’ is bound to that tuple.

> I assumed that you could only assign one object per assignment without
> the presence of tuples, list, or dictionaries.

I don't really understand that statement.

Does it help you to understand if I clarify that a tuple is one value?
That a list is one value? That a dict is one value?

Each of those types implements a collection; a tuple value (and likewise
a list value, a dict value) itself contains other values. But those
values are only *contained in*, not identical to, the collection.

-- 
 \        “Laurie got offended that I used the word ‘puke’. But to me, |
  `\                 that's what her dinner tasted like.” —Jack Handey |
_o__)                                                                  |
Ben Finney



More information about the Tutor mailing list