[Tutor] multiple objects with one assignment?
steve at pearwood.info
Fri Jan 2 12:34:58 CET 2015
On Fri, Jan 02, 2015 at 05:25:12AM -0500, Brandon Dorsey wrote:
> I know there is are easier ways to assign multiple objects to a variable,
> but why, does the following code work? 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?"
There is no semi-colon involved.
The thing to remember is that *commas*, not parentheses, are used for
making tuples. The round brackets are just for grouping.
So these are exactly the same:
x = "hello", "world" # tuple of two strings
y = ("hello", "world") # also a tuple of two strings
So you can make a tuple of a single item:
x = 23, # same as x = (23,)
The exception is the empty tuple:
y = ()
More information about the Tutor