1 00:00:00,900 --> 00:00:02,600 - [Instructor] Now that we've talked about lists 2 00:00:02,600 --> 00:00:06,340 in more detail, let's take another look at tuples. 3 00:00:06,340 --> 00:00:08,770 So for that purpose, we're going to go ahead 4 00:00:08,770 --> 00:00:10,739 and use an interactive iPython section. 5 00:00:10,739 --> 00:00:12,963 And first let's talk about creating tuples 6 00:00:12,963 --> 00:00:15,320 a little bit more. 7 00:00:15,320 --> 00:00:17,924 Now previously we demonstrated that you can 8 00:00:17,924 --> 00:00:22,880 create a tuple using a comma separated list 9 00:00:22,880 --> 00:00:24,750 of items in parenthesis. 10 00:00:24,750 --> 00:00:26,254 It turns out the parenthesis are not required 11 00:00:26,254 --> 00:00:29,568 with a couple of exceptions, 12 00:00:29,568 --> 00:00:34,260 one of which is when you create an empty tuple. 13 00:00:34,260 --> 00:00:36,810 So if you want a tuple of no elements at any 14 00:00:36,810 --> 00:00:41,420 given time, then you simply use an empty set of parenthesis, 15 00:00:41,420 --> 00:00:44,120 and when you evaluate any tuple, 16 00:00:44,120 --> 00:00:46,570 it's going to display that tuple's contents 17 00:00:46,570 --> 00:00:49,790 in parenthesis, so as you can see here we get an 18 00:00:49,790 --> 00:00:52,970 empty set of parenthesis for the empty tuple. 19 00:00:52,970 --> 00:00:56,500 And we can also confirm that it has no elements. 20 00:00:56,500 --> 00:01:00,101 All we have to do is use the len function to do that, 21 00:01:00,101 --> 00:01:01,785 and we can see indeed that the number 22 00:01:01,785 --> 00:01:04,610 of elements is zero. 23 00:01:04,610 --> 00:01:07,534 Now let's take that student tuple variable 24 00:01:07,534 --> 00:01:11,606 and let's assign it a completely new tuple. 25 00:01:11,606 --> 00:01:13,680 Actually you know what, let's do it without parenthesis 26 00:01:13,680 --> 00:01:16,405 this time just so you can see that the parenthesis 27 00:01:16,405 --> 00:01:17,973 are not required. 28 00:01:17,973 --> 00:01:21,050 So let's say we have somebody named John Green, 29 00:01:21,050 --> 00:01:25,010 and we'll assume since this is a student 30 00:01:25,010 --> 00:01:27,431 that we're going to have a grade point average 31 00:01:27,431 --> 00:01:30,340 associated with that student as well. 32 00:01:30,340 --> 00:01:33,530 What makes this a tuple is not parenthesis, 33 00:01:33,530 --> 00:01:35,810 in fact we have no parenthesis here, 34 00:01:35,810 --> 00:01:40,230 it's the commas that separate the individual values. 35 00:01:40,230 --> 00:01:42,803 Tuples are commonly use to group together 36 00:01:42,803 --> 00:01:44,803 related pieces of data, 37 00:01:46,053 --> 00:01:46,953 and in the case of a student, 38 00:01:48,680 --> 00:01:49,683 this might be the first name, last name, 39 00:01:49,683 --> 00:01:50,829 and grade point average of a given student. 40 00:01:50,829 --> 00:01:53,199 Now once we have that tuple, 41 00:01:53,199 --> 00:01:58,199 we can go ahead and evaluate it and see the contents, 42 00:01:58,410 --> 00:02:02,370 but notice when Python outputs the tuple, 43 00:02:02,370 --> 00:02:05,630 it always uses those parenthesis even though 44 00:02:05,630 --> 00:02:10,630 they're not required to create the tuple in the first place. 45 00:02:12,730 --> 00:02:17,200 Let me go ahead and recall the lens snippet from up above, 46 00:02:17,200 --> 00:02:20,200 and now we have this student tuple variable 47 00:02:20,200 --> 00:02:23,646 referring to a completely new separate tuple object. 48 00:02:23,646 --> 00:02:26,070 I can't modify the existing one, 49 00:02:26,070 --> 00:02:27,967 they're immutable, so anytime you assign 50 00:02:27,967 --> 00:02:32,540 to a variable that previously was referring to a tuple, 51 00:02:32,540 --> 00:02:34,666 another tuple, you're just replacing the one that was there, 52 00:02:34,666 --> 00:02:37,900 aiming it at a new object. 53 00:02:37,900 --> 00:02:39,935 But now we can see that based on the updated object, 54 00:02:39,935 --> 00:02:44,935 we have a length of three for that particular tuple. 55 00:02:45,810 --> 00:02:50,090 Now the parenthesis are not required, but we do 56 00:02:50,090 --> 00:02:51,830 recommend using them. 57 00:02:51,830 --> 00:02:55,470 So let's say we want to create another student tuple 58 00:02:56,533 --> 00:03:00,060 and for this other student, 59 00:03:00,060 --> 00:03:02,970 let's put the names and everything in parenthesis. 60 00:03:02,970 --> 00:03:05,117 So let's do something like Mary and Red, 61 00:03:05,117 --> 00:03:09,930 and we'll give the same grade point average for now. 62 00:03:09,930 --> 00:03:13,780 So here we have a completely new tuple that we just 63 00:03:13,780 --> 00:03:16,860 created, this time we used parenthesis, 64 00:03:16,860 --> 00:03:19,530 and of course if we then go and evaluate that, 65 00:03:19,530 --> 00:03:21,183 you still see those parenthesis 66 00:03:21,183 --> 00:03:24,360 in the string representation. 67 00:03:24,360 --> 00:03:27,298 Now, sometimes you'll create tuples that have only 68 00:03:27,298 --> 00:03:30,370 one element, and you may be tempted to simply 69 00:03:30,370 --> 00:03:35,370 provide an individual value to represent that tuple. 70 00:03:40,031 --> 00:03:43,350 Let's create a variable, we'll call it a singleton tuple, 71 00:03:43,350 --> 00:03:46,680 and let's say we wanted to just put the string red 72 00:03:46,680 --> 00:03:47,513 in there. 73 00:03:49,377 --> 00:03:50,377 If I complete this assignment, 74 00:03:50,377 --> 00:03:52,540 what I have is a variable referring to a string. 75 00:03:52,540 --> 00:03:54,070 So you might say well wait, all right, 76 00:03:54,070 --> 00:03:55,272 let's put it in parenthesis. 77 00:03:55,272 --> 00:03:58,570 Well what I'd have in this case is still 78 00:03:58,570 --> 00:04:00,531 a variable referring to a string because 79 00:04:00,531 --> 00:04:04,599 the parenthesis are not what defines a tuple. 80 00:04:04,599 --> 00:04:07,520 Commas are what defines a tuple. 81 00:04:07,520 --> 00:04:09,260 So if you really want to create a tuple 82 00:04:09,260 --> 00:04:10,838 with only one element in it, 83 00:04:10,838 --> 00:04:14,444 you're going to put that element followed by a comma. 84 00:04:14,444 --> 00:04:17,119 And because that's kind of a strange notation 85 00:04:17,119 --> 00:04:19,800 to most programmers new to python, 86 00:04:19,800 --> 00:04:22,838 I recommend also putting that in parenthesis as well 87 00:04:22,838 --> 00:04:25,650 to further emphasize the fact that you're creating 88 00:04:25,650 --> 00:04:29,410 a tuple and group those items together. 89 00:04:29,410 --> 00:04:31,906 So the way I can tell that this is a tuple 90 00:04:31,906 --> 00:04:36,294 versus just an individual string is if the parenthesis 91 00:04:36,294 --> 00:04:39,050 get displayed around the contents. 92 00:04:39,050 --> 00:04:42,620 So if I now go and evaluate that tuple, 93 00:04:42,620 --> 00:04:44,480 we can see that indeed the parenthesis 94 00:04:44,480 --> 00:04:46,349 are displayed and not only that, 95 00:04:46,349 --> 00:04:48,373 in the case of a singleton tuple, 96 00:04:48,373 --> 00:04:51,488 it also displays the trailing comma 97 00:04:51,488 --> 00:04:54,963 to indicate that indeed, it is a tuple. 98 00:04:56,500 --> 00:04:59,930 So now that we've shown how to create a few tuples, 99 00:04:59,930 --> 00:05:03,252 let's now start talking about accessing tuple elements. 100 00:05:03,252 --> 00:05:07,182 Now turns out that the square brackets we used 101 00:05:07,182 --> 00:05:10,970 to access the elements of a list can also be used 102 00:05:10,970 --> 00:05:14,910 to access the elements of any sequenced data structure. 103 00:05:14,910 --> 00:05:16,387 So that could be a string, 104 00:05:16,387 --> 00:05:20,585 and in fact we demonstrated earlier that you can use 105 00:05:20,585 --> 00:05:23,810 the square brackets to access an individual character 106 00:05:23,810 --> 00:05:26,900 in the string, and it can be a tuple as well, 107 00:05:26,900 --> 00:05:29,730 and that's what we'd like to take a look at here. 108 00:05:29,730 --> 00:05:31,419 Now to save me a little bit of typing time, 109 00:05:31,419 --> 00:05:35,010 let me go ahead and copy and paste here a time tuple, 110 00:05:35,010 --> 00:05:38,290 and let's assume these represent hours, minutes, 111 00:05:38,290 --> 00:05:41,413 and seconds, and what we would like to do first 112 00:05:41,413 --> 00:05:44,873 is just display that so we can see the contents, 113 00:05:44,873 --> 00:05:46,799 and make sure that it was initialized correctly. 114 00:05:46,799 --> 00:05:50,750 And let's assume we'd like to use each of those 115 00:05:50,750 --> 00:05:53,200 three values in a calculation. 116 00:05:53,200 --> 00:05:55,910 So let's say we want to calculate the total number 117 00:05:55,910 --> 00:05:58,680 of seconds based on the current time. 118 00:05:58,680 --> 00:06:01,000 So here we have an expression that will do that, 119 00:06:01,000 --> 00:06:03,514 where we use time tuple sub zero to access 120 00:06:03,514 --> 00:06:08,240 the element at index number zero within the tuple. 121 00:06:08,240 --> 00:06:10,070 We're going to assume that's the hours 122 00:06:10,070 --> 00:06:13,040 and multiply that by 3600 for the total number 123 00:06:13,040 --> 00:06:14,362 of seconds in a hour. 124 00:06:14,362 --> 00:06:16,382 Then we're going to add to that the result of the next 125 00:06:16,382 --> 00:06:19,815 expression which uses time tuple sub one to get 126 00:06:19,815 --> 00:06:22,208 the next element in the tuple, 127 00:06:22,208 --> 00:06:25,034 and then finally we'll add to that the seconds 128 00:06:25,034 --> 00:06:27,990 which is the last element in the tuple, 129 00:06:27,990 --> 00:06:30,480 the element index number two. 130 00:06:30,480 --> 00:06:33,088 Now when we evaluate that, we can see the total 131 00:06:33,088 --> 00:06:35,100 of all of those elements. 132 00:06:35,100 --> 00:06:39,140 So you can always use the elements within a tuple 133 00:06:39,140 --> 00:06:41,634 by the tuple name, a set of square brackets, 134 00:06:41,634 --> 00:06:44,023 and an index number. 135 00:06:45,630 --> 00:06:47,814 In the next section, we'll actually show another way 136 00:06:47,814 --> 00:06:50,830 to access the elements of a tuple. 137 00:06:50,830 --> 00:06:53,689 Again, we'll revisit the concept called unpacking 138 00:06:53,689 --> 00:06:58,430 which we demonstrated briefly in the functions lesson. 139 00:06:58,430 --> 00:07:01,535 So now let's talk a little bit about operations 140 00:07:01,535 --> 00:07:06,270 that appear to modify tuples. 141 00:07:06,270 --> 00:07:09,540 So once again, let me go ahead and do some copying 142 00:07:09,540 --> 00:07:11,991 and pasting here to save some typing time. 143 00:07:11,991 --> 00:07:16,170 Tuple one is going to be a tuple containing three integers, 144 00:07:16,170 --> 00:07:17,356 10, 20, and 30. 145 00:07:17,356 --> 00:07:19,970 And let's go and create a second variable, 146 00:07:19,970 --> 00:07:23,040 tuple two, and we'll assign it tuple one. 147 00:07:23,040 --> 00:07:27,382 So first of all, this is a reference assignment. 148 00:07:27,382 --> 00:07:31,350 The variable tuple one is not the object containing 149 00:07:31,350 --> 00:07:35,771 10, 20, and 30, it is a reference to that object. 150 00:07:35,771 --> 00:07:37,797 So what I'm doing here is taking that reference 151 00:07:37,797 --> 00:07:41,700 and placing it into tuple two. 152 00:07:41,700 --> 00:07:45,150 As a result of that snippet that I'm now executing, 153 00:07:45,150 --> 00:07:49,370 both of these variables refer to the same tuple object 154 00:07:49,370 --> 00:07:53,737 in memory, and recall that you can actually check that 155 00:07:53,737 --> 00:07:58,737 with the ID function that's built into Python. 156 00:07:58,890 --> 00:08:01,218 Now of course if I evaluate tuple two, 157 00:08:01,218 --> 00:08:05,273 I can see that it appears to have the same contents 158 00:08:05,273 --> 00:08:08,870 as what we assigned to tuple one up above, 159 00:08:08,870 --> 00:08:11,220 and that's again because they are in fact 160 00:08:11,220 --> 00:08:12,167 the same object. 161 00:08:12,167 --> 00:08:14,970 So what about an expression like this. 162 00:08:14,970 --> 00:08:18,740 Let's say we want to do tuple one plus equals, 163 00:08:18,740 --> 00:08:23,300 and we want to add to the end of that tuple, 40 and 50. 164 00:08:23,300 --> 00:08:26,955 Now tuples are immutable, so when you see an expression 165 00:08:26,955 --> 00:08:29,363 like this with an immutable object referred 166 00:08:29,363 --> 00:08:31,806 to on the left hand side, 167 00:08:31,806 --> 00:08:34,782 what's really going to happen here is Python is going 168 00:08:34,782 --> 00:08:39,607 to create an entirely new tuple object containing 169 00:08:39,607 --> 00:08:44,560 all of the values from whatever object tuple one 170 00:08:44,560 --> 00:08:48,350 refers to and these new values in the tuple on 171 00:08:48,350 --> 00:08:50,797 the right hand side of the plus equals operator. 172 00:08:50,797 --> 00:08:52,568 Then we'll have that new objects reference 173 00:08:52,568 --> 00:08:55,300 get assigned back to tuple one, 174 00:08:55,300 --> 00:08:59,241 replacing the original object's reference. 175 00:08:59,241 --> 00:09:02,700 So tuple one currently refers to 10, 20, and 30, 176 00:09:02,700 --> 00:09:06,369 but it's going to be reaimed at a different object 177 00:09:06,369 --> 00:09:10,040 as a result of executing that statement. 178 00:09:10,040 --> 00:09:11,802 Now if you simply evaluate tuple one, 179 00:09:11,802 --> 00:09:13,820 you can see that all the elements are there, 180 00:09:13,820 --> 00:09:16,193 but you really can't tell that it's a separate object 181 00:09:16,193 --> 00:09:19,250 unless you start playing with the ID function 182 00:09:19,250 --> 00:09:22,683 once again, to check what the ID's of the objects are 183 00:09:22,683 --> 00:09:26,070 before and after the operation. 184 00:09:26,070 --> 00:09:29,440 And I'll leave that up to you to try out as an exercise. 185 00:09:29,440 --> 00:09:31,443 It's one of the great things about the interactive 186 00:09:31,443 --> 00:09:34,417 iPython environment, that you can just fiddle around 187 00:09:34,417 --> 00:09:37,270 like that and learn more. 188 00:09:37,270 --> 00:09:41,960 Now did this modification modify tuple two? 189 00:09:41,960 --> 00:09:43,039 Well, let's check that. 190 00:09:43,039 --> 00:09:45,381 So we can evaluate tuple two and see that it's still 191 00:09:45,381 --> 00:09:50,180 aimed at the original tuple object. 192 00:09:50,180 --> 00:09:52,908 Now if the left hand side is a mutable object 193 00:09:52,908 --> 00:09:57,289 like a list, then the plus equals operation 194 00:09:57,289 --> 00:09:59,110 works a little bit different. 195 00:09:59,110 --> 00:10:01,913 So let's say I have numbers as a list 196 00:10:01,913 --> 00:10:04,375 and we'll put some values in there. 197 00:10:04,375 --> 00:10:06,480 So there's our list numbers, 198 00:10:06,480 --> 00:10:08,222 and let's say we want to modify numbers 199 00:10:08,222 --> 00:10:13,222 by adding to the end of it with the plus equals 200 00:10:13,970 --> 00:10:17,620 operator, a tuple of additional elements. 201 00:10:17,620 --> 00:10:19,889 Let's say we want to add on six and seven. 202 00:10:19,889 --> 00:10:23,389 So again, we saw this with lists in the proceeding video. 203 00:10:23,389 --> 00:10:26,198 When the left hand side is a list, 204 00:10:26,198 --> 00:10:29,283 the right hand side has to be some sort of iterable 205 00:10:29,283 --> 00:10:33,178 object, some sort of sequence typically, 206 00:10:33,178 --> 00:10:36,600 and that could be another list, 207 00:10:36,600 --> 00:10:38,395 it could be a tuple, it could be a string, 208 00:10:38,395 --> 00:10:41,199 it can be anything that is capable of being 209 00:10:41,199 --> 00:10:44,860 iterated over one element at a time. 210 00:10:44,860 --> 00:10:48,167 And each individual element will be appended to the end 211 00:10:48,167 --> 00:10:50,400 of the list up above. 212 00:10:50,400 --> 00:10:52,171 So we can go ahead and execute that, 213 00:10:52,171 --> 00:10:55,282 and evaluate numbers to see that indeed, 214 00:10:55,282 --> 00:11:00,193 six and seven were appended to the end of that list. 215 00:11:01,360 --> 00:11:03,100 Now the last thing I'd like to talk about 216 00:11:03,100 --> 00:11:05,623 in this video is the fact that tuples, 217 00:11:05,623 --> 00:11:08,358 though they are immutable themselves, 218 00:11:08,358 --> 00:11:12,371 are capable of storing mutable objects. 219 00:11:12,371 --> 00:11:13,925 So I'm gonna go ahead and copy and paste 220 00:11:13,925 --> 00:11:17,714 a tuple in here, called student tuple. 221 00:11:17,714 --> 00:11:20,439 This particular tuple has three elements. 222 00:11:20,439 --> 00:11:23,621 Amanda and blue are strings representing 223 00:11:23,621 --> 00:11:25,256 a first name and a last name, 224 00:11:25,256 --> 00:11:28,337 and this last element is a list object 225 00:11:28,337 --> 00:11:33,040 containing three grades that of course, 226 00:11:33,040 --> 00:11:35,350 probably belong to Amanda Blue. 227 00:11:35,350 --> 00:11:38,770 So we've now created this student tuple object, 228 00:11:38,770 --> 00:11:43,770 and we can access the elements within the student tuple 229 00:11:44,112 --> 00:11:49,112 using the square brackets operators. 230 00:11:49,503 --> 00:11:52,661 Now if I use a square bracket 231 00:11:52,661 --> 00:11:55,950 and I say student tuple sub two, 232 00:11:55,950 --> 00:11:59,150 well Amanda is element zero. 233 00:11:59,150 --> 00:12:02,790 Blue is element one, and this last element, 234 00:12:02,790 --> 00:12:04,840 element two, is a list object. 235 00:12:04,840 --> 00:12:06,280 Well if it's a list object, 236 00:12:06,280 --> 00:12:08,210 I can use a second set of square brackets in this 237 00:12:08,210 --> 00:12:12,181 and I can go ahead and say let's say this grade was wrong, 238 00:12:12,181 --> 00:12:14,166 75, so I want to fix it. 239 00:12:14,166 --> 00:12:17,819 I can make it 85, so this is going to go to the 240 00:12:17,819 --> 00:12:19,575 third element of the tuple, 241 00:12:19,575 --> 00:12:21,358 which happens to be a list, 242 00:12:21,358 --> 00:12:24,453 then it's going to access element number one 243 00:12:24,453 --> 00:12:27,760 within that list which is the second element here, 244 00:12:27,760 --> 00:12:31,180 and we're going to replace that value with 85. 245 00:12:31,180 --> 00:12:34,710 So the list operation of modifying the contents 246 00:12:34,710 --> 00:12:38,550 of one element in that list seems to have worked 247 00:12:38,550 --> 00:12:40,670 because we didn't get an error message here. 248 00:12:40,670 --> 00:12:43,650 So to prove that, we can go ahead and evaluate 249 00:12:43,650 --> 00:12:45,520 student tuple and see that indeed, 250 00:12:45,520 --> 00:12:48,532 we were able to modify that object because 251 00:12:48,532 --> 00:12:51,046 the object that this element refers to 252 00:12:51,046 --> 00:12:55,623 is a mutable, modifiable object.