All language subtitles for 059 Indentation in Python.en
Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (SoranĂ®)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,760 --> 00:00:02,260
In previous lessons,
2
00:00:02,290 --> 00:00:07,150
I've already mentioned how important it is in Python to be aware of your
3
00:00:07,150 --> 00:00:12,010
indentation. So we know that when we create a function like this,
4
00:00:12,040 --> 00:00:15,850
that every line that comes after this definition
5
00:00:16,120 --> 00:00:20,590
that is indented is going to be inside this function.
6
00:00:21,130 --> 00:00:23,050
So by indented, I mean
7
00:00:23,050 --> 00:00:26,650
it's shifted to the right by four spaces like this.
8
00:00:27,280 --> 00:00:31,990
So if you imagine each dot as a space, then this is a single block of code.
9
00:00:33,580 --> 00:00:37,900
And if we want to continue adding to this block of code inside my function,
10
00:00:38,170 --> 00:00:41,650
then we would continue adding lines of code, which are indented.
11
00:00:42,430 --> 00:00:47,430
So you have to almost visualize for yourself this invisible line around these
12
00:00:47,560 --> 00:00:51,100
blocks of code, which are indented. Now,
13
00:00:51,190 --> 00:00:54,190
if my code was written like this on the other hand,
14
00:00:54,550 --> 00:00:59,550
then the block of code is in fact only this part. And this print statement will
15
00:01:02,110 --> 00:01:07,000
not get triggered when this function gets triggered because it's independent
16
00:01:07,270 --> 00:01:12,130
from this function because it's not indented. Now, a simple way
17
00:01:12,130 --> 00:01:16,840
that I like to think about this kind of indentation is kind of like the file
18
00:01:16,840 --> 00:01:19,840
structure you see when you go into finder on the Mac,
19
00:01:19,870 --> 00:01:22,390
or when you go into Explorer on Windows.
20
00:01:23,380 --> 00:01:24,910
If we have a function,
21
00:01:25,000 --> 00:01:29,950
it's kind of like a folder and anything that goes inside the function,
22
00:01:29,980 --> 00:01:33,100
for example, if I throw this file inside the function,
23
00:01:33,460 --> 00:01:36,160
then you notice that it gets indented, right?
24
00:01:36,670 --> 00:01:39,250
And this way it shows to you very,
25
00:01:39,250 --> 00:01:42,130
very clearly that these two files print-
26
00:01:42,130 --> 00:01:46,420
hello and print-world are living inside this folder,
27
00:01:46,480 --> 00:01:50,920
my function. And this is the equivalent of this code.
28
00:01:51,400 --> 00:01:56,400
We have this folder say, my_function, and it contains these two lines of code.
29
00:01:58,090 --> 00:01:59,260
Now, on the other hand,
30
00:01:59,290 --> 00:02:04,290
if I take this print world and I put it outside of the function folder,
31
00:02:05,470 --> 00:02:09,490
then you can see that this is completely independent from that folder.
32
00:02:10,030 --> 00:02:14,170
And it's now indented at the same level as the function.
33
00:02:14,800 --> 00:02:19,780
So this is the equivalent of this code, where we've got this print-
34
00:02:19,780 --> 00:02:21,820
hello being inside the
35
00:02:21,820 --> 00:02:26,820
my_function and this print-world being outside and at the same indentation level
36
00:02:28,240 --> 00:02:31,840
as this function definition. Now,
37
00:02:31,840 --> 00:02:35,920
the indentation gets a little bit more complicated when we have other blocks of
38
00:02:35,920 --> 00:02:40,240
code. So for example, the, if/elif/else statements.
39
00:02:40,420 --> 00:02:41,800
They have blocks of code,
40
00:02:42,190 --> 00:02:45,430
which need to be indented to be inside the block.
41
00:02:45,940 --> 00:02:49,240
For loops need to be indented to be inside the loop.
42
00:02:49,870 --> 00:02:54,820
And it's very important that you get used to looking at blocks of code like
43
00:02:54,820 --> 00:02:58,720
this. So for example, if we were to expand our simple
44
00:02:59,410 --> 00:03:03,640
my_function and add a whole bunch of code into it,
45
00:03:04,150 --> 00:03:08,890
then we would have to indent all of those lines of code by four spaces
46
00:03:09,070 --> 00:03:13,810
represented by the four dots here. Now, if we wanted to have
47
00:03:13,810 --> 00:03:17,740
another block of code inside this if statement,
48
00:03:18,040 --> 00:03:20,950
then this line has to be indented,
49
00:03:21,190 --> 00:03:23,470
a further four spaces.
50
00:03:24,310 --> 00:03:27,070
So this is the function block,
51
00:03:28,000 --> 00:03:30,910
this is the if block,
52
00:03:31,600 --> 00:03:36,600
this is the elif block and you have to be able to see all of this while just
53
00:03:38,230 --> 00:03:43,120
looking at the indentation. If we wanted to represent this,
54
00:03:43,180 --> 00:03:47,500
then it's almost like creating a new folder. Let's call it the
55
00:03:47,530 --> 00:03:50,770
If sky == clear.
56
00:03:52,480 --> 00:03:56,740
And this if block goes inside my function, so it's indented.
57
00:03:57,490 --> 00:04:01,810
Now, if I wanted to have a print statement,
58
00:04:01,840 --> 00:04:06,190
let's call it print-blue inside this if statement.
59
00:04:06,220 --> 00:04:10,390
So this is what should be executed if this function gets called
60
00:04:10,510 --> 00:04:13,630
and if the sky is equal to clear, well
61
00:04:13,630 --> 00:04:16,390
then this line is indented twice.
62
00:04:17,140 --> 00:04:22,140
And that is the equivalent of four spaces in our code, like this.
63
00:04:24,100 --> 00:04:26,470
Now, every time I've talked about indentation,
64
00:04:26,500 --> 00:04:31,500
I've been talking about spaces and actually there's two ways of creating
65
00:04:31,750 --> 00:04:35,080
indentation. You don't have to just use spaces.
66
00:04:35,260 --> 00:04:40,260
You can also use tabs and that's created using the tab key on your keyboard,
67
00:04:40,720 --> 00:04:42,730
which can look a little bit like this.
68
00:04:43,840 --> 00:04:48,550
Now there's a lot of debate around spaces and tabs where some people prefer
69
00:04:48,550 --> 00:04:52,570
using spaces to indent, other people prefer tabs to indent.
70
00:04:53,050 --> 00:04:56,740
And there's a lot of people arguing in the coding community.
71
00:04:57,250 --> 00:05:01,720
And I think this excerpt from one of my favorite shows really demonstrates this.
72
00:05:11,980 --> 00:05:15,940
Richard what's wrong? Nothing, nothing, literally. It's all good.
73
00:05:20,640 --> 00:05:24,750
Come on. Oh my God. Your roommates are right. You really hate spaces.
74
00:05:25,050 --> 00:05:28,920
No, no, no, no. I don't. It's not hate, hate is a strong word. Um,
75
00:05:29,460 --> 00:05:32,490
truth be told I do have a slight preference for tabs,
76
00:05:32,520 --> 00:05:34,360
but that's only because I'm anal
77
00:05:34,360 --> 00:05:37,590
and because I prefer precision.
78
00:05:40,390 --> 00:05:42,160
Well, not to pick a fight here,
79
00:05:42,160 --> 00:05:45,580
but if you really care about precision why don't you use spaces?
80
00:05:46,360 --> 00:05:50,050
But whatever, once it goes through the compiler, it's the same thing, right?
81
00:05:51,230 --> 00:05:53,300
Yeah. Yeah. Technically, yes.
82
00:05:53,630 --> 00:05:54,463
Right.
83
00:05:59,150 --> 00:06:03,380
I guess I just, I just don't understand why anyone would use spaces over tabs.
84
00:06:03,380 --> 00:06:04,850
Like if it's all the same,
85
00:06:04,920 --> 00:06:08,300
why not just use tabs? Because it could look different on other people's
86
00:06:08,300 --> 00:06:11,480
computers. Tabs create smaller file sizes. All right.
87
00:06:11,480 --> 00:06:13,160
I run a compression company. Trust me.
88
00:06:13,160 --> 00:06:17,090
I've devoted my life to minimalizing file sizes. It's what I do. I mean,
89
00:06:17,090 --> 00:06:21,440
I do not get why anyone would use spaces over tabs. I mean,
90
00:06:21,440 --> 00:06:26,390
why not just use VIM over Emacs? I do use VIM over Emacs. Oh, God help us.
91
00:06:26,930 --> 00:06:31,220
Okay. Uh, you know what? I just, I don't think this is gonna work.
92
00:06:31,310 --> 00:06:33,080
I'm so sorry. Uh, I mean like what,
93
00:06:33,260 --> 00:06:35,720
we're going to bring kids into this world with that over the head.
94
00:06:35,720 --> 00:06:37,820
Stuff's not really fair to them. Don't you think? Kids?
95
00:06:37,820 --> 00:06:39,800
we haven't even slept together. And guess what?
96
00:06:40,010 --> 00:06:42,920
It's never going to happen now because there is no way I'm going to be with
97
00:06:42,920 --> 00:06:45,440
someone who uses spaces over tabs.
98
00:06:46,260 --> 00:06:50,520
So should you be using spaces or should you be using tabs?
99
00:06:50,580 --> 00:06:55,580
The age old question and the extent to which people obsess over this question,
100
00:06:56,430 --> 00:07:01,050
it really shows in this 2017 Stack Overflow developer survey,
101
00:07:01,140 --> 00:07:05,070
where they are asked the developers, do you use spaces or do you use tabs?
102
00:07:05,490 --> 00:07:08,010
And then they compare it against their annual salary
103
00:07:08,070 --> 00:07:13,070
and somehow have managed to show that the people who use spaces seem to earn a
104
00:07:13,320 --> 00:07:15,480
lot more than the people who use tabs.
105
00:07:16,830 --> 00:07:21,830
Now the official guide from the Python community is in fact to use spaces.
106
00:07:23,010 --> 00:07:25,890
So if you click on the section, tabs or spaces,
107
00:07:26,220 --> 00:07:28,350
they tell you in no uncertain words,
108
00:07:28,350 --> 00:07:31,650
that spaces are the preferred indentation method.
109
00:07:32,220 --> 00:07:34,620
And in fact, in Python 3,
110
00:07:34,980 --> 00:07:39,980
you can't mix a code file that uses tabs and spaces for indentation in the same
111
00:07:41,190 --> 00:07:45,720
file. And it also tells you that in order to indent a line of code,
112
00:07:45,750 --> 00:07:49,110
it should be indented using four spaces,
113
00:07:49,350 --> 00:07:53,520
so four hits of the space bar. Now,
114
00:07:53,520 --> 00:07:57,270
for a lot of people, that may seem quite inefficient because I have to hit my
115
00:07:57,270 --> 00:07:58,103
space bar
116
00:08:00,060 --> 00:08:03,480
four times in order to achieve a single indent.
117
00:08:03,600 --> 00:08:08,070
And you've seen how much we indent throughout our code. So it seems very,
118
00:08:08,130 --> 00:08:09,420
very inefficient.
119
00:08:10,110 --> 00:08:13,410
But luckily in most code editors,
120
00:08:13,680 --> 00:08:18,680
they actually have a setting that allows you to indent using spaces,
121
00:08:19,830 --> 00:08:22,860
changing the indent size to four.
122
00:08:23,430 --> 00:08:25,140
And when you hit the tab key,
123
00:08:25,500 --> 00:08:28,920
it will automatically insert four spaces,
124
00:08:29,220 --> 00:08:33,030
which you can tell when you're trying to highlight this space,
125
00:08:33,030 --> 00:08:36,120
you can see the cursor is jumping four times.
126
00:08:36,990 --> 00:08:41,990
This means that you get to hit tab once and that your code is in line with the
127
00:08:42,840 --> 00:08:45,510
guidance because the hind the scenes
128
00:08:45,510 --> 00:08:48,900
your code editor is inserting four spaces.
129
00:08:50,430 --> 00:08:54,420
So I hope that clears up a little bit on tabs versus spaces.
130
00:08:54,750 --> 00:08:59,400
And I highly recommend you to head over to the course resources and click on the link
131
00:08:59,430 --> 00:09:03,660
to go to the style guide for Python code and take a look through the parts about
132
00:09:03,660 --> 00:09:08,190
indentation. Now in the next lesson, I've got a quick quiz for you
133
00:09:08,370 --> 00:09:12,720
to test your knowledge on indentation. So for all of that and more,
134
00:09:13,110 --> 00:09:14,340
I'll see you on the next lesson.
12997
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.