1 00:00:00,510 --> 00:00:07,710 Let's discuss tuples now, tuples are a very swift like concept, and they make our lives extremely 2 00:00:07,710 --> 00:00:08,130 easy. 3 00:00:08,610 --> 00:00:10,800 It took me a long time to come round to tuples. 4 00:00:10,800 --> 00:00:15,510 I didn't even know they existed until I started digging, particularly for courses like this, and went, 5 00:00:15,510 --> 00:00:16,650 oh, that's handy. 6 00:00:17,310 --> 00:00:18,870 So let's say you've got two variables. 7 00:00:19,110 --> 00:00:21,660 One is an error code, which is faurot for an integer. 8 00:00:21,900 --> 00:00:24,630 One is an error message, which is a string not found. 9 00:00:25,530 --> 00:00:32,340 Now, if you wanted to tell the user in your app that that resource wasn't found, you would do not 10 00:00:32,340 --> 00:00:34,020 a print, but something else similar. 11 00:00:34,530 --> 00:00:37,620 You'd say string error code. 12 00:00:39,850 --> 00:00:41,350 And then you'd say, plus. 13 00:00:44,270 --> 00:00:48,200 Espace, plus the error message. 14 00:00:49,010 --> 00:00:55,170 OK, and if we run that, we're going to get the output, you think we're going to get 404 not found. 15 00:00:55,970 --> 00:01:03,620 Now, the problem is that we have two variables that describe one situation, that it would be really 16 00:01:03,620 --> 00:01:10,210 handy if those variables were mashed together, if you like, into a single variable. 17 00:01:10,970 --> 00:01:12,250 That's what tuples are for. 18 00:01:12,830 --> 00:01:15,440 So we can say let our error. 19 00:01:16,940 --> 00:01:21,380 Equal to open and close brackets, these are just the regular brackets. 20 00:01:23,000 --> 00:01:25,790 404 and. 21 00:01:27,700 --> 00:01:28,210 Not. 22 00:01:29,830 --> 00:01:30,400 Found. 23 00:01:31,810 --> 00:01:37,570 And because we've put everything in brackets, our system knows that this is a tuple. 24 00:01:37,850 --> 00:01:40,630 So how do you access the tuple part of this? 25 00:01:41,020 --> 00:01:46,270 Well, we're going to have the same old print statement where we turn something from an integer into 26 00:01:46,270 --> 00:01:46,770 a string. 27 00:01:47,140 --> 00:01:52,960 We reference the tuple and then we put it point zero and you'll notice that's what it suggests for us, 28 00:01:53,150 --> 00:01:53,800 point zero. 29 00:01:54,670 --> 00:02:01,040 And then we're going to add the space and then add the error. 30 00:02:01,050 --> 00:02:01,930 Dot one. 31 00:02:02,870 --> 00:02:08,000 So, of course, if we print this, we will get the same error, print it out, 404 not found, except 32 00:02:08,240 --> 00:02:11,810 now we have two lines of code instead of three. 33 00:02:13,390 --> 00:02:17,830 OK, so let's look at the notes here, what else have I got for you, right? 34 00:02:18,220 --> 00:02:21,010 Oh, an advanced code now. 35 00:02:22,640 --> 00:02:27,680 The advanced error code might look a little something like this, you know what, I'm just going to 36 00:02:27,680 --> 00:02:29,860 copy this because you don't want to see me typed. 37 00:02:31,830 --> 00:02:39,810 Let's go advanced code would look like this, so we have a 404 and then inside the tuple we have another 38 00:02:39,810 --> 00:02:40,230 tuple. 39 00:02:41,200 --> 00:02:48,040 And so that might contain a specific error code for the not found and then a specific error message 40 00:02:48,340 --> 00:02:50,080 and to access these things. 41 00:02:51,090 --> 00:02:58,200 We use the following syntax, 021, you know how to access zero, but if we go to one and we put another 42 00:02:58,200 --> 00:03:01,230 dot, it allows us to access zero and one. 43 00:03:02,070 --> 00:03:11,520 Now, if you were using a tuple to decipher XML or JSON that was coming back from your Web server, 44 00:03:12,450 --> 00:03:20,100 I would say it wouldn't be the best way of deciphering your error codes and things because it's better 45 00:03:20,100 --> 00:03:24,720 to use a model that's just the way that we do things. 46 00:03:24,720 --> 00:03:27,120 Encoding use models, not tuples. 47 00:03:27,240 --> 00:03:29,770 But there are other places where tuples will come in handy. 48 00:03:29,790 --> 00:03:36,180 So what I'm saying is don't use this for a 404 not found, but it does make a good example to teach 49 00:03:36,180 --> 00:03:36,560 you guys. 50 00:03:37,440 --> 00:03:39,320 OK, so what else have we got here? 51 00:03:39,330 --> 00:03:41,960 We can print that out, of course, but we're not going to do that. 52 00:03:42,360 --> 00:03:44,950 Let's get equal, are there we go. 53 00:03:45,510 --> 00:03:48,380 So let's copy this and drop it into here. 54 00:03:49,080 --> 00:03:50,830 Let's start equal the following. 55 00:03:51,210 --> 00:03:56,930 So when you define a tuple, you can actually give names to the individual elements. 56 00:03:57,150 --> 00:04:05,220 So if I was going to access this stat dot now you don't have indexes as in zero and one, you have the 57 00:04:05,220 --> 00:04:06,040 actual names. 58 00:04:06,060 --> 00:04:11,660 So Stackpole would come out, of course, as 404 so you can name them. 59 00:04:11,820 --> 00:04:14,700 And in practice, it's actually quite a good idea to name them. 60 00:04:15,000 --> 00:04:20,610 But by the point you've gotten round to naming your tuples, you may as well have created a data class 61 00:04:20,610 --> 00:04:22,080 or a model class of some sort. 62 00:04:22,650 --> 00:04:24,570 If you don't know what those words mean yet, don't worry. 63 00:04:24,690 --> 00:04:28,590 You will write a little bit of homework, a little task for you. 64 00:04:30,190 --> 00:04:38,110 The following, please create a nested name tuple that holds your age first and last name and then print 65 00:04:38,110 --> 00:04:39,550 out your full name and age. 66 00:04:40,450 --> 00:04:47,110 So a nested tuple is another word for this, where we put a tuple inside a tuple, it's nesting inside 67 00:04:47,110 --> 00:04:47,260 it. 68 00:04:47,830 --> 00:04:49,420 So you've got your homework. 69 00:04:49,750 --> 00:04:50,260 Good luck.