WEBVTT 1 00:00:02.230 --> 00:00:05.060 So in the previous video we drew attention to the fact that there's code 2 00:00:05.060 --> 00:00:09.210 warnings in IntelliJ, and they're those dashes you saw on the right hand side. 3 00:00:09.210 --> 00:00:12.120 So, right at the moment now we haven't typed anything in, but 4 00:00:12.120 --> 00:00:15.570 you'll notice the little green arrow there, which is actually indicating that 5 00:00:15.570 --> 00:00:17.900 there's no actual errors in the code or no warnings. 6 00:00:17.900 --> 00:00:21.660 So as I start typing, i'm gonna actually type in the program that we actually typed 7 00:00:21.660 --> 00:00:22.880 in in the last section. 8 00:00:22.880 --> 00:00:28.420 Notice the little warnings that actually come 9 00:00:28.420 --> 00:00:31.760 up, the little lines as I type. 10 00:00:31.760 --> 00:00:33.150 So I type number=9223372036854775807. 11 00:00:33.150 --> 00:00:35.140 So far, so good. 12 00:00:35.140 --> 00:00:41.100 You'll notice there's a little I there showing as I was typing and 13 00:00:41.100 --> 00:00:42.340 that it actually ended up with a t. 14 00:00:42.340 --> 00:00:46.370 So as I'm typing, it's actually scanning and 15 00:00:46.370 --> 00:00:48.060 giving us errors if there are errors there. 16 00:00:48.060 --> 00:00:52.170 So cleaned number, equals double quote, 17 00:00:52.170 --> 00:00:57.190 double quote gives us two quotes, and I'll add a few more spaces there so 18 00:00:57.190 --> 00:00:59.610 we can be clear about what we're trying to do here. 19 00:00:59.610 --> 00:01:03.580 So, for i in range, just going to type this program in again now, 20 00:01:03.580 --> 00:01:06.938 0, len(number), again, this is the old version. 21 00:01:06.938 --> 00:01:12.818 If number 22 00:01:12.818 --> 00:01:17.828 I in 0123456789, then we're gonna do 23 00:01:17.828 --> 00:01:24.310 cleanedNumber = cleanedNumber, plus number I. 24 00:01:24.310 --> 00:01:28.110 And it's just a couple more lines. 25 00:01:28.110 --> 00:01:30.703 New number, whoops, getting our indentation numbers correct. 26 00:01:30.703 --> 00:01:34.071 New number equals int, 27 00:01:34.071 --> 00:01:38.850 clean number, then print into the top of there. 28 00:01:40.100 --> 00:01:43.675 Come back to the minute print, the number is 29 00:01:46.675 --> 00:01:50.480 .format(newNumber) and fix that typo. 30 00:01:50.480 --> 00:01:53.430 And again notice that when you have actually made a typo, 31 00:01:53.430 --> 00:01:56.750 it does actually appear as here as some warning as well. 32 00:01:56.750 --> 00:01:57.590 So type int. 33 00:01:58.910 --> 00:01:59.780 Fix that up. 34 00:01:59.780 --> 00:02:03.770 So if you actually type it in, exactly as I've typed it in here, the first real code 35 00:02:03.770 --> 00:02:07.860 of course starts on line three, the number we're actually assigning the value 36 00:02:07.860 --> 00:02:11.490 into a number variable, you should see a marker at the end of line eight. 37 00:02:11.490 --> 00:02:12.160 So coming down here, 38 00:02:12.160 --> 00:02:16.200 there's actually one on line three as well, missing white space around operator. 39 00:02:16.200 --> 00:02:20.000 So what I should have done there is put, = and a space, like so. 40 00:02:20.000 --> 00:02:21.470 And that will get rid off that one. 41 00:02:21.470 --> 00:02:22.810 But the one we wanna look at first, 42 00:02:22.810 --> 00:02:25.710 before we get started, is this one here on line eight. 43 00:02:25.710 --> 00:02:27.620 So if you hover over this with a mouse again, 44 00:02:27.620 --> 00:02:30.440 it says, assignments can be replaced with augmented assignment. 45 00:02:30.440 --> 00:02:33.110 And again, with these warnings we're already on that line, but if you click on 46 00:02:33.110 --> 00:02:35.730 them you will actually go for that line and sort of parse button as well. 47 00:02:35.730 --> 00:02:38.070 You can start editing it as well. 48 00:02:38.070 --> 00:02:40.278 So in this case, this is actually not an error. 49 00:02:40.278 --> 00:02:43.892 It's more of a suggestion, but given that in the last videos I recommended you 50 00:02:43.892 --> 00:02:45.262 checking out these markers, 51 00:02:45.262 --> 00:02:48.406 this is actually a good place to discuss the augmented assignment. 52 00:02:48.406 --> 00:02:50.890 Which of course is not what it's actually asking about, 53 00:02:50.890 --> 00:02:54.450 it's actually mentioning that maybe we should be using augmented assignment. 54 00:02:54.450 --> 00:02:57.320 So I know augmented assignment sounds really fancy, but 55 00:02:57.320 --> 00:03:00.220 it's really just a short-hand way of assigning values to a variable. 56 00:03:00.220 --> 00:03:05.380 Now the Python documentation describes them as the following, the combination in 57 00:03:05.380 --> 00:03:10.800 single statement of a binary operation and an assignment statement, which is true but 58 00:03:10.800 --> 00:03:15.490 If you're like me, listening to that doesn't sound terribly understandable. 59 00:03:15.490 --> 00:03:18.600 So in line eight we performed the concatenation of clean number plus 60 00:03:18.600 --> 00:03:19.730 number i. 61 00:03:19.730 --> 00:03:21.970 Then we assigned the result to clean number. 62 00:03:21.970 --> 00:03:24.770 So the concatenation is the binary operation. 63 00:03:24.770 --> 00:03:27.070 Binary because it takes two upper ends to work on. 64 00:03:27.070 --> 00:03:29.490 And then of course, there's the assignment after that. 65 00:03:29.490 --> 00:03:32.720 So what you could do to change this to an augmented assignment, we could instead, 66 00:03:32.720 --> 00:03:34.960 I have this example I've done here on line eight. 67 00:03:34.960 --> 00:03:39.526 We could have actually done this, +=number i. 68 00:03:39.526 --> 00:03:44.710 When we do that, the warning indicator over here has now disappeared, 69 00:03:44.710 --> 00:03:47.370 and it's telling that this is a more efficient way. 70 00:03:47.370 --> 00:03:49.750 It's a short hand way for what we had in the past. 71 00:03:49.750 --> 00:03:53.360 Now if you're used to programming in Java or C++, and particularly if you used 72 00:03:53.360 --> 00:03:58.000 IntelliJ ,then you would find that IntelliJ doesn't make this suggestion for 73 00:03:58.000 --> 00:04:00.580 those languages, because both statements are identical. 74 00:04:00.580 --> 00:04:02.180 The use is just really a matter of preference. 75 00:04:02.180 --> 00:04:06.140 So whether you typed it in the long way or the short way with the plus equal, 76 00:04:06.140 --> 00:04:09.670 as far as Java or C++, which is two examples of languages. 77 00:04:09.670 --> 00:04:11.070 It makes no difference. 78 00:04:11.070 --> 00:04:15.260 Thing to keep in mind though, is that in Python the augmented assignment form only 79 00:04:15.260 --> 00:04:20.670 evaluates the signee, which is the clean number in this example, once. 80 00:04:20.670 --> 00:04:22.890 So, in the other form, where clean number appeared twice, 81 00:04:22.890 --> 00:04:25.390 the variable is evaluated more than once. 82 00:04:25.390 --> 00:04:27.430 So, basically there's more work for the computer to do. 83 00:04:27.430 --> 00:04:30.440 So, it's actually processing that more than once. 84 00:04:30.440 --> 00:04:31.725 And also with the first form, 85 00:04:31.725 --> 00:04:35.700 plus the long winded way with clean number equals clean number plus one. 86 00:04:35.700 --> 00:04:40.620 Python actually creates a new variable in memory, assigns the value after 87 00:04:40.620 --> 00:04:44.450 performing the calculation, then destroys the original version. 88 00:04:44.450 --> 00:04:47.650 Now using an augmented assignment can perform the operation in place, 89 00:04:47.650 --> 00:04:49.610 with possible modifying of the original variables. 90 00:04:49.610 --> 00:04:50.850 So there's less work. 91 00:04:50.850 --> 00:04:52.140 There's one less variable. 92 00:04:52.140 --> 00:04:56.630 In this case it would be created internally from Python's perspective, and 93 00:04:56.630 --> 00:04:58.880 it's going to be more efficient in most cases. 94 00:04:58.880 --> 00:05:02.230 With that said, it's not always possible, depending on the type of object. 95 00:05:02.230 --> 00:05:04.670 We have the variable, in other words, in question to the do this. 96 00:05:04.670 --> 00:05:06.580 But it can be done in place wherever possible. 97 00:05:06.580 --> 00:05:10.020 And that's why in this case IntelliJ, when you're using Python with IntelliJ, 98 00:05:10.020 --> 00:05:13.710 it's suggesting that you use augmented assignments rather than longhand. 99 00:05:13.710 --> 00:05:17.830 Because unlike Java and other programming languages, the shorthand way with the plus 100 00:05:17.830 --> 00:05:21.380 equals in this particular case is actually a lot more efficient 101 00:05:21.380 --> 00:05:24.730 from Python's perspective, and you'll ultimately, as you start writing 102 00:05:24.730 --> 00:05:27.810 real Python programs you're going to be finding this is more efficient, and you're 103 00:05:27.810 --> 00:05:30.780 going to be actually saving resources which is actually very important. 104 00:05:30.780 --> 00:05:32.600 And just a reminder that this last line here, 105 00:05:32.600 --> 00:05:36.390 this last warning, it's saying there's no new line at the end of file. 106 00:05:36.390 --> 00:05:40.320 We fix that by just creating an empty line and that will actually remove that error. 107 00:05:40.320 --> 00:05:42.580 And we should in due course get a little tick that'll appear up here. 108 00:05:42.580 --> 00:05:45.340 And again as I mentioned the term augmented 109 00:05:45.340 --> 00:05:48.260 assignment sounds a lot more complicated, and it really is. 110 00:05:48.260 --> 00:05:51.090 Sounds like it's a fancy term. 111 00:05:51.090 --> 00:05:52.680 It's really just a short hand way as you saw. 112 00:05:52.680 --> 00:05:53.330 So the symbol for 113 00:05:53.330 --> 00:05:56.740 them is just like typing the binary operator followed by an equal sign. 114 00:05:56.740 --> 00:05:58.790 So you could do something similar. 115 00:05:58.790 --> 00:06:00.680 You put plus there, but you can use other ones. 116 00:06:00.680 --> 00:06:04.700 You can use the minus, and that would be the equivalent to clean number=clean 117 00:06:04.700 --> 00:06:08.360 number, type number i, which doesn't make sense in the context of this string, but 118 00:06:08.360 --> 00:06:11.160 it might be if you're actually adding numbers. 119 00:06:11.160 --> 00:06:12.270 You could also use multiplication. 120 00:06:12.270 --> 00:06:14.190 That would be star equals like that. 121 00:06:14.190 --> 00:06:18.360 And then you could use slash for division slash equals. 122 00:06:18.360 --> 00:06:21.440 So let's actually go through an example of a couple of these so 123 00:06:21.440 --> 00:06:22.510 they actually make sense. 124 00:06:22.510 --> 00:06:24.700 So we can start by saying x equals twenty three, 125 00:06:26.280 --> 00:06:28.130 then we could put x plus equals one. 126 00:06:28.130 --> 00:06:31.320 So applying this to numbers makes a lot more sense. 127 00:06:31.320 --> 00:06:34.240 We put x of course we should get the value twenty four. 128 00:06:34.240 --> 00:06:39.300 So if we run that I see we got the value 24, so no surprises there. 129 00:06:39.300 --> 00:06:42.230 Then if we actually put x -= 4 and 130 00:06:42.230 --> 00:06:46.780 print that out again that should be 24 take four. 131 00:06:46.780 --> 00:06:48.210 We should get the answer 20. 132 00:06:48.210 --> 00:06:52.810 And there's a second answer there, it's 20, which is correct. 133 00:06:52.810 --> 00:06:56.660 And continuing on from that we can write x=x*=5, 134 00:06:56.660 --> 00:07:01.090 that's what it is, it's the abbreviation for it. 135 00:07:01.090 --> 00:07:04.380 And that should be 20 times 5 and that should give us 100 when we run it. 136 00:07:04.380 --> 00:07:09.560 Let's say we've got 100 there and if we try division x/=4, 137 00:07:09.560 --> 00:07:14.380 so x equals x divided by 4, in other words. 138 00:07:14.380 --> 00:07:17.140 That should give us an answer of 25. 139 00:07:17.140 --> 00:07:21.950 Bearing in mind it's a flighty answer because we only used a single division. 140 00:07:21.950 --> 00:07:26.780 And from there we could have done something like x star, star equals two. 141 00:07:26.780 --> 00:07:33.760 And print that out and we should get the answer of 625.0 from that. 142 00:07:33.760 --> 00:07:36.480 And of course if you recall star, star is to the power. 143 00:07:36.480 --> 00:07:40.160 So it's basically 25 to the power of 25, 144 00:07:40.160 --> 00:07:43.510 which was to the power of 2 I should say which was 625. 145 00:07:43.510 --> 00:07:48.730 And lastly from our remainder process x like so. 146 00:07:48.730 --> 00:07:52.930 If you print x from there, so we're looking for the remainder. 147 00:07:52.930 --> 00:07:56.650 I've divided what the current value of x is by 60, if we actually run that. 148 00:07:56.650 --> 00:07:57.760 We get the answer of 25. 149 00:07:57.760 --> 00:08:01.930 And that's because 60 goes into 625 ten times, 150 00:08:01.930 --> 00:08:07.130 leaving a remainder of 25, and the number 25 is what the answer is to that. 151 00:08:07.130 --> 00:08:12.690 And just to finish off, we can actually apply some of these, binary operators. 152 00:08:12.690 --> 00:08:15.990 There's also two binary operators that can be performed on strings. 153 00:08:15.990 --> 00:08:19.350 Concatenation which we've seen, but we'll do again and also repetition. 154 00:08:19.350 --> 00:08:24.005 So we could put greeting = "Good" 155 00:08:24.005 --> 00:08:31.390 (greeting)+= "morning" print (greeting). 156 00:08:31.390 --> 00:08:32.230 We can actually run that. 157 00:08:33.470 --> 00:08:38.260 We have good morning, so you've seen the pluses before for catanation purposes. 158 00:08:38.260 --> 00:08:39.110 But we can also put this. 159 00:08:39.110 --> 00:08:46.470 We can put greeting *=far so it's actually replicating by repetition. 160 00:08:46.470 --> 00:08:49.730 The current contents of the string if you actually print that out. 161 00:08:49.730 --> 00:08:52.030 You can actually run that. 162 00:08:52.030 --> 00:08:53.130 And we get good morning, five times. 163 00:08:53.130 --> 00:08:55.770 And we could've added a space here. 164 00:08:55.770 --> 00:08:59.500 Then run them again if you wanna them a little bit more nicely across the screen. 165 00:08:59.500 --> 00:09:01.760 And you can see it's repeated five times. 166 00:09:01.760 --> 00:09:04.800 They're the only binary operators that actually work with strings. 167 00:09:04.800 --> 00:09:07.790 Now there's five more binary operations that we haven't yet looked at, but 168 00:09:07.790 --> 00:09:09.190 we're gonna leave those for later video. 169 00:09:09.190 --> 00:09:11.668 I'll actually just paste you a list of what they are on the screen now so 170 00:09:11.668 --> 00:09:12.643 you can actually see them. 171 00:09:14.905 --> 00:09:16.170 So we've seen some of them. 172 00:09:16.170 --> 00:09:17.200 We've seen a lot of them. 173 00:09:17.200 --> 00:09:20.120 But some of these other ones here, less than, less than, equals, greater than, 174 00:09:20.120 --> 00:09:22.850 greater than, equals ampersand equals, and so on. 175 00:09:22.850 --> 00:09:26.300 So we'll actually see those and be talking about those in a future video. 176 00:09:26.300 --> 00:09:27.280 So that's the end of this video. 177 00:09:27.280 --> 00:09:30.130 In the next video, it's time for a section challenge. 178 00:09:30.130 --> 00:09:33.020 So, we're gonna summarize what we've done to date in this section of the course, and 179 00:09:33.020 --> 00:09:36.880 then we're actually gonna come up with a challenge to hopefully help you understand 180 00:09:36.880 --> 00:09:39.290 all the concepts that you've actually learned in this section. 181 00:09:39.290 --> 00:09:40.540 So, I'll see you in that next video.