How to you get the value out of a tuple? The only way I seem to be able to do it is to set variables equal to the tuple indexes, but if that's the only way, I don;t see the point of using a tuple at all. I might as well just stick with the variables...
Let's take a tuple: aa = (1, 2, 3, 4) To access any of those values, I'd simply plug in aa[x], where x is the index I want it to be. Example: aa[1] = 2 aa[3] = 4 ab = aa[1] + aa[3] In the example below, ab would then be equal to six. This isn't the most useful for once-off things, indeed. But if you've got a long tuple, a for-loop would give you the ability to build do a lot with just a minimal amount of coding on your part.
Ok, I think I was using () instead of [] before, and I was getting an error that tuples cannot be called. This seems to be working now. Thanks!
If you use parentheses, it thinks you're calling a function. by using the square brackets, you're telling it to look for the indexed location.
Join our real-time social learning platform and learn together with your friends!