1 00:00:00,000 --> 00:00:04,260 Okay, let's take a look at these things called tuples, 2 00:00:04,270 --> 00:00:05,550 or some people call them tuples. 3 00:00:05,560 --> 00:00:07,260 I actually don't know which is the right way, 4 00:00:07,270 --> 00:00:09,360 tuple, tuple. You can say it either way, I guess. 5 00:00:09,420 --> 00:00:15,270 So a tuple in Python is basically a list, but it cannot be 6 00:00:15,270 --> 00:00:18,860 changed. So a little while ago we saw a list is equal to 7 00:00:18,870 --> 00:00:25,980 '[1, 2, 3, 4]', and we could print the list, and then we could do 8 00:00:25,990 --> 00:00:29,540 'lst.append', and we could put 5 in there, 9 00:00:29,550 --> 00:00:33,080 and then we could print that list, and we see that we added something 10 00:00:33,140 --> 00:00:36,020 just like this, '[1, 2, 3, 4]', '[1, 2, 3, 4, 5]'. 11 00:00:36,180 --> 00:00:38,400 But a tuple does not work that way. 12 00:00:38,410 --> 00:00:41,370 A tuple, it cannot be modified. 13 00:00:41,620 --> 00:00:44,620 This is really good for performance increases, but it's also 14 00:00:44,650 --> 00:00:47,830 really, really good just for keeping your data the same. 15 00:00:48,060 --> 00:00:50,040 Some things just don't need to change. 16 00:00:50,050 --> 00:00:55,130 So for instance, let's say I have a tuple, and you open it, 17 00:00:55,130 --> 00:00:58,370 and close it with a regular set of parentheses here. 18 00:00:58,420 --> 00:01:04,250 And let's do '('Kalob', 'Henry', 'Zephyr')' 19 00:01:04,260 --> 00:01:06,019 And this is not going to be called 'tup'. 20 00:01:06,110 --> 00:01:08,570 This is going to be called a 'group'. 21 00:01:08,580 --> 00:01:10,040 So it's me and my two cats. 22 00:01:10,460 --> 00:01:15,550 Now, on some instances of Python I have noticed that Python 23 00:01:15,560 --> 00:01:17,710 will get this confused for a function. 24 00:01:17,720 --> 00:01:20,260 I don't know why that happens, but if that does ever happen 25 00:01:20,270 --> 00:01:22,240 to you, you can just throw a comma at the end here, 26 00:01:22,250 --> 00:01:25,670 and that comma with the closing parentheses, Python goes, 27 00:01:25,680 --> 00:01:26,870 "Oh, I know that's not a function. 28 00:01:26,880 --> 00:01:28,670 There's nothing there. That's got to be a tuple". 29 00:01:28,740 --> 00:01:32,310 So we have this 'group', and in this 'group', let's say we wanted to 30 00:01:32,320 --> 00:01:34,780 add a new person to this 'group'. 31 00:01:35,140 --> 00:01:38,540 Well, let's hit '.', and then tab, and we see that we don't 32 00:01:38,550 --> 00:01:39,560 have 'append' in there. 33 00:01:39,570 --> 00:01:42,020 We have 'count' and 'index', but we don't have 'append'. 34 00:01:42,020 --> 00:01:43,100 So let's go ahead and try this 35 00:01:43,100 --> 00:01:50,700 anyways, 'append', a new pet called "Axel", I guess. "AttributeError: 36 00:01:50,780 --> 00:01:52,510 object has no attribute 'append'." 37 00:01:52,520 --> 00:01:53,770 It simply does not exist. 38 00:01:53,780 --> 00:01:55,870 We cannot do anything with that. 39 00:01:56,100 --> 00:01:57,990 It does have 'count', which is pretty cool. 40 00:01:58,000 --> 00:02:00,800 So I could count the number of times "Kalob" shows up. 41 00:02:00,800 --> 00:02:01,800 It shows up once. 42 00:02:01,800 --> 00:02:07,200 I'll also show the number of times "Henry" shows up, which is also once. 43 00:02:07,260 --> 00:02:09,419 And we can actually change this as well. 44 00:02:09,430 --> 00:02:13,500 So let's put "Henry" in here just to show the 'count' example. 45 00:02:13,509 --> 00:02:15,120 We've got "Henry" in here twice. 46 00:02:15,440 --> 00:02:16,820 I just reran those cells. 47 00:02:16,820 --> 00:02:19,500 And so it's '('Kalob', 'Henry', 'Zephyr', 'Henry')' now. 48 00:02:19,580 --> 00:02:22,790 'group.append' still doesn't work. Down here, 49 00:02:22,970 --> 00:02:23,840 'group.count( 50 00:02:23,870 --> 00:02:25,950 "Henry")', now shows up as two. 51 00:02:25,960 --> 00:02:29,500 Now, one thing to keep in mind here, is with lists and tuples, they 52 00:02:29,510 --> 00:02:31,030 do not have to be unique items. 53 00:02:31,040 --> 00:02:33,760 We'll talk about sets a little bit later where every item 54 00:02:33,760 --> 00:02:34,700 is going to be unique. 55 00:02:34,700 --> 00:02:35,860 But that's not the case here. 56 00:02:35,900 --> 00:02:41,100 So effectively we are stuck with '(Kalob', 'Henry', 'Zephyr', 'Henry')'. 57 00:02:41,140 --> 00:02:43,480 And let's say we wanted to get rid of "Henry". 58 00:02:43,490 --> 00:02:44,860 How do we get rid of this? 59 00:02:46,000 --> 00:02:50,600 Well, with a list, we simply did the list name or the variable name, '.remove', 60 00:02:50,780 --> 00:02:53,590 and let's do "Henry". Oh, that doesn't work. 61 00:02:54,090 --> 00:02:57,820 Let's do another example here where instead of running 'remove', 62 00:02:57,880 --> 00:02:59,010 we run 'pop'. 63 00:02:59,130 --> 00:03:01,110 Oh, we can't even get rid of that. 64 00:03:01,120 --> 00:03:02,100 So what do we do? 65 00:03:02,110 --> 00:03:05,940 The only other option we have here is we can create a new 66 00:03:05,940 --> 00:03:09,940 tuple. So let's call this 'new_group', and we are going to 67 00:03:09,940 --> 00:03:12,300 say 'group', and we're going to slice this. 68 00:03:12,300 --> 00:03:14,000 We're going to start at number 0, 69 00:03:14,200 --> 00:03:15,200 this is an index. 70 00:03:16,500 --> 00:03:22,500 So 0-1-2, and we want to go up to number 3, but not include number 3. 71 00:03:22,500 --> 00:03:26,600 So 0-1-2-3 not including 3. 72 00:03:26,600 --> 00:03:29,700 [no audio] 73 00:03:29,720 --> 00:03:35,280 And let's do this, 'new_group', and we have removed it. 74 00:03:35,290 --> 00:03:36,960 But we've also created a new variable. 75 00:03:36,970 --> 00:03:39,810 So now we have two variables with basically the same data 76 00:03:39,820 --> 00:03:42,150 in it, and we're no longer being very efficient. 77 00:03:42,150 --> 00:03:47,600 Now, as an example of how a tuple is a sequence, we can use 78 00:03:47,600 --> 00:03:52,900 a for loop. So 'for item in new_group', because we don't want 79 00:03:52,900 --> 00:03:55,800 the old 'group' anymore, 'print(item)'. 80 00:03:56,120 --> 00:03:59,300 Now, that 'item' is going to be "Kalob", "Henry", and "Zephyr", in that 81 00:03:59,310 --> 00:04:02,630 order, and a tuple will always maintain that order. 82 00:04:02,840 --> 00:04:05,570 Other data types do not necessarily do that. 83 00:04:05,580 --> 00:04:08,720 Lists do, strings do, and tuples do. 84 00:04:08,730 --> 00:04:09,980 They will maintain their order. 85 00:04:09,990 --> 00:04:13,430 Sets do not, and dictionaries are not always guaranteed to 86 00:04:13,430 --> 00:04:16,600 maintain their order, depending on the version of Python you're using. 87 00:04:16,600 --> 00:04:19,100 But when it comes to a data type like a tuple, once it is 88 00:04:19,100 --> 00:04:21,800 set, it is set in stone, and the only way to get rid of it 89 00:04:21,850 --> 00:04:25,450 is to delete it. So we could do 'del group'. 90 00:04:26,079 --> 00:04:28,149 And if we do 'group', we're going to see an error. 91 00:04:28,260 --> 00:04:31,500 'group is not defined' anymore, because we deleted it. 92 00:04:31,510 --> 00:04:32,640 So that is a tuple. 93 00:04:32,640 --> 00:04:36,000 A tuple is a lot like a list, but it cannot be changed. 94 00:04:36,060 --> 00:04:37,990 It is completely immutable. 95 00:04:38,000 --> 00:04:39,010 It's that word here. 96 00:04:39,020 --> 00:04:42,220 I did not mean for that to execute, but it is completely 97 00:04:42,220 --> 00:04:45,430 immutable, which means once you set your data in here, you 98 00:04:45,430 --> 00:04:47,600 set those items, they are set in stone. 99 00:04:47,980 --> 00:04:51,460 There are ways around that by creating a new variable and 100 00:04:51,460 --> 00:04:52,400 deleting the old one, 101 00:04:52,400 --> 00:04:54,800 but at that point, you're still not being very efficient at all. 102 00:04:54,800 --> 00:04:57,100 And it sort of defeats the purpose of using a tuple. 103 00:04:57,110 --> 00:04:59,560 If you need to actually modify your tuple at all, you're 104 00:04:59,570 --> 00:05:01,120 probably better off using a list. 105 00:05:01,420 --> 00:05:06,100 But in Python, tuples have their place, and it is good to 106 00:05:06,110 --> 00:05:08,110 know how they look and how they act. 107 00:05:08,420 --> 00:05:11,660 And the fact that you can't really do too much with a tuple 108 00:05:11,840 --> 00:05:13,190 is actually pretty nice 109 00:05:13,200 --> 00:05:15,110 sometimes. Sometimes you want that data integrity. 110 00:05:15,300 --> 00:05:16,680 Go ahead and give this a shot. 111 00:05:16,690 --> 00:05:17,760 Create a tuple. 112 00:05:18,100 --> 00:05:20,900 Where do we have a tuple? Tuple-Tuple-Tuple. 113 00:05:20,900 --> 00:05:23,400 'group = ('Kalob', 'Henry', 'Zephyr', 'Henry')'. 114 00:05:23,420 --> 00:05:26,150 Create your own tuple, and then just print that out, 115 00:05:26,160 --> 00:05:29,210 and then what I would like you to do is try to run '.append' 116 00:05:29,900 --> 00:05:35,400 as well as '.remove', and you should see an 'AttributeError' for both of those. 117 00:05:35,500 --> 00:05:39,200 If you see those errors, you're doing the right thing. That's okay. 118 00:05:39,250 --> 00:05:42,370 Errors are okay to have. Once you see those 'AttributeError', 119 00:05:42,380 --> 00:05:44,620 let's head on over to that next lesson where we learn about 120 00:05:44,740 --> 00:05:47,650 sets, I believe is the next one where it's a lot like a list 121 00:05:47,650 --> 00:05:51,200 and a tuple, but it's totally unique and doesn't always maintain its 122 00:05:51,200 --> 00:05:53,100 order. So have fun with this. 123 00:05:53,100 --> 00:05:56,000 And let's head on over to that next lesson whenever you're ready.