WEBVTT 1 00:00:01.550 --> 00:00:04.550 So, now it's time to talk about tuples. 2 00:00:04.550 --> 00:00:08.590 And what tuples are is a term for an ordered set of data. 3 00:00:08.590 --> 00:00:12.680 Now, in Python, tuples are similar to lists which we've already talked about. 4 00:00:12.680 --> 00:00:15.640 But the difference here is that they're immutable. 5 00:00:15.640 --> 00:00:17.960 And what that means is they can't be changed. 6 00:00:17.960 --> 00:00:18.460 So, in other words, 7 00:00:18.460 --> 00:00:22.020 attempting to append an item to a tuple will actually give you an error. 8 00:00:22.020 --> 00:00:24.960 Now, you might have read that definition stated that 9 00:00:24.960 --> 00:00:28.810 lists are enclosed in square brackets, and that's true, and you've seen that. 10 00:00:28.810 --> 00:00:32.480 And that tuples are enclosed in parentheses, but that is not true. 11 00:00:32.480 --> 00:00:35.190 So, a comma is used to separate the elements of a tuple, but 12 00:00:35.190 --> 00:00:39.980 the parentheses are only required when necessary to remove syntactic ambiguity. 13 00:00:39.980 --> 00:00:41.628 So, I'm gonna show you an example of what I mean. 14 00:00:41.628 --> 00:00:46.710 I can put my space here. 15 00:00:46.710 --> 00:00:52.195 So I'm gonna type t = "a", "b", "c", 16 00:00:52.195 --> 00:00:59.139 print(t) print("a", "b", 17 00:00:59.139 --> 00:01:04.660 "c") print A. 18 00:01:04.660 --> 00:01:05.160 B. C. 19 00:01:08.750 --> 00:01:13.630 So, if we actually run that, if you've seen the first example, 20 00:01:13.630 --> 00:01:18.150 the answer is returned in brackets, and that signifies that it is a double, and 21 00:01:18.150 --> 00:01:21.740 also the third example Shows that it's a tuple as well and 22 00:01:21.740 --> 00:01:25.610 you can see why the second example the one on line four. 23 00:01:25.610 --> 00:01:30.410 That's actually just returned the letters a, b, c and that's an example of having to 24 00:01:30.410 --> 00:01:35.570 use parentheses to actually avoid syntactic ambiguity. 25 00:01:35.570 --> 00:01:39.090 So, in other words print will also accept parameters followed by a comma. 26 00:01:39.090 --> 00:01:42.370 So, if you do want to print out a tuple then you'd need to actually put brackets 27 00:01:42.370 --> 00:01:44.080 there as an example. 28 00:01:44.080 --> 00:01:48.050 So, this also applies when you're actually passing a tuple tool function and 29 00:01:48.050 --> 00:01:51.850 as we've seen earlier enclosing an expression in parentheses allows it to be 30 00:01:51.850 --> 00:01:55.790 split over several lines without using line continuation characters. 31 00:01:55.790 --> 00:01:59.110 So, sometimes enclose our tuples in them as well for that reason. 32 00:01:59.110 --> 00:02:01.270 Now, you might prefer to always do so, and that's fine. 33 00:02:01.270 --> 00:02:05.460 Just be aware that if you come across code that does not use parenthesis, 34 00:02:05.460 --> 00:02:07.090 then you'll need to understand what that is. 35 00:02:07.090 --> 00:02:08.140 So, it's up to you. 36 00:02:08.140 --> 00:02:10.560 Probably what I would recommend is to actually 37 00:02:10.560 --> 00:02:14.350 use brackets as much as possible like this when creating a tuple. 38 00:02:14.350 --> 00:02:17.610 Again, we're gonna go through a bit more about what tuples are as we go 39 00:02:17.610 --> 00:02:21.330 through this video but the choice is up to you and you will see examples. 40 00:02:21.330 --> 00:02:25.090 Like the example I started with, like this example here, where they're not enclosed 41 00:02:25.090 --> 00:02:28.470 in parentheses but they're still actually tuples and you need to be aware of that. 42 00:02:28.470 --> 00:02:30.696 Before I continue And this is a digress. 43 00:02:30.696 --> 00:02:32.930 What I want to do, we haven't actually mentioned this before, but 44 00:02:32.930 --> 00:02:37.110 you can also pass a comma separated list of items to the print function, and 45 00:02:37.110 --> 00:02:38.720 it'll print them all on the same line. 46 00:02:38.720 --> 00:02:42.270 And what I mean by that, we're going to actually type some code in to show you. 47 00:02:43.730 --> 00:02:44.980 Let me comment out this code for now. 48 00:02:46.210 --> 00:02:48.060 So, let's start by creating some tuples. 49 00:02:48.060 --> 00:02:52.040 So, first one is welcome = "Welcome to my 50 00:02:52.040 --> 00:02:57.020 Nightmare" Alice Cooper, 1975. 51 00:02:57.020 --> 00:02:59.350 This is a tuple. 52 00:02:59.350 --> 00:03:01.546 Now, notice how it's a different type on the end. 53 00:03:03.213 --> 00:03:06.580 Oops, I've got my equals and so forth done correctly. 54 00:03:06.580 --> 00:03:08.320 And notice how you can have, actually, 55 00:03:08.320 --> 00:03:10.660 different data types on the one line with the tuple. 56 00:03:10.660 --> 00:03:15.813 And we'll do something like this, 57 00:03:15.813 --> 00:03:22.510 bad equals "Bad Company" "Bad Company", 58 00:03:22.510 --> 00:03:27.171 1974 budgie = Nightflight 59 00:03:31.712 --> 00:03:36.728 1981 and Imelda 60 00:03:36.728 --> 00:03:41.449 [SOUND] 2011 and 61 00:03:41.449 --> 00:03:49.421 Metallica Ride the Lightning. 62 00:03:54.296 --> 00:03:56.258 1984. 63 00:03:56.258 --> 00:04:01.450 So, we can actually print Metallica like so. 64 00:04:01.450 --> 00:04:02.970 We can actually run that. 65 00:04:02.970 --> 00:04:08.520 And you can see when we just print it it actually prints out the entire topple. 66 00:04:08.520 --> 00:04:15.830 But, we can also do print Metallica[0] in square brackets. 67 00:04:15.830 --> 00:04:16.350 And we run that. 68 00:04:16.350 --> 00:04:19.880 And surprisingly, we get the first element. 69 00:04:19.880 --> 00:04:21.330 And we can also do the same for the other two. 70 00:04:21.330 --> 00:04:22.300 We can print those separately. 71 00:04:22.300 --> 00:04:27.060 So, we can go like so, 1, and let's put that on a new line, 2. 72 00:04:27.060 --> 00:04:30.460 And we run that. 73 00:04:30.460 --> 00:04:32.660 We get the other elements printing them individually. 74 00:04:32.660 --> 00:04:34.900 But the point I want to make, as I mentioned at the start of the video, 75 00:04:34.900 --> 00:04:36.730 is that tupples are immutable. 76 00:04:36.730 --> 00:04:38.590 So, any attempt to change them, 77 00:04:38.590 --> 00:04:42.350 in this case a Metallica album to say Master of Puppets, would give an error. 78 00:04:42.350 --> 00:04:45.230 So, we can actually do something like that. 79 00:04:45.230 --> 00:04:50.330 We can try typing metallica[0], element 0, = "Master of Puppets". 80 00:04:51.510 --> 00:04:52.690 If we actually run that, 81 00:04:52.690 --> 00:04:58.340 we actually get an error, tuple object does not support item assignment. 82 00:04:58.340 --> 00:05:01.270 So, you can't actually change it in that fashion. 83 00:05:01.270 --> 00:05:04.030 Now, the good thing now is tuples actually support indexing and 84 00:05:04.030 --> 00:05:09.170 slicing, which is sort of a sneaky way of actually correcting any problems or 85 00:05:09.170 --> 00:05:11.320 updating particular entries. 86 00:05:11.320 --> 00:05:13.980 So, for example, I made a typo on line nine. 87 00:05:13.980 --> 00:05:15.340 With the actual name, Imelda Mae. 88 00:05:15.340 --> 00:05:18.920 So, if I've got to change that I can actually type something 89 00:05:18.920 --> 00:05:21.260 like imelda equals imelda. 90 00:05:21.260 --> 00:05:22.711 And we can put 0. 91 00:05:28.421 --> 00:05:32.686 If we actually run that, unless you print that out afterwards, print imelda, 92 00:05:32.686 --> 00:05:34.990 just to make sure it's actually working. 93 00:05:34.990 --> 00:05:35.910 So, if I actually run that, 94 00:05:37.090 --> 00:05:39.420 actually what we want to do is get rid of this error line first now. 95 00:05:39.420 --> 00:05:40.690 So, that that code actually executes. 96 00:05:40.690 --> 00:05:43.350 So, I'll comment that out, run that again. 97 00:05:43.350 --> 00:05:45.790 We actually get more my hand, Emilda May this time, 98 00:05:45.790 --> 00:05:48.630 remembering that initially I actually put it through incorrectly. 99 00:05:48.630 --> 00:05:50.610 It's Emilda, with an E on the start. 100 00:05:50.610 --> 00:05:53.420 So, I've actually got the values correctly added to the, or 101 00:05:53.420 --> 00:05:56.090 updated I should say in the middle of the tuple. 102 00:05:56.090 --> 00:05:57.790 So actually there's two important concepts here. 103 00:05:57.790 --> 00:06:02.190 Firstly, a type being immutable means that you can't change the contents of an object 104 00:06:02.190 --> 00:06:03.880 once you've created it. 105 00:06:03.880 --> 00:06:07.650 But, it doesn't mean that your variable Can't be assigned 106 00:06:07.650 --> 00:06:11.390 a new object of that type, so that's an important clarification here. 107 00:06:11.390 --> 00:06:15.100 So, just to confirm the differences between a immutable object, 108 00:06:15.100 --> 00:06:17.320 an mutable one, an mutable one being changeable, 109 00:06:17.320 --> 00:06:20.790 immutable meaning you can't change consider this current I'm going to add. 110 00:06:20.790 --> 00:06:26.526 So we are going to create a new 111 00:06:26.526 --> 00:06:31.546 one metallic2 equals, 112 00:06:31.546 --> 00:06:37.283 and I'm creating a list this 113 00:06:37.283 --> 00:06:42.303 time instead of a tuple, 114 00:06:42.303 --> 00:06:48.518 lightning [NOISE] Teleca two And 115 00:06:48.518 --> 00:06:52.837 print metallica2. 116 00:06:52.837 --> 00:06:56.210 So, we've created a list there, just to show you the differences. 117 00:06:56.210 --> 00:07:00.490 So, course in the case of the list, that should be metalica2 there, because I'm 118 00:07:00.490 --> 00:07:03.850 updating the list here, in this particular group of code, block of code. 119 00:07:03.850 --> 00:07:04.600 Run that again. 120 00:07:04.600 --> 00:07:09.450 So, in the case of lines 21 to 24, we're working with a list. 121 00:07:09.450 --> 00:07:13.810 And you can see that I was able to directly change the element zero, 122 00:07:13.810 --> 00:07:17.015 the album name, and it worked quite Quite nicely. 123 00:07:17.015 --> 00:07:20.565 But of course in the case above when this error are here when I actually 124 00:07:20.565 --> 00:07:24.625 ran that code Which it got an error and we couldn't actually update it that way. 125 00:07:24.625 --> 00:07:26.445 Because it's an immutable object. 126 00:07:26.445 --> 00:07:28.415 So, again tuple immutable object. 127 00:07:28.415 --> 00:07:29.297 A list is a. 128 00:07:29.297 --> 00:07:34.267 Object, but you can see also that in terms of tuples we were able to get around it 129 00:07:34.267 --> 00:07:38.057 by actually assigning new objects to that variable. 130 00:07:38.057 --> 00:07:42.420 So, in this case Imelda had the initial value set here for the tuple. 131 00:07:42.420 --> 00:07:46.210 They were able to actually recreate it and that works quite nicely. 132 00:07:46.210 --> 00:07:49.160 You'll find that the term immutable works in a lot of languages 133 00:07:50.241 --> 00:07:52.910 you can't change the existing contents, but 134 00:07:52.910 --> 00:07:55.590 you can actually reference it or to a new object of that type. 135 00:07:55.590 --> 00:08:00.330 Now, the second thing that might be obvious, but it is important for 136 00:08:00.330 --> 00:08:01.580 me to actually point out. 137 00:08:01.580 --> 00:08:04.850 Is that expressions on the right hand side of an assignment 138 00:08:04.850 --> 00:08:07.660 are always evaluated before the left hand side. 139 00:08:07.660 --> 00:08:09.660 So, in the imelda example above, 140 00:08:09.660 --> 00:08:12.930 we're taking the first element of the count imelda objects. 141 00:08:12.930 --> 00:08:16.020 I'm talking about this line here, line 18. 142 00:08:16.020 --> 00:08:18.580 So, we're taking the first element of the current IMELDA object 143 00:08:18.580 --> 00:08:22.410 which of course indicts 0 plus the string IMELDA may correctly 144 00:08:22.410 --> 00:08:26.610 typed as I-M-E-L-D-A instead on E-M-I-L-D-A. 145 00:08:26.610 --> 00:08:29.960 Followed by the third element of the current IMELDA to produce a new tuple. 146 00:08:29.960 --> 00:08:32.920 So, you can see that it's actually This would only work if 147 00:08:32.920 --> 00:08:37.620 the computer was actually evaluating this right hand side of the expression first. 148 00:08:37.620 --> 00:08:40.080 If it started evaluating the left hand side first, 149 00:08:40.080 --> 00:08:42.950 then of course this imelda object would already have been updated. 150 00:08:42.950 --> 00:08:45.220 So again, it's looking at these values first and 151 00:08:45.220 --> 00:08:48.400 assigning those before ultimately looking at the left hand side. 152 00:08:48.400 --> 00:08:50.585 To see where these objects are going to actually be saved. 153 00:08:50.585 --> 00:08:54.100 S,o in this case the new tupple was then assigned to the value mild or 154 00:08:54.100 --> 00:08:56.590 after that and it replaces the previous object. 155 00:08:56.590 --> 00:08:59.180 So, it's not the variable that is immutable, but 156 00:08:59.180 --> 00:09:03.220 it's the object that the variable Imelda in this case points to. 157 00:09:03.220 --> 00:09:06.650 So we haven't changed the contents of the tupple object. 158 00:09:06.650 --> 00:09:10.400 What we actually did here on line 18 is we created a new tupple. 159 00:09:10.400 --> 00:09:13.190 And then pointed the variable and the order to reference it. 160 00:09:13.190 --> 00:09:16.400 And again, because the right hand side is evaluated first, 161 00:09:16.400 --> 00:09:20.120 the current contents of an order can be used to create the new topple before 162 00:09:20.120 --> 00:09:22.270 the assignment and that's why line 18 actually worked. 163 00:09:22.270 --> 00:09:24.820 And probably about now you're starting to wonder, 164 00:09:24.820 --> 00:09:27.670 well Why would I actually be using a tuple? 165 00:09:27.670 --> 00:09:29.990 What's the advantages of using tuple over a list? 166 00:09:29.990 --> 00:09:33.780 Because at first glance, you might look at it and think that lists seem more useful. 167 00:09:33.780 --> 00:09:37.780 And while it's actually true that you can generally use a list in place of a tuple, 168 00:09:37.780 --> 00:09:41.600 there's some important reasons why you'd wanna favor tuples over lists. 169 00:09:41.600 --> 00:09:45.530 And then we haven't actually covered hashes or dictionaries yet in the course. 170 00:09:45.530 --> 00:09:48.530 But a dictionary key requires immutable object, and 171 00:09:48.530 --> 00:09:51.400 that removes the choice if you wanna use your object as a dictionary key. 172 00:09:51.400 --> 00:09:53.260 But, you'll be finding out more about that later. 173 00:09:53.260 --> 00:09:57.440 But another key difference, is that a list is intended, although it's not enforced, 174 00:09:57.440 --> 00:09:59.420 to contain items of the same type. 175 00:09:59.420 --> 00:10:00.850 Much like a real world list. 176 00:10:00.850 --> 00:10:03.610 So, you wouldn't add an activity such as sky diving to a shopping list, 177 00:10:03.610 --> 00:10:04.570 for example. 178 00:10:04.570 --> 00:10:07.040 The computing temp of this is homogenous. 179 00:10:07.040 --> 00:10:10.840 So, in other words the list have intended to hold homogenous items. 180 00:10:10.840 --> 00:10:13.410 In other words, meaning items of the same type. 181 00:10:13.410 --> 00:10:16.850 So, tuples are actually intended to hold heterogeneous items. 182 00:10:16.850 --> 00:10:20.205 So, like our albums example where we were storing three different things in 183 00:10:20.205 --> 00:10:20.985 each tuple. 184 00:10:20.985 --> 00:10:22.535 An album title, artist name, and 185 00:10:22.535 --> 00:10:25.375 a year, which you can see that with different types on lines 6 through 10. 186 00:10:25.375 --> 00:10:30.037 Now, the other things to keep in mind is the protection offered by using a tuple. 187 00:10:30.037 --> 00:10:32.717 The things that should not change can actually help prevent 188 00:10:32.717 --> 00:10:33.967 bugs in your programs. 189 00:10:33.967 --> 00:10:37.517 So, in other words, once an album is released, its details do not change. 190 00:10:37.517 --> 00:10:40.617 Prince and the Sensational Alex Harvey Band, notwithstanding. 191 00:10:40.617 --> 00:10:43.473 So, it actually makes sense to store the details in a tuple. 192 00:10:43.473 --> 00:10:46.583 Because of course they're fixed items, which should never change. 193 00:10:46.583 --> 00:10:49.723 So, if your code then attempts to change one of the fields in the tuple, 194 00:10:49.723 --> 00:10:52.233 the attempt is gonna fail and you saw the error earlier in the video. 195 00:10:52.233 --> 00:10:55.910 When I actually tried to do that, on line 17 which is now commented out. 196 00:10:55.910 --> 00:10:56.760 So, that it will file, and 197 00:10:56.760 --> 00:10:59.910 it will actually highlight something that your code probably shouldn't be doing. 198 00:10:59.910 --> 00:11:01.740 So, it's really good from that point of view, 199 00:11:01.740 --> 00:11:05.700 to use a tuple in that way to avoid getting into difficulties later. 200 00:11:05.700 --> 00:11:07.590 So, one example of that might be an adventure game. 201 00:11:07.590 --> 00:11:11.030 So the layout of your game, the various rooms and the links between them will be 202 00:11:11.030 --> 00:11:14.860 fixed throughout the game's, Even if all exits are not immediately available to 203 00:11:14.860 --> 00:11:18.050 the players, so you would actually store that or could store that in tuples. 204 00:11:18.050 --> 00:11:20.940 The players can change though as they gain hit points and 205 00:11:20.940 --> 00:11:24.650 take damage through the game, so a tuple wouldn't be appropriate in this case for 206 00:11:24.650 --> 00:11:26.686 example to storing player details. 207 00:11:26.686 --> 00:11:29.720 So, we're going to digress slightly now and show another way to assign values to 208 00:11:29.720 --> 00:11:32.040 variables that we haven't discussed earlier. 209 00:11:32.040 --> 00:11:34.130 So, what I'm gonna do is create a new pathing file for 210 00:11:34.130 --> 00:11:36.660 this example because we're gonna be coming back to this tuples.py shortly. 211 00:11:36.660 --> 00:11:39.900 So, I'm just gonna come up here, as I've done throughout the course, 212 00:11:39.900 --> 00:11:43.050 and I'm just gonna call this one demo but you can call it whatever you'd like. 213 00:11:44.360 --> 00:11:47.990 And close down the run window for now, and I'll just delete that first line. 214 00:11:47.990 --> 00:11:51.040 So, in Python it's possible to assign values to several variables at 215 00:11:51.040 --> 00:11:51.590 the same time. 216 00:11:51.590 --> 00:11:55.450 And here's an example of setting four variables to the same value. 217 00:11:55.450 --> 00:11:58.050 So, we can put a = b = c = d = 12. 218 00:11:58.050 --> 00:12:03.720 And if we print c, for example, and run that. 219 00:12:05.150 --> 00:12:06.370 And see, we got the answer, 12. 220 00:12:06.370 --> 00:12:10.000 So in that case, all four variables were assigned the value of 12. 221 00:12:10.000 --> 00:12:13.370 But we can also assign different values to multiple variables. 222 00:12:13.370 --> 00:12:18.040 So, another example might be A, B equals 12, 13. 223 00:12:18.040 --> 00:12:22.171 We could print A, B. 224 00:12:24.290 --> 00:12:28.360 You can see, in that case, line three, a's been assigned the value 12, 225 00:12:28.360 --> 00:12:29.470 b's been assigned the value 13. 226 00:12:29.470 --> 00:12:33.290 So, once again, remember that the right hand side is evaluated first. 227 00:12:33.290 --> 00:12:35.920 So, just clarify that, I can do something like this. 228 00:12:35.920 --> 00:12:41.490 I can type So we've got a and b defined as 12, 13, and we've printed the values out. 229 00:12:41.490 --> 00:12:44.520 We can do something like a, b = b, a. 230 00:12:44.520 --> 00:12:49.880 Then we could do print(a is 231 00:12:56.878 --> 00:12:59.921 .Formata. 232 00:12:59.921 --> 00:13:01.128 We're gonna do the same for b. 233 00:13:05.712 --> 00:13:11.611 We can run that, and we can see there we've got the a have been assigned to 13, 234 00:13:11.611 --> 00:13:14.710 and b has been assigned the value of 12. 235 00:13:14.710 --> 00:13:18.220 And again, the value for that, the reason that that's working is the right-hand 236 00:13:18.220 --> 00:13:23.090 assignment is actually calculated, or evaluated I should say first. 237 00:13:23.090 --> 00:13:26.380 Now, looking at line 3, it looks suspiciously like a tupple. 238 00:13:26.380 --> 00:13:28.680 Considering what we've learned in this session. 239 00:13:28.680 --> 00:13:31.720 And that means that we can actually pull out all the elements of a tuple in 240 00:13:31.720 --> 00:13:32.710 a single assignment. 241 00:13:32.710 --> 00:13:34.590 Please go back to the albums Python now. 242 00:13:34.590 --> 00:13:36.970 And we'll just delete a bit of this code. 243 00:13:36.970 --> 00:13:38.530 And we'll fix this tuple up here. 244 00:13:38.530 --> 00:13:41.880 And delete that. 245 00:13:41.880 --> 00:13:45.000 And metallica2 I'm gonna leave as a list there. 246 00:13:45.000 --> 00:13:47.310 We're gonna print out part two if it down here. 247 00:13:47.310 --> 00:13:49.852 This is the example, we can type title. 248 00:13:49.852 --> 00:13:56.126 Artist, year, equals Imelda. 249 00:13:56.126 --> 00:13:58.978 Print title. 250 00:13:58.978 --> 00:14:02.115 Print artist. 251 00:14:02.115 --> 00:14:04.690 Print year. 252 00:14:04.690 --> 00:14:05.960 We actually now run this code. 253 00:14:07.500 --> 00:14:08.890 Run titles. 254 00:14:08.890 --> 00:14:11.990 We get the actual, press the print of the list, which we've left in. 255 00:14:11.990 --> 00:14:15.550 But notice how now that we've extracted the valleys out of the total 256 00:14:15.550 --> 00:14:20.040 into individual variables using the common syntax that I've just shown you. 257 00:14:20.040 --> 00:14:21.620 So, there is actually a term for that. 258 00:14:21.620 --> 00:14:23.990 It's actually called unpacking the tupple. 259 00:14:23.990 --> 00:14:27.500 And just incidentally in this case, where you've got more than one file open, if you 260 00:14:27.500 --> 00:14:30.500 are ever in doubt as to which one is gonna run, come up here to the top right-hand 261 00:14:30.500 --> 00:14:33.880 corner, and you can actually choose which file you actually want to run. 262 00:14:33.880 --> 00:14:37.710 For example, I can choose demo and it will actually run demo. 263 00:14:37.710 --> 00:14:38.870 And run the code for this file. 264 00:14:38.870 --> 00:14:43.560 But, equally I could come back here and select topples and actually run that code. 265 00:14:43.560 --> 00:14:46.820 So, another alternative of course is just to right click the relevant file and 266 00:14:46.820 --> 00:14:47.620 actually run it. 267 00:14:47.620 --> 00:14:49.540 So, we've just touched on unpacking the topple. 268 00:14:49.540 --> 00:14:50.800 Gonna end the video here, and 269 00:14:50.800 --> 00:14:53.010 we'll actually talk more about this in the next video.