WEBVTT

0
00:00.440 --> 00:08.390
In previous lessons, I've already mentioned how important it is in Python to be aware of your indentation,

1
00:08.390 --> 00:15.920
so we know that when we create a function like this, that every line that comes after this definition

2
00:15.920 --> 00:20.810
that is indented is going to be inside this function.

3
00:20.810 --> 00:26.930
So by indented I mean it's shifted to the right by four spaces, like this.

4
00:26.930 --> 00:32.120
So if you imagine each dot as a space then this is a single block of code.

5
00:33.200 --> 00:39.350
And if we want to continue adding to this block of code inside my function, then we would continue

6
00:39.350 --> 00:41.900
adding lines of code which are indented.

7
00:42.140 --> 00:48.860
So you have to almost visualize for yourself this invisible line around these blocks of code which are

8
00:48.860 --> 00:49.700
indented.

9
00:50.390 --> 00:57.980
Now, if my code was written like this, on the other hand, then the block of code is in fact only

10
00:57.980 --> 01:05.720
this part and this print statement will not get triggered when this function gets triggered, because

11
01:05.720 --> 01:10.340
it's independent from this function, because it's not indented.

12
01:11.030 --> 01:17.420
Now, a simple way I like to think about this kind of indentation is kind of like the file structure

13
01:17.420 --> 01:22.610
you see when you go into Finder on the Mac, or when you go into Explorer on Windows.

14
01:23.090 --> 01:29.930
If we have a function, it's kind of like a folder and anything that goes inside the function.

15
01:29.930 --> 01:36.320
For example, if I throw this file inside the function, then you notice that it gets indented right?

16
01:36.320 --> 01:43.910
And this way it shows to you very, very clearly that these two files print-hello and print-world are

17
01:43.910 --> 01:46.430
living inside this folder,

18
01:46.430 --> 01:51.110
My Function. And this is the equivalent of this code.

19
01:51.110 --> 01:57.350
We have this folder say, my_function and it contains these two lines of code.

20
01:57.890 --> 02:05.780
Now on the other hand, if I take this print-world and I put it outside of the function folder, then

21
02:05.780 --> 02:12.740
you can see that this is completely independent from that folder, and it's now indented at the same

22
02:12.740 --> 02:14.420
level as the function.

23
02:14.540 --> 02:19.730
So this is the equivalent of this code where we've got this 

24
02:19.730 --> 02:20.360
print("Hello")

25
02:20.360 --> 02:28.550
being inside the my_function and this print("World") being outside and at the same indentation level as

26
02:28.550 --> 02:30.350
this function definition.

27
02:31.460 --> 02:36.570
Now the indentation gets a little bit more complicated when we have other blocks of code.

28
02:36.600 --> 02:43.770
So for example, the if, elif, else statements, they have blocks of code which need to be indented

29
02:43.770 --> 02:49.470
to be inside the block, for loops need to be indented to be inside the loop.

30
02:49.530 --> 02:55.440
And it's very important that you get used to looking at blocks of code like this.

31
02:55.560 --> 03:03.830
So for example, if we were to expand our simple my_function and add a whole bunch of code into it,

32
03:03.830 --> 03:10.490
then we would have to indent all of those lines of code by four spaces, represented by the four dots

33
03:10.490 --> 03:10.940
here.

34
03:11.180 --> 03:20.210
Now, if we wanted to have another block of code inside this if statement, then this line has to be

35
03:20.210 --> 03:23.600
indented a further four spaces.

36
03:24.080 --> 03:27.230
So this is the function block.

37
03:27.740 --> 03:31.100
This is the if block.

38
03:31.280 --> 03:34.190
This is the elif block.

39
03:34.460 --> 03:39.860
And you have to be able to see all of this while just looking at the indentation.

40
03:40.790 --> 03:46.490
If we wanted to represent this, then it's almost like creating a new folder.

41
03:46.520 --> 03:56.930
Let's call it the if sky == clear, and this if block goes inside my function, so it's indented.

42
03:57.290 --> 04:06.140
Now if I wanted to have a print statement let's call it print blue inside this if statement.

43
04:06.140 --> 04:13.610
So this is what should be executed if this function gets called and if the sky is equal to clear, well

44
04:13.610 --> 04:21.570
then this line is indented twice and that is the equivalent of four spaces in our code.

45
04:21.840 --> 04:22.950
Like this.

46
04:23.790 --> 04:28.620
Now every time I've talked about indentation, I've been talking about spaces.

47
04:28.620 --> 04:32.790
And actually there's two ways of creating indentation.

48
04:32.790 --> 04:35.130
You don't have to just use spaces,

49
04:35.130 --> 04:37.260
you can also use tabs.

50
04:37.260 --> 04:42.830
And that's created using the Tab key on your keyboard, which can look a little bit like this.

51
04:43.520 --> 04:50.210
Now there's a lot of debate around spaces and tabs where some people prefer using spaces to indent,

52
04:50.240 --> 04:52.760
other people prefer tabs to indent,

53
04:52.760 --> 04:56.960
and there's a lot of people arguing in the coding community.

54
04:56.960 --> 05:01.910
And I think this excerpt from one of my favorite shows really demonstrates this.

55
05:11.710 --> 05:12.250
[Silicon Valley TV show excerpt captions] Richard: Huh.
Winnie: Richard,

56
05:12.250 --> 05:13.000
what's wrong?

57
05:13.150 --> 05:14.020
Richard: Nothing.

58
05:14.290 --> 05:14.860
Nothing.

59
05:14.860 --> 05:15.370
Literally.

60
05:15.370 --> 05:16.090
It's all good.

