1.8: Tubles - nealtran1905/PythonForResearch GitHub Wiki
Tuples are immutable sequences typically used to store heterogeneous data.
The best way to view tuples is as a single object that consists of several different parts. Tuples have many uses in Python programming. One especially important use case is when you want to return more than one object from your Python function. In that case, you would typically wrap all of those objects within a single tuple object, and then return that tuple. Let's now take a look at some of the basic operations that we can do using tuples. I'm first going to construct a tuple. I'm going to just call it capital T. And let's just put in a few numbers in my tuple. Let's say 1, 3, 5, and 7. Again, tuples are a type of sequence. So if I wanted to know how many objects I have in my tuple, I can use the len function. I can also concatenate tuples. So I can do something like T plus. I need a new tuple here. Let's say 9 and 11. And in this case, Python returns a new tuple to me where the two tuples have been put together. Because tuples are sequences, the way you access different objects within a tuple is by their position. So if I wanted to access the second object in my tuple, I would type capital T, square bracket, and 1. And remember, using position 1 is going to give me the second object in the tuple, because indices in Python start at 0.
Another operation that you need to be familiar with
is how to pack and unpack tuples.
Imagine I have two numbers-- two variables, x and y. Let's just quickly create them. Let's say x is equal to this. y is equal to this. Imagine now if I wanted to construct a tuple object. We could think of these two numbers x and y as coordinates. So I could do something like this. I could define my coordinate as a tuple, which consists of two objects, x and y. If I now ask Python, what is the type of my coordinate object, Python will tell me that's a tuple. This operation is called packing a tuple, or tuple packing. Another related operation is how you unpack a tuple. Our coordinate contains two numbers. Our coordinate object is a tuple. Here is how you can unpack this tuple. Let's say I would like to unpack that into two numbers-- say c1 and c2, perhaps short for coordinate 1 and coordinate 2. I can just write c2 and c2 as a tuple. And then I can assign coordinate into that tuple. If I now look at the values of c1 and c2, I will observe the following. c1 contains the first object in that tuple. Where c2 contains the second object of the tuple. We can also use tuples in FOR loops, which is extremely handy. Let's say I've created multiple coordinates. So in this case, my object coordinates is a list which consists of tuples where each tuple consists of two numbers. What if I wanted to loop over these objects in say a FOR loop? Then I can do the following. I can call these coordinate pairs x and y. Let me enclose these in parentheses here, in coordinates. And I can ask Python to print the value of x and y. So this is what's happening here. Coordinates is a list of tuples. In my FOR loop I am going over that container, that sequence of coordinates, one at a time. The key part to focus here is how I unpack the tuples from my list of tuples. So the syntax is 4x comma y in coordinates. In other words, I'm unpacking the tuples within the coordinates list one at a time. One more thing about unpacking tuples in a loop. I don't necessarily need the parentheses surrounding x and y. So I can also just type for x comma y in coordinates. And then I just have the same print function. This also works. However, sometimes having the extra parentheses around the tuple will make it clearer to you that you are dealing with a tuple object. It's relatively easy to understand how to construct and deal with tuples that contain multiple objects. But what if you just have one object within your tuple? Let's experiment with that first. Let's start with a tuple where we have two objects, say 2 and 3. We know this is a tuple from the way we constructed. We can also ask Python to return to us the type of the object, which we now happen to call c. If I wanted to construct a new tuple with just one object in it, you might guess that we could just use the following structure. We could just type c is equal to parentheses. And we put the one number in there. However, if we ask Python now, what is the type of this object? It's not actually a tuple. If we check the type of this object by typing type parentheses c, Python is telling us that c is actually an integer. But this is not what we wanted. We wanted to have a tuple object that contains just one object. This is where the syntax is a little bit counterintuitive. To construct a tuple with just one object, we have to use the following syntax. We start by saying c is equal to. We put our tuple parentheses. We put it in our number 2. And we add the comma. When we now ask Python what type of object is c, we know that this is a tuple. Finally, if you want, you can also omit the parentheses. This also works. But the code is not quite as clear. That's why I recommend using parentheses whenever you're using a tuple.