On 01/02/2015 05:25 AM, 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?"
print(greetings)
x = 1, 2, 3, 4, 5, 6, 7
print(x)
I assumed that you could only assign one object per assignment without the
presence of tuples, list, or dictionaries.
Ben's description is very good. But I think the main thing you're
missing is that a tuple is created by the comma, not by parentheses. In
some contexts, parentheses need to be added to make it non-ambiguous,
since comma is overloaded.
a = 1, 2, 3
1,2,3 is a tuple. This statement is identical to:
a = (1, 2, 3)
Likewise when you say:
return 1, 2
you are returning a tuple.
If you are passing a (literal) tuple as an argument to a function, you
would need parens, since the function call also uses commas to separate
the arguments:
myfunc(val1, (21, 22, 23), val3)
Here the function is being called with 3 arguments:
val1
the tuple
val3
--
DaveA
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor