WEBVTT 1 00:00:02.230 --> 00:00:06.000 So in the previous video, we started talking about variables for Python and 2 00:00:06.000 --> 00:00:08.800 thus, we ended talking about operator precedence. 3 00:00:08.800 --> 00:00:13.610 So you saw that by using brackets we can make sure that the calculations we think 4 00:00:13.610 --> 00:00:15.730 work, use the correct order or 5 00:00:15.730 --> 00:00:20.120 actually act processed in the right order to avoid getting us incorrect results. 6 00:00:20.120 --> 00:00:23.560 And that's mainly by putting brackets around each sort of section. 7 00:00:23.560 --> 00:00:26.486 Now there's another way of doing that, that would be easier, and 8 00:00:26.486 --> 00:00:29.046 in some cases you may wanna instead of looking at something 9 00:00:29.046 --> 00:00:32.235 that's relatively complex like you can see with that print statement, 10 00:00:32.235 --> 00:00:35.140 you can actually use variables to hold the intermediate values. 11 00:00:35.140 --> 00:00:40.941 So for example, we could say, a variable with the name of c = a + b. 12 00:00:43.031 --> 00:00:46.410 And then we can also do d = c / 3. 13 00:00:46.410 --> 00:00:50.900 And then we could do e = d / 4. 14 00:00:50.900 --> 00:00:53.480 And then we could do print(e * 12). 15 00:00:53.480 --> 00:00:58.090 Now if we actually run that, we should get the same result in both cases. 16 00:00:59.210 --> 00:01:00.590 We get 12 for this line here. 17 00:01:00.590 --> 00:01:04.055 But we also get 12 for this line here. 18 00:01:04.055 --> 00:01:07.244 And that's because we've broken up those individual components, and 19 00:01:07.244 --> 00:01:09.981 assigned them to different variables to get the same result. 20 00:01:09.981 --> 00:01:14.015 So definitely there's gonna be times where it makes sense to break up an expression 21 00:01:14.015 --> 00:01:15.760 like this into smaller parts. 22 00:01:15.760 --> 00:01:18.620 It's really gonna be up to you to compare the two examples to see which 23 00:01:18.620 --> 00:01:19.910 one works better for you. 24 00:01:19.910 --> 00:01:23.220 Once you start getting used to the concept of brackets, 25 00:01:23.220 --> 00:01:26.750 it's easy to see actually at a glance with that one, as to what's happening 26 00:01:26.750 --> 00:01:30.140 with this one because you're introducing different variables. 27 00:01:30.140 --> 00:01:33.611 It can take a bit of work to look at that because you've actually got more output 28 00:01:33.611 --> 00:01:34.190 to look at. 29 00:01:34.190 --> 00:01:36.740 But the bottom line is there's no right or wrong here. 30 00:01:36.740 --> 00:01:39.710 It's whatever's more appropriate and more readable for you. 31 00:01:39.710 --> 00:01:43.868 So, moving on now, let's actually start talking about another data type and 32 00:01:43.868 --> 00:01:45.830 that's the data type of top string. 33 00:01:45.830 --> 00:01:49.670 Now you've seen us use strings in earlier examples to print messages on the screen. 34 00:01:49.670 --> 00:01:52.830 But I want to go through now and show you some of the other cool things you can do 35 00:01:52.830 --> 00:01:54.650 when you're actually working with strings. 36 00:01:54.650 --> 00:01:58.480 So strings are one of Python's what they call sequence data types, 37 00:01:58.480 --> 00:02:00.440 which makes sense because that's what it really is. 38 00:02:00.440 --> 00:02:03.380 It's really a sequence of characters that you can put together 39 00:02:03.380 --> 00:02:05.110 that form an actual string. 40 00:02:05.110 --> 00:02:07.860 So we've seen how to print them out and concatenate strings together. 41 00:02:07.860 --> 00:02:10.630 But we can also pick out individual characters or substrings. 42 00:02:10.630 --> 00:02:14.360 So I'm gonna delete some of this now, and let's just see how that works. 43 00:02:14.360 --> 00:02:18.085 I'm gonna type parrot = "Norwegian Blue". 44 00:02:18.085 --> 00:02:19.993 We can print(parrot). 45 00:02:19.993 --> 00:02:26.080 And no surprises there if we run that to see what actually happens. 46 00:02:27.490 --> 00:02:34.175 Norwegian Blue, we should expect, we can actually do print(parrot[3]) like so. 47 00:02:36.467 --> 00:02:40.864 If you run that you can see what's happened is by doing that it's 48 00:02:40.864 --> 00:02:43.070 actually printed the letter w. 49 00:02:43.070 --> 00:02:46.090 Now you might be looking at that and thinking, well why is that w 50 00:02:46.090 --> 00:02:50.430 because we specified the third character, so shouldn't that be counting from left to 51 00:02:50.430 --> 00:02:54.540 right, N is the first character, o is the second and r is the third. 52 00:02:54.540 --> 00:02:57.090 And in actual fact w is the fourth character. 53 00:02:57.090 --> 00:02:58.170 So why did that work? 54 00:02:58.170 --> 00:03:01.760 It's actually usual for programming languages to start counting at zero, 55 00:03:01.760 --> 00:03:03.160 and not at one. 56 00:03:03.160 --> 00:03:04.900 So that's parrot zero is N. 57 00:03:04.900 --> 00:03:07.199 So we can actually go to confirm that. 58 00:03:09.335 --> 00:03:12.620 Parrot[0], if we actually run that is an n. 59 00:03:12.620 --> 00:03:16.220 So you'll find that in most languages, as I said, they'll actually start counting 60 00:03:16.220 --> 00:03:19.770 from position n and not from position one as we would normally do as humans. 61 00:03:19.770 --> 00:03:22.620 So there's 14 characters in Norwegian Blue, numbered from zero to 13. 62 00:03:22.620 --> 00:03:27.010 If we try to print(parrot[14]), we get an error, string index out of range. 63 00:03:27.010 --> 00:03:34.589 So if we actually try and do that So 14. 64 00:03:34.589 --> 00:03:35.430 We'll actually get an error. 65 00:03:35.430 --> 00:03:41.080 And that's because as far as it's concerned, there's no character 66 00:03:41.080 --> 00:03:44.540 at that position, because there's only characters in position 0 to 13. 67 00:03:44.540 --> 00:03:47.240 The other thing you can actually do is count backwards. 68 00:03:47.240 --> 00:03:49.976 So, you can type print(parrot[-1]). 69 00:03:49.976 --> 00:03:54.331 And if we actually run that, negative one gives us a, so 70 00:03:54.331 --> 00:04:01.030 what's happened is that it started here at position zero and went to negative one. 71 00:04:01.030 --> 00:04:03.020 Negative one goes all to the way to the end of the string and 72 00:04:03.020 --> 00:04:04.920 starts counting from the end. 73 00:04:04.920 --> 00:04:09.450 So we get the letter a coming up, which is the last character from the string. 74 00:04:09.450 --> 00:04:10.720 And we can work backwards like that. 75 00:04:10.720 --> 00:04:13.740 We can also print what Python calls a slice by, 76 00:04:13.740 --> 00:04:17.640 instead of providing one number, provide two numbers separated by a colon. 77 00:04:17.640 --> 00:04:19.225 So in other words, it's gonna be like a range. 78 00:04:19.225 --> 00:04:25.160 So, we can do print(parrot[0:6]), 79 00:04:25.160 --> 00:04:30.530 if you run that, you get Norweg, which is the six characters starting, 80 00:04:30.530 --> 00:04:33.620 which is the length of six characters starting at position zero. 81 00:04:33.620 --> 00:04:38.190 So the first number is the number in the string to start, printing out, and 82 00:04:38.190 --> 00:04:40.690 the sixth is how many characters to actually print out. 83 00:04:40.690 --> 00:04:42.780 So it's actually two ways at looking at this. 84 00:04:42.780 --> 00:04:48.000 First, you could say that printing parrot[0:6] starts from 85 00:04:48.000 --> 00:04:53.250 character's position index zero up to but not including index position six. 86 00:04:53.250 --> 00:04:56.320 But we can also say the six character starting from index zero. 87 00:04:56.320 --> 00:05:00.340 But in Python, the first is technically more correct even if it seems to amount 88 00:05:00.340 --> 00:05:03.990 where they both seem, well actually both do end up with showing the same amount. 89 00:05:03.990 --> 00:05:05.480 If the first number is a minute, 90 00:05:05.480 --> 00:05:08.030 the slice is gonna start from the beginning of the string. 91 00:05:08.030 --> 00:05:10.900 If the second number is a minute, it's gonna run to the end of the string. 92 00:05:10.900 --> 00:05:12.260 Just to show you what I mean by that. 93 00:05:12.260 --> 00:05:20.580 Type print(parrot[:6]. 94 00:05:20.580 --> 00:05:21.948 That's gonna start from automatically, 95 00:05:21.948 --> 00:05:23.442 because we haven't specified a number here. 96 00:05:23.442 --> 00:05:26.315 It's gonna start from the start of the string. 97 00:05:26.315 --> 00:05:28.580 That's gonna print the same as the previous line. 98 00:05:28.580 --> 00:05:31.830 But if we also did print(parrot[6:]) and 99 00:05:31.830 --> 00:05:36.610 didn't specify the second number, it's gonna run to the end of the string. 100 00:05:36.610 --> 00:05:40.700 So if you run that, you can see it's moved, started at the position, so 101 00:05:40.700 --> 00:05:43.980 it's actually went to effectively the seventh position, so it's counting, 102 00:05:43.980 --> 00:05:48.500 zero, one, two, three, four, five, six, ian Blue. 103 00:05:48.500 --> 00:05:51.780 So ian Blue and it's going along because we haven't specified where to end, 104 00:05:51.780 --> 00:05:55.080 so it's actually moved on to the end, continued and done the entire string. 105 00:05:55.080 --> 00:05:57.040 And if you start dealing with negative indices, 106 00:05:57.040 --> 00:05:59.900 it quickly becomes apparent while the second definition is not gonna work. 107 00:05:59.900 --> 00:06:06.330 So in other words, if we are stuck trying to do this, print(parrot[-4:2]) 108 00:06:06.330 --> 00:06:13.270 that's not actually gonna print anything. 109 00:06:13.270 --> 00:06:15.650 That's because you can't go back which from the starting position in this 110 00:06:15.650 --> 00:06:16.510 context. 111 00:06:16.510 --> 00:06:19.800 But if we actually go back and put a -2 there, and on that. 112 00:06:21.780 --> 00:06:25.260 You can see it's actually started and produced a bill and that's because it 113 00:06:25.260 --> 00:06:28.750 started from the start of the string, it's called back four characters, up to and 114 00:06:28.750 --> 00:06:32.930 including the second to last character in the string which was the U. 115 00:06:32.930 --> 00:06:36.120 Another thing you can actually do is you can actually do a print. 116 00:06:36.120 --> 00:06:39.443 You can actually do funny slices that skip characters candy and stips. 117 00:06:39.443 --> 00:06:45.288 So I could do print(parrot[0:6:2]). 118 00:06:45.288 --> 00:06:47.240 And that should give us the answer in Nre. 119 00:06:47.240 --> 00:06:50.170 So we run that, Nre as you can see. 120 00:06:50.170 --> 00:06:54.800 So starting from index position zero, extract all the characters up to, but 121 00:06:54.800 --> 00:06:58.110 not including index position six in steps of two. 122 00:06:58.110 --> 00:07:02.100 As you saw in the first example and that task started on N. 123 00:07:02.100 --> 00:07:04.590 And skip to r and skip to e. 124 00:07:04.590 --> 00:07:11.420 And we can change that and do something like print(parrot[0:6:3]), 125 00:07:11.420 --> 00:07:16.300 so I make the step increment three, and we should get N and w in that case, and 126 00:07:16.300 --> 00:07:18.640 w because it's skipping three characters at time. 127 00:07:18.640 --> 00:07:21.453 So slightly useless application, but 128 00:07:21.453 --> 00:07:25.810 I'm gonna show you an example is All this top number = "9. 129 00:07:25.810 --> 00:07:28.726 And there's numbers here, 130 00:07:28.726 --> 00:07:34.807 223,372,036,854,775,807". 131 00:07:34.807 --> 00:07:39.315 And if I actually do a prin(number[1::4] with a two columns, 132 00:07:39.315 --> 00:07:44.367 if I actually run that, you can see how it's actually skipped the numbers and 133 00:07:44.367 --> 00:07:49.057 actually just printed out the commas at each position automatically. 134 00:07:49.057 --> 00:07:52.112 So that's a good way to step through the extract portions out of the particular 135 00:07:52.112 --> 00:07:53.660 string that you wanna work with. 136 00:07:53.660 --> 00:07:58.710 But using a better example, let's just try numbers = "1, 137 00:07:58.710 --> 00:08:03.663 2, 3, and notice I'm putting one space after each, 4, 138 00:08:03.663 --> 00:08:06.404 5, 6, 7, 8, 9" like so. 139 00:08:06.404 --> 00:08:11.748 And if we actually do, print(number put 140 00:08:11.748 --> 00:08:17.102 numbers there numbers[0::3]). 141 00:08:17.102 --> 00:08:18.070 We actually run that. 142 00:08:19.480 --> 00:08:22.180 You can see we've quickly extracted out the numbers, and 143 00:08:22.180 --> 00:08:24.170 we've avoided the commas and the spaces there. 144 00:08:24.170 --> 00:08:27.540 So you can see how it would be useful if you wanted to extract information out in 145 00:08:27.540 --> 00:08:28.520 a particular format. 146 00:08:28.520 --> 00:08:31.660 So moving on, let's talk a little bit about string operators. 147 00:08:31.660 --> 00:08:33.030 So we've talked about operators before, and 148 00:08:33.030 --> 00:08:36.370 you've seen the plus operator when we use strings to concatenate. 149 00:08:36.370 --> 00:08:40.613 So if we had something like this. 150 00:08:40.613 --> 00:08:43.710 String1 = "he' s". 151 00:08:43.710 --> 00:08:46.084 String2 = "probably ". 152 00:08:48.070 --> 00:08:52.830 We could do print(string1 + string2). 153 00:08:52.830 --> 00:08:54.390 We've seen that before. 154 00:08:54.390 --> 00:08:57.490 If we run that, the two strings get added together, he's probably. 155 00:08:57.490 --> 00:08:59.182 But if you're using a string literal, and 156 00:08:59.182 --> 00:09:02.613 that's when you're just typing the value in direct and you're not using a variable, 157 00:09:02.613 --> 00:09:04.463 you don't actually need to use the plus sign. 158 00:09:04.463 --> 00:09:09.344 So we could do something like this, we could do 159 00:09:09.344 --> 00:09:14.619 print("he's " "probably " "pining"). 160 00:09:14.619 --> 00:09:15.630 We can run that. 161 00:09:16.810 --> 00:09:20.610 We can see how Python quite happily allows to do actually do something like that. 162 00:09:20.610 --> 00:09:22.540 Now I'm mentioning that just to show you how it works. 163 00:09:22.540 --> 00:09:25.680 I wouldn't think you'll ever wanna do something like that because it's a little 164 00:09:25.680 --> 00:09:26.820 bit totally confusing. 165 00:09:26.820 --> 00:09:30.010 And probably not really useful because you can obviously just do this, 166 00:09:30.010 --> 00:09:34.210 you can actually delete that out anyway and obviously you get the same result. 167 00:09:34.210 --> 00:09:37.910 But it's just that, showing you that it can potentially be done. 168 00:09:37.910 --> 00:09:40.380 So next, so you can actually multiply strings. 169 00:09:40.380 --> 00:09:43.390 It sounds a bit weird because multiplication generally is on any 170 00:09:43.390 --> 00:09:49.360 numbers, but if you do something like this: print("Hello : *5). 171 00:09:49.360 --> 00:09:51.755 Path will give you, Hello Hello Hello Hello Hello so 172 00:09:51.755 --> 00:09:54.190 it will give you one of this in the string. 173 00:09:54.190 --> 00:09:55.245 And it will do that five times. 174 00:09:55.245 --> 00:09:57.090 It's sort of like appending to that five times. 175 00:09:57.090 --> 00:09:59.410 An operator precedence applies. 176 00:09:59.410 --> 00:10:04.681 So if we do something like print("Hello " *5 177 00:10:04.681 --> 00:10:09.240 +4) remembering what we know about operator precedence. 178 00:10:09.240 --> 00:10:09.940 We try and run that. 179 00:10:11.300 --> 00:10:15.118 We actually get an error, can't convert int object into a string implicitly. 180 00:10:15.118 --> 00:10:18.796 And that's because, hello, the times 5, this part here, 181 00:10:18.796 --> 00:10:23.604 that actually evaluates to a string, and they attempt to then add 4 to it fouls, 182 00:10:23.604 --> 00:10:28.340 and that's really not the way to do it, so if you want to do something like that, 183 00:10:28.340 --> 00:10:31.416 you'd actually either put it brackets, like so. 184 00:10:31.416 --> 00:10:34.770 And if you run that you then get the correct result. 185 00:10:34.770 --> 00:10:39.436 Alternatively you would type print("Hello " * 5 + "4"). 186 00:10:39.436 --> 00:10:42.702 If you run that, that's 187 00:10:42.702 --> 00:10:47.370 actually if you wanted to pin the number 188 00:10:47.370 --> 00:10:52.420 four onto the end of the strings that have been modified out five times. 189 00:10:52.420 --> 00:10:55.310 There's also an operator to check if one string 190 00:10:55.310 --> 00:10:58.750 is a substring of another which can be useful for searching purposes. 191 00:10:58.750 --> 00:11:01.870 So we did something like today = "friday". 192 00:11:03.910 --> 00:11:07.530 We can do print("day" in today). 193 00:11:07.530 --> 00:11:10.210 So that's the in operator. 194 00:11:10.210 --> 00:11:11.970 So that should return true. 195 00:11:11.970 --> 00:11:15.240 Returns a true or false to indicate whether it thinks 196 00:11:15.240 --> 00:11:19.550 that whatever we've typed here, day in this case, is actually in that word. 197 00:11:19.550 --> 00:11:21.840 So if we run that, we should get true. 198 00:11:21.840 --> 00:11:23.230 And so you've got True there on the screen. 199 00:11:23.230 --> 00:11:27.418 If we did something like prin("fri" in 200 00:11:27.418 --> 00:11:31.590 today) we should also get a true, which we'll test shortly. 201 00:11:31.590 --> 00:11:35.614 And if we do something different like print("thur" in 202 00:11:35.614 --> 00:11:38.650 today) we should get a false for that. 203 00:11:38.650 --> 00:11:41.335 And lastly, we did something like, 204 00:11:41.335 --> 00:11:46.545 ("parrot" in "fjord") that should obviously be a false as well. 205 00:11:46.545 --> 00:11:51.862 So if true show "day" in today, if I rise in today in Thursday or 206 00:11:51.862 --> 00:11:55.410 Thur is not in that variable called today. 207 00:11:55.410 --> 00:12:00.030 And, obviously the word, parrot, is not in fjord either. 208 00:12:00.030 --> 00:12:03.070 So that's you got some basic k information on operators. 209 00:12:03.070 --> 00:12:06.330 And I hope you got a lot out of the two videos on variables in Python. 210 00:12:06.330 --> 00:12:08.142 In the next video, we're gonna take it to the next level. 211 00:12:08.142 --> 00:12:11.640 We're gonna look at various ways that we can format strings and also some of 212 00:12:11.640 --> 00:12:15.410 the string methods that are built into Python to allow us to manipulate them. 213 00:12:15.410 --> 00:12:16.800 So, I'll see you in that next video.