Re: [Tutor] multiple objects with one assignment?
On Fri, Jan 2, 2015 at 6:08 AM, Ben Finney wrote: > 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? > Well I knew that those data structures represent one value that can hold "x" amount of objects, but what I didn't realize was that commas are used for making tuples, not parentheses. Thank you for the clarification. -- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] multiple objects with one assignment?
On Fri, Jan 2, 2015 at 6:27 AM, Dave Angel wrote: 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. That's what I was baffled about. -- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] multiple objects with one assignment?
On Fri, Jan 2, 2015 at 6:34 AM, Steven D'Aprano wrote: > The thing to remember is that *commas*, not parentheses, are used for > making tuples. The round brackets are just for grouping. > That's what I was confused about. I didn't realize commas defined tuples, not parentheses. Is this the case for list and dictionaires as well? -- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] multiple objects with one assignment?
On Fri, Jan 02, 2015 at 06:51:16AM -0500, Brandon Dorsey wrote: > On Fri, Jan 2, 2015 at 6:34 AM, Steven D'Aprano wrote: > > > The thing to remember is that *commas*, not parentheses, are used for > > making tuples. The round brackets are just for grouping. > > > > That's what I was confused about. I didn't realize commas defined tuples, > not parentheses. Is this the case > for list and dictionaires as well? No. x = 1, 2 y = 1, 2 cannot make a tuple for x and a list for y. How would the compiler know which you wanted? The syntax for lists: [a, b, c, ... ] requires the square brackets. You must have a comma between items, and optionally after the last item. That makes it easy to add new items to large lists without worrying about keeping the last item special. Here's an example from some code of mine: PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, ] Now I can add or remove lines without bothering to remove the comma from the very last line. If there are no items, you can't use a comma: x = [,] is a syntax error. Likewise for dicts, and in Python 3, sets: d = {1:'a', 2:'b'} s = {1, 2, 3} # set syntax is Python 3 only It is the curly brackets { } that tell Python you're creating a dict or set, not the commas. The commas separate items, that is all. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] multiple objects with one assignment?
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 = () -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] multiple objects with one assignment?
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
Re: [Tutor] multiple objects with one assignment?
Brandon Dorsey 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor