Think of Tuples as Rows

00:00 Let’s get started talking about tuples, or immutable sequences, and I want to start off this part of the course by helping you to find a way to think of tuples as rows.

00:12 Let’s start off by, how do they even pronounce this word? There are actually two valid pronunciations of the word tuple. The first one is the one I just said, toople.

00:22 So you would pronounce it like in the word quadruple. And another one is tupple, like in the word quintuple. Both of these pronunciations are fine, and both are used throughout the Python community.

00:34 So you can pick. Either you can mix and match, or you can just settle on one of those. It seems like eventually I settled on calling them tooples, even though I remember also talking about them as tupples sometime in the past.

00:46 And I may end up throwing in a tupple here or there during this course just to make sure that everyone’s happy or everyone’s unhappy. I don’t know. Both of these pronunciations are fine and refer to the same thing in Python.

01:00 Now what is that thing in Python? What is a tuple? A tuple is a finite, ordered, immutable sequence of values. That’s a mouthful. It gets worse. Python borrows the name and the notation from mathematics, which means per definition, each element is separated by a comma inside a tuple, and all the elements are surrounded by a single pair of parentheses.

01:21 This is a tuple that contains three elements in Python. That would be three integers, one, two, and three.

01:28 Okay, so that’s maybe the definition, but it’s not super helpful to really understand what a tuple is. So I want to give you some help in remembering an approximation of how you can think about a tuple in Python.

01:43 It’s very helpful to think about database records in that context, or rows in a spreadsheet. You may have worked with some sort of a spreadsheet application before. You have some sort of idea of what a spreadsheet looks like, and that it consists of multiple columns and multiple rows.

01:59 Here in this example, you’d see an employees sheet that has three columns—employee ID, name, and role—and then it has three rows that each contain data.

02:09 You could think of a Python tuple as one row in this spreadsheet, or one record in the database, right? So the first row here, you could represent it as a Python tuple as shown at the bottom of this slide.

02:23 So here you would surround the information that is given in the row with parentheses and separate each of the values in each column with a comma. So the first one is the employee ID, which here has the value one.

02:38 So you would put in an integer, 1, separated with a comma from the next value. The next one is the name—in this case, Adisaand that would be a string in Python.

02:46 So you would write out the string "Adisa", put a comma again, and then you’re in the third column. That’s the role. And in this case, she’s a software engineer.

02:55 So you would put these words, "software engineer" also as a string in Python, as the third item in the tuple.

03:04 So while this isn’t a perfect explanation of what a tuple is, it’s a pretty good approximation if you think of a tuple as a read-only database record. So one of these rows, but read-only.

03:17 Why is that? A read-only database record is a finite sequence of values. It ends at some point. It has a fixed structure. You usually don’t change these columns around very much.

03:28 It has fixed values—that is, if it’s read-only. And it contains a variety of data types. And the one you saw before, you saw some text and you saw some numbers, and it also usually contains related data.

03:40 So each row, the data that is in the different fields is related to each other.

03:46 And if you keep that in mind, that a tuple is kind of like a read-only database record, you have a good chance of using them in the way that they’re intended in Python.

03:55 Again, if you walk away from this course and in the evening you remember that you’ve heard something about tuples, but you’re like, what was this thing again?

04:03 Toople? Topple? Then I want you just to think, ah, all right, it’s a read-only database record. And if that approximation gives you some sort of an idea, then I think you’ve got a better grasp on how to think about tuples in Python.

04:17 That is what a tuple is, and you’ll explore them in much more detail in the following lessons. In the next one, you’re going to learn how you can create a tuple in Python.

Become a Member to join the conversation.