Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 21 Online
OpenStudy (anonymous):

Playing with bugs from pset2: (rehearsing for the quiz:) http://dpaste.com/770171/ If there's parentheses around the (i,) line 17 IDLE computes (0,) (0, 1, 1) (0, 1, 1, 2, 2, 2) (0, 1, 1, 2, 2, 2) -->why? With tuple(poly[i]*i,) IDLE displays TypeError: 'int' object is not iterable -->what does that actually mean, that it's not iterable? and why is that the case here? Finally, why is it again that you have the comma after the i? (still on line 17) Thanks a lot!

OpenStudy (anonymous):

All of these are good questions! Let me answer question 3 first. Let's say we had a list called x and it was equal to [1,2]. Now let's say I wanted to append the element 3 to the end. I could use the append method, but I could also use concatenation. x = x + [3]. Notice I had to put 3 in a list because concatenation requires two objects of the same type. Now let's look at tuples. Let's say I have a tuple called y equal to (1,2). I want to add the element 3 to the end. Note that I *can't* use an append method; tuples are immutable, which means they can't be changed. My only choice is to create a completely new tuple and write over my old tuple. Most people would be tempted to write this: y = y + (3) But that doesn't actually work! The reason is that Python is not quite smart enough to know that when you take an element and wrap parentheses around it, you're talking about a tuple. It just thinks it's a number surrounded by parentheses. So you have to tell Python "Hey, this 3 with parentheses here? That's a tuple, idiot!" The way you do that is to put a comma next to the 3 like so: y = y + (3,) Now there's no question that it's a tuple. It's weird, but it removes ambiguity from the language, which is a good thing.

OpenStudy (anonymous):

Okay, let's go back to question 1, which should be easier to answer now. Right now, the line says: deriv = deriv + (poly[i]*i,) It's figuring out what "poly[i]*i" is equal to, putting that result in a tuple (by sticking parentheses around it and the comma at the end) and then concatenating that onto the end of the tuple "deriv". But what happens if we wrap parentheses around the "i,". Something pretty interesting! deriv = deriv + (poly[i]*(i,)) So what's going on here? Let's take it from the back...(i,) is a tuple with one element. We're multiplying that by whatever poly[i] is. But what happens when you multiply a tuple by an integer? Well, it actually creates a tuple with that many copies of the same sequence. (2,) * 4 ==> (2,2,2,2) (1,3) * 3 ==> (1,3,1,3,1,3) So your tiny change actually concatenates a string with poly[i] copies of the tuple (i,), when you really just wanted to concatenate a single element: the result of poly[i]*i.

OpenStudy (anonymous):

Annnnd...question 2. An "iterable" is any object type that can be iterated, or "walked through". Strings, lists, tuples, and dictionaries are all iterable because they contain multiple elements. Ints cannot. The function tuple can only take something that is already an iterable and turns it into a tuple. So tuple([3,4,5]) will work, but tuple(6) will not. But here's where it gets weird: >> 3*5, >> (15,) Notice I don't even need the parentheses, and it automatically makes it a tuple for me. But >> tuple(3*5,) gives me an error...you'd think it would evaluate "3*5," and get a tuple, and then turn that tuple into a tuple (which is obviously unnecessary). But it doesn't understand that what's inside there is a tuple. But if you type >> tuple((3*5,)) then there's no problem. I guess the lesson here is, if you're going to use the comma to turn something into a tuple, you don't need to actually call the tuple function, because weird things happen.

OpenStudy (anonymous):

integers do not support iteration. they are not an Iterator Type. http://docs.python.org/library/stdtypes.html#iterator-types at line 17 i suspect the offending integer is i line 17 is concatenating two tuples - deriv and (poly[i]*i,) (poly[i]*i,) is in the end a single number like (2.3,) The Tutorial in the Python docs is a must http://docs.python.org/tutorial/datastructures.html#tuples-and-sequences I usually work with a different data type (like a list) inside a function, becuase it is easier to work with, then turn it into a tuple when I return it if that is required.

OpenStudy (anonymous):

@shandelman exhaustive and very helpful answers, thanks a lot! @bwCA got it, thanks!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!