WEBVTT 1 00:00:02.350 --> 00:00:05.210 So now that we can perform calculations and just play the results, 2 00:00:05.210 --> 00:00:10.120 it's time to move on and see how we can get Python to make decisions and 3 00:00:10.120 --> 00:00:12.260 also how to perform tasks repeatedly. 4 00:00:12.260 --> 00:00:15.350 Unlike most programming languages Python works in blocks of code, 5 00:00:15.350 --> 00:00:18.550 and actually one of the design principals of Python 6 00:00:18.550 --> 00:00:21.000 was that it should be uncluttered and easy to read. 7 00:00:21.000 --> 00:00:25.307 Because of this Python's doesn't use delimiters and blocks of code. 8 00:00:25.307 --> 00:00:27.750 And so what I'm going to do is show you what I mean by that, and 9 00:00:27.750 --> 00:00:29.490 show you the Python solution. 10 00:00:29.490 --> 00:00:34.280 So, I've opened up a new project, and I'm going to create a new path and file, and 11 00:00:34.280 --> 00:00:39.750 I'm going to call it Tabs, for reasons that will become apparent shortly. 12 00:00:40.990 --> 00:00:45.350 So firstly, let's just type in an example in Python on how indentation works. 13 00:00:45.350 --> 00:00:49.750 You saw this in the previous video, we create a for loop. 14 00:00:49.750 --> 00:00:57.060 So for i in range(1, 12 colon. 15 00:00:57.060 --> 00:01:01.300 And notice when I press enter here, it's automatically indented by Python. 16 00:01:01.300 --> 00:01:05.100 So python has moved forward four letters, or four spaces I should say, 17 00:01:05.100 --> 00:01:08.000 so that when we start typing the next part, it's actually indented. 18 00:01:08.000 --> 00:01:10.180 So this is the concept of indenting. 19 00:01:10.180 --> 00:01:13.016 And I'm just gonna finish typing this, and I won't go through the code, 20 00:01:13.016 --> 00:01:15.690 because we explained that and went through that in the previous video 21 00:01:32.393 --> 00:01:33.163 Okay, so and 22 00:01:33.163 --> 00:01:37.654 if we actually run that we have the answers that we saw in the previous video. 23 00:01:37.654 --> 00:01:42.175 Now a point I wanna make though is about the use of indentation and 24 00:01:42.175 --> 00:01:46.270 using braces in other programming languages. 25 00:01:46.270 --> 00:01:50.950 For example with Java, you can type something and you'd put if something. 26 00:01:52.540 --> 00:01:56.300 And to do the same thing with indentation, you actually put a brace like so, 27 00:01:56.300 --> 00:01:58.230 and then you'd actually add another brace. 28 00:01:58.230 --> 00:02:00.252 Now obviously we're getting an error on the screen now, 29 00:02:00.252 --> 00:02:04.450 because IntelliJ is saying, well this is the valid path and code. 30 00:02:04.450 --> 00:02:07.600 So the point I wanna make is that, you have to actually denote the code block, 31 00:02:07.600 --> 00:02:10.600 the code that you want executed in the middle, by using something. 32 00:02:10.600 --> 00:02:12.310 Something actually like a brace. 33 00:02:12.310 --> 00:02:14.990 Well, the deleting this out now in order to get rid of that error. 34 00:02:14.990 --> 00:02:15.740 Python doesn't do that. 35 00:02:15.740 --> 00:02:20.610 So what Python does, it's quite happy when you actually start typing a statement, 36 00:02:20.610 --> 00:02:22.370 it's gonna indent that for you automatically. 37 00:02:22.370 --> 00:02:25.500 It doesn't use delimiters around the blocks of code, in other words. 38 00:02:25.500 --> 00:02:29.070 Now if you actually delete the spaces, you'll actually get an error. 39 00:02:29.070 --> 00:02:33.500 So if I should delete those now, we'll actually get an error on the screen. 40 00:02:33.500 --> 00:02:34.440 Indent expected. 41 00:02:34.440 --> 00:02:38.170 And that's because python is actually saying, it needs at least one space 42 00:02:38.170 --> 00:02:43.020 to know whether this is part of the previous line's code in other words. 43 00:02:43.020 --> 00:02:45.150 So in other words, to indent it needs at least one space. 44 00:02:45.150 --> 00:02:49.480 So we could go back up there and indent that one space and it will be quite happy. 45 00:02:49.480 --> 00:02:51.090 But generally speaking it will do it for you. 46 00:02:51.090 --> 00:02:53.930 So again, on the previous line you still want to start a topping. 47 00:02:53.930 --> 00:02:57.350 When I pressed enter there, it automatically indented it four spaces, and 48 00:02:57.350 --> 00:03:00.710 if I press delete now I can get that other code back up there on the screen. 49 00:03:00.710 --> 00:03:04.660 Now both IntelliJ and Idol make it easy for you by doing that in 50 00:03:04.660 --> 00:03:08.180 default indention when appropriate, and then also continue the indention. 51 00:03:08.180 --> 00:03:13.530 So if we come back to the end of the line here, pressed enter, notice that it knows, 52 00:03:13.530 --> 00:03:16.870 because of you're already in an indented line, it's assuming that you don't need to 53 00:03:16.870 --> 00:03:19.480 be indented again, and we can actually start topping from that point. 54 00:03:19.480 --> 00:03:22.920 So in other words it's handling a lot of that for you automatically, and 55 00:03:22.920 --> 00:03:25.320 also pressing backspace, 56 00:03:25.320 --> 00:03:28.260 moves the indent when you're finished entering the code for a block. 57 00:03:28.260 --> 00:03:29.480 In other words, you can go there. 58 00:03:29.480 --> 00:03:32.590 So if you're on the next line there, and let's just say you decided, 59 00:03:32.590 --> 00:03:33.990 well, you're gonna need a little more code. 60 00:03:33.990 --> 00:03:36.150 You wanna go back and print another statement. 61 00:03:36.150 --> 00:03:41.440 If I press backspace now, I'll go back automatically four spaces as you saw there 62 00:03:41.440 --> 00:03:43.990 to the start line, I can actually start typing again. 63 00:03:43.990 --> 00:03:46.990 Now you might be asking, now what is this block you're talking about? 64 00:03:46.990 --> 00:03:50.480 Well, Python actually referred to that as being 65 00:03:50.480 --> 00:03:54.100 a piece of Python program text that is executed as a unit. 66 00:03:54.100 --> 00:03:57.810 So in the example above, you can actually say that print itself was a call to 67 00:03:57.810 --> 00:04:01.020 the printer method to be more precise, is a block. 68 00:04:01.020 --> 00:04:04.280 Now we could have included some extra lines at the same level of indentation, 69 00:04:04.280 --> 00:04:06.150 and they'd become part of the same block. 70 00:04:06.150 --> 00:04:13.003 So in other words, you could actually start typing, print, calculation complete. 71 00:04:15.943 --> 00:04:17.803 Try again, for example. 72 00:04:17.803 --> 00:04:21.710 All of these are actually part, in this case, of the same block. 73 00:04:21.710 --> 00:04:24.340 The reason for that is they're at the same level of indentation. 74 00:04:24.340 --> 00:04:27.150 Now when we get to the point in this course where we're defining our own 75 00:04:27.150 --> 00:04:30.960 functions or methods, the body of the function is a block, and 76 00:04:30.960 --> 00:04:34.030 that may in turn contain other blocks. 77 00:04:34.030 --> 00:04:38.148 Now we actually need some more complex code examples to demonstrate this fully, 78 00:04:38.148 --> 00:04:42.021 so what I'm going to do now is finish by showing you some of the preferences in 79 00:04:42.021 --> 00:04:46.318 IntelliJ to do with tabs, and then in the next lecture we'll start getting into some 80 00:04:46.318 --> 00:04:48.160 more complex code examples. 81 00:04:48.160 --> 00:04:52.800 So if you actually want to change the level of tabs or spaces 82 00:04:52.800 --> 00:04:56.720 the way to do it is to go into Preferences IntelliJ so I'm going to select that. 83 00:04:56.720 --> 00:04:59.690 Preferences and we can go into Editor. 84 00:04:59.690 --> 00:05:04.190 And because we've actually installed the Python plug-in you should 85 00:05:04.190 --> 00:05:04.980 actually see that. 86 00:05:04.980 --> 00:05:08.310 So, let me go into General and scroll down into Code Style. 87 00:05:08.310 --> 00:05:13.110 We can actually see Python, so I'm gonna select python. 88 00:05:13.110 --> 00:05:13.990 Once I've actually done that, so 89 00:05:13.990 --> 00:05:16.550 again that's Editor, Code Style, Python is how to get to it. 90 00:05:16.550 --> 00:05:18.960 You'll notice here that we've actually got tab size. 91 00:05:18.960 --> 00:05:21.560 The python folder is showing you that the tab size is 4. 92 00:05:21.560 --> 00:05:23.980 And what that means is if you press the tab key, and 93 00:05:23.980 --> 00:05:27.730 that's to get this little indentation, if you do that yourself, 94 00:05:27.730 --> 00:05:30.690 every time you do that's gonna add four spaces for you automatically. 95 00:05:30.690 --> 00:05:32.350 Now if you click on this Use tab character, 96 00:05:32.350 --> 00:05:35.790 what will happen if we do that, notice how this has changed now and 97 00:05:35.790 --> 00:05:38.760 it's flashing to show you that now this is actually a tab character. 98 00:05:38.760 --> 00:05:42.590 So what that saying is instead of actually adding four spaces for you, it's actually 99 00:05:42.590 --> 00:05:45.510 gonna add the tab character itself for you if that's what you wanna do. 100 00:05:45.510 --> 00:05:49.110 So you can play around with this, you can change the level of indentation, 101 00:05:49.110 --> 00:05:52.880 you can get yourself a little bit of difficulty doing that. 102 00:05:52.880 --> 00:05:54.910 If you do find that things aren't working. 103 00:05:54.910 --> 00:05:58.970 Come here back in editor, code style python, click on reset, or 104 00:05:58.970 --> 00:06:02.090 just make sure that your tab size and indent is both set to 4. 105 00:06:02.090 --> 00:06:04.490 And optionally you can leave that on, but 106 00:06:04.490 --> 00:06:06.420 I'll probably suggest that you actually leave it off. 107 00:06:06.420 --> 00:06:10.380 And what that means is that IntelliJ will automatically be adding spaces, as and 108 00:06:10.380 --> 00:06:11.640 when you actually need them. 109 00:06:11.640 --> 00:06:14.340 So, that's what you need to do if you do get in any difficulty. 110 00:06:14.340 --> 00:06:16.220 And just to confirm how that works again. 111 00:06:16.220 --> 00:06:17.040 Because we added four, 112 00:06:17.040 --> 00:06:20.760 every time I press Tab, you could see I'm going four characters. 113 00:06:20.760 --> 00:06:24.220 And that's because we've specified four in the preferences that the number of 114 00:06:24.220 --> 00:06:26.650 characters that were gonna move forward. 115 00:06:26.650 --> 00:06:28.300 And just one final note. 116 00:06:28.300 --> 00:06:30.990 You don't wanna get into the situation where you're mixing spaces and 117 00:06:30.990 --> 00:06:32.690 tabs when you're indenting blocks. 118 00:06:32.690 --> 00:06:34.700 And the reason for it is it can get in the difficulty and 119 00:06:34.700 --> 00:06:36.010 it can get a little bit confusing. 120 00:06:36.010 --> 00:06:38.520 And IntelliJ does a pretty good job at that. 121 00:06:38.520 --> 00:06:42.820 So if you do do that, chances are, it's gonna fix that and correct that for you. 122 00:06:42.820 --> 00:06:46.060 But if you're bringing in source code or editing in another environment and 123 00:06:46.060 --> 00:06:49.780 then bring it into IntelliJ you might run into a bit of difficulties, and 124 00:06:49.780 --> 00:06:51.220 you may have to re-fix things up. 125 00:06:51.220 --> 00:06:54.930 Also the other thing to keep in mind is, you come up here to Code. 126 00:06:54.930 --> 00:06:56.620 Try doing reformat code. 127 00:06:56.620 --> 00:06:59.320 That can actually help you if you actually select that. 128 00:06:59.320 --> 00:07:01.060 That will often actually fix errors. 129 00:07:01.060 --> 00:07:02.720 So if you've got some weird sort of formatting and 130 00:07:02.720 --> 00:07:06.270 things not working, go into other option and click reformat code and 131 00:07:06.270 --> 00:07:08.540 that can get you out of difficulties as well. 132 00:07:08.540 --> 00:07:10.960 So that's it for this video, short, sharp, and shining. 133 00:07:10.960 --> 00:07:12.510 In the next one, as I mentioned, 134 00:07:12.510 --> 00:07:16.070 we're gonna move on to some more complex examples of how this all works. 135 00:07:16.070 --> 00:07:18.770 And specifically we're gonna start looking at the if statements. 136 00:07:18.770 --> 00:07:20.184 So I'll see you in that next video.