61
05:20.590 --> 05:21.400
Come on. Huh. [laughs]

62
05:22.000 --> 05:22.660
Winnie: Oh my God.

63
05:23.050 --> 05:24.040
Richard: I' m sorry. 
Winnie: Your roommates were right.         

64
05:24.040 --> 05:25.150
You really hate spaces.

65
05:25.180 --> 05:27.400
Richard: No no no no, I don't...it's not hate.

66
05:27.430 --> 05:28.450
Hate is a strong word.

67
05:28.570 --> 05:35.320
Truth be told, I do have a slight preference for tabs, but that's only because I'm anal and because

68
05:35.320 --> 05:37.810
I prefer precision.

69
05:40.240 --> 05:45.820
Winnie: Well, not to pick a fight here, but if you really care about precision, wouldn't you use spaces?

70
05:46.300 --> 05:47.440
But whatever.

71
05:47.470 --> 05:50.230
Once it goes through the compiler, it's the same thing, right?

72
05:51.040 --> 05:52.120
Richard: Yeah, yeah.

73
05:52.150 --> 05:53.500
Technically, yes.

74
05:58.900 --> 06:03.310
I guess I just...I just don't understand why you...anyone would use spaces over tabs.

75
06:03.310 --> 06:06.220
Like, if it's all the same, why not just use tabs?

76
06:06.370 --> 06:08.920
Winnie: Because it could look different on other people's computers.

77
06:08.920 --> 06:11.110
Richard: Tabs create smaller file sizes.

78
06:11.110 --> 06:11.410
All right.

79
06:11.410 --> 06:12.670
I run a compression company.

80
06:12.670 --> 06:15.460
Trust me, I've devoted my life to minimalizing file sizes.

81
06:15.460 --> 06:16.810
It's what I do.

82
06:16.810 --> 06:21.040
I mean, I do not get why anyone would use spaces over tabs.

83
06:21.040 --> 06:23.410
I mean, why not just use Vim over Emacs?

84
06:23.800 --> 06:25.330
Winnie: I do use Vim over Emacs.

85
06:25.330 --> 06:26.530
Richard: Oh, God help us!

86
06:26.710 --> 06:29.350
Okay, uh, you know what?

87
06:29.350 --> 06:31.270
I just I don't think this is going to work.

88
06:31.270 --> 06:32.200
I'm so sorry.

89
06:32.200 --> 06:33.100
I mean, like what?

90
06:33.250 --> 06:35.740
We're going to bring kids into this world with that over their heads.

91
06:35.740 --> 06:37.270
That's not really fair to them, don't you think?

92
06:37.270 --> 06:39.070
Winnie: Kids, we haven't even slept together.

93
06:39.070 --> 06:39.880
Richard: And guess what?

94
06:39.880 --> 06:44.530
It's never going to happen now, because there is no way I'm going to be with someone who uses spaces

95
06:44.530 --> 06:45.460
over tabs. [End of the excerpt]

96
06:46.060 --> 06:50.560
So should you be using spaces or should you be using tabs?

97
06:50.560 --> 06:57.400
The age old question, and the extent to which people obsess over this question really shows.

98
06:57.400 --> 07:04.240
In this 2017 StackOverflow developer survey where they asked the developers, do you use spaces or do

99
07:04.240 --> 07:05.170
you use tabs?

100
07:05.170 --> 07:11.350
And then they compared it against their annual salary and somehow have managed to show that the people

101
07:11.350 --> 07:15.670
who use spaces seem to earn a lot more than the people who use tabs.

102
07:16.540 --> 07:22.960
Now, the official guide from the Python community is in fact to use spaces.

103
07:22.960 --> 07:29.350
So if you click on the section tabs or spaces, they tell you in no uncertain words that spaces are

104
07:29.350 --> 07:31.840
the preferred indentation method.

105
07:31.840 --> 07:40.480
And in fact, in Python 3, you can't mix a code file that uses tabs and spaces for indentation

106
07:40.480 --> 07:41.830
in the same file.

107
07:41.830 --> 07:49.120
And it also tells you that in order to indent a line of code, it should be indented using four spaces.

108
07:49.120 --> 07:52.000
So four hits of the space bar.

109
07:53.020 --> 07:58.180
Now, for a lot of people, that may seem quite inefficient because I have to hit my spacebar,

110
07:59.920 --> 08:03.460
four times in order to achieve a single indent.

111
08:03.460 --> 08:09.430
And you've seen how much we indent throughout our code, so it seems very, very inefficient.

112
08:09.970 --> 08:19.510
But luckily in most code editors, they actually have a setting that allows you to indent using spaces,

113
08:19.510 --> 08:28.210
changing the indent size to four, and when you hit the tab key, it will automatically insert four

114
08:28.210 --> 08:32.950
spaces, which you can tell when you're trying to highlight this space.

115
08:32.950 --> 08:36.250
You can see the cursor is jumping four times.

116
08:36.760 --> 08:44.410
This means that you get to hit tab once, and that your code is in line with the guidance, because

117
08:44.410 --> 08:49.030
behind the scenes, your code editor is inserting four spaces.

118
08:50.170 --> 08:54.520
So I hope that clears up a little bit on tabs versus spaces.

119
08:54.520 --> 09:00.310
And I highly recommend you to head over to the Course Resources and click on the link to go to the Style

120
09:00.310 --> 09:04.840
Guide for Python code, and take a look through the parts about indentation.

121
09:04.840 --> 09:10.720
Now in the next lesson, I've got a quick quiz for you to test your knowledge on indentation.

122
09:10.720 --> 09:14.350
So for all of that and more, I'll see you in the next lesson.