Locked lesson.
About this lesson
Tuples are just like lists, but they can't be changed once created. Let's discuss how to create and reference them.
Exercise files
Download this lesson’s related exercise files.
9 - Python Tuples.docx57.1 KB 9 - Python Tuples SOLUTION.docx
55.4 KB
Quick reference
Python Tuples
Tuples are just like Lists, but they can't be changed once created.
When to use
Use these when you want to deal with data that can't be changed.
Instructions
To create a tuple, just name it:
names = ("John", "Mary", "Tim")
To access an item in the Tuple, reference its item number.
To access John:
names[0]
Hints & tips
- names = ("John", "Mary", "Tim")
- names[0]
- Tuples can't be changed once they are created.
- 00:05 Okay, in this video I want to talk about tuples.
- 00:07 And tuples are basically the same thing as lists.
- 00:09 They're nearly identical.
- 00:11 The only real difference is a tuple can't be changed.
- 00:14 Once a tuple is created, you can't add to it, you can't remove,
- 00:17 you can't change it in any way.
- 00:19 Except you sort of can, we'll talk about that as we go on here.
- 00:22 So we haven't talked about it yet, but in the future,
- 00:25 we're going to be connecting our Python programs to our Excel spreadsheets
- 00:29 using something called openpyxl.
- 00:31 And sometimes openpyxl returns things as tuples,sometimes it returns them as lists.
- 00:36 So we need to be familiar with what a tuple is at least, and
- 00:38 that's what we're going to talk about in this video.
- 00:41 So let's create a tuple, but first, let me create a list.
- 00:44 And I'm going to create a list called names.
- 00:47 And just like we learned in the last video, we can go 'John', and
- 00:51 'Mary', let's go T'im'.
- 00:54 So we've got this list called names.
- 00:56 And if we want to print this out, we can print names.
- 00:59 And we learned how to do this already in the last video.
- 01:01 So the difference between a list and a tuple is these brackets, so
- 01:05 a list has square brackets.
- 01:09 A tuple has parentheses, and really that's the only difference.
- 01:12 So now this is a tuple, right?
- 01:15 So it's sort of weird, but this is really the only difference.
- 01:19 And we can print it out just like we did with a list.
- 01:22 So if we go ahead and save this, and head back over to our terminal.
- 01:27 And we can run this guy.
- 01:28 We get John, Mary, Tim, and you notice the parentheses around it.
- 01:33 So even the Python interpreter knows that this is a tuple.
- 01:36 If we were to come back here and change this back to a list,
- 01:41 and then save this and run it.
- 01:47 I can see now the square brackets are outputs.
- 01:50 So just remember parentheses means tuple, square brackets means list.
- 01:55 So, okay, let's change this back to a tuple now.
- 02:00 And how do we access the things inside of our tuple?
- 02:03 Just the exact same way as we did with a list.
- 02:06 And again, these are numbered just like a list.
- 02:08 So the first item is zero.
- 02:10 Second item is one.
- 02:11 The third item is two.
- 02:12 So zero, one, two.
- 02:14 And we access them in the exact same way with these square brackets.
- 02:18 Now, this is kind of weird, right?
- 02:19 You would think that we would do something like this, since tuples have parentheses.
- 02:24 But no, we still use the square brackets.
- 02:28 Which is kind of weird, but that's how this goes.
- 02:30 So go ahead and save this and let's run this.
- 02:34 And it prints out John.
- 02:36 I mentioned earlier that the main difference between a tuple and
- 02:40 a list besides the parentheses, are that you can't change a tuple.
- 02:43 Tuples are immutable.
- 02:45 They can't be added to or you can't remove things from them.
- 02:48 Except you sort of can if you hack around a little bit, so we can,
- 02:51 let's rename this to names_1.
- 02:53 Let's create another tuple.
- 02:54 And we'll call it names_2.
- 02:56 And inside of here, let's add Bob.
- 03:00 And one weird thing about a tuple is when you only have one item,
- 03:03 you still have to put a comma as if there were other items.
- 03:07 So we've got names_1 and names_2.
- 03:10 Now we can create a third tuple, let's call it names_3.
- 03:15 And set that equal to names_1 + names_2.
- 03:20 So we can add the first tuple to the second tuple.
- 03:23 And it's not actually adding them it's putting them together, right?
- 03:27 And so then our third one becomes John, Mary, Tim and Bob.
- 03:31 So if we were then to come down here and print out names_3,
- 03:37 if we go ahead and save this and run it.
- 03:40 We see John, Mary, Tim, and Bob.
- 03:43 So technically, did we change this first tuple?
- 03:47 No, but if we wanted to add a name to this tuple, we could just do it this way.
- 03:53 And then now we have a new table called names_3.
- 03:55 If we then wanted to rename names_3 names_1, we could and then hey,
- 03:59 we've changed our first tuple.
- 04:02 I mean, technically we haven't.
- 04:03 But for all intents and purposes we have.
- 04:05 So there's always ways with coding to hack around things and
- 04:08 get around rules if you need to.
- 04:10 So I'm not sure we're actually ever going to use this in this course, but
- 04:12 it's kind of a fun thing to learn.
- 04:14 And something interesting about tuple.
- 04:16 So those are tuples.
- 04:18 Like I said, we're going to be getting data from our Excel spreadsheets in
- 04:21 the form of lists and tuples.
- 04:23 So it's a good idea that we just understand at least what they are.
- 04:26 And I think we've done that in the last couple of videos.
- 04:28 We really don't need in depth knowledge of these things.
- 04:32 We just need a basic understanding of them.
- 04:33 And I think we've got that now.
- 04:34 So in the next video, we'll look at for loops.
- 04:37 And then after that, we'll start to look at openpyxl.
Lesson notes are only available for subscribers.