All language subtitles for 059 Indentation in Python.en

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

1

In previous lessons,

2

I've already mentioned how important it is in Python to be aware of your

3

indentation. So we know that when we create a function like this,

4

that every line that comes after this definition

5

that is indented is going to be inside this function.

6

So by indented, I mean

7

it's shifted to the right by four spaces like this.

8

So if you imagine each dot as a space, then this is a single block of code.

9

And if we want to continue adding to this block of code inside my function,

10

then we would continue adding lines of code, which are indented.

11

So you have to almost visualize for yourself this invisible line around these

12

blocks of code, which are indented. Now,

13

if my code was written like this on the other hand,

14

then the block of code is in fact only this part. And this print statement will

15

not get triggered when this function gets triggered because it's independent

16

from this function because it's not indented. Now, a simple way

17

that I like to think about this kind of indentation is kind of like the file

18

structure you see when you go into finder on the Mac,

19

or when you go into Explorer on Windows.

20

If we have a function,

21

it's kind of like a folder and anything that goes inside the function,

22

for example, if I throw this file inside the function,

23

then you notice that it gets indented, right?

24

And this way it shows to you very,

25

very clearly that these two files print-

26

hello and print-world are living inside this folder,

27

my function. And this is the equivalent of this code.

28

We have this folder say, my_function, and it contains these two lines of code.

29

Now, on the other hand,

30

if I take this print world and I put it outside of the function folder,

31

then you can see that this is completely independent from that folder.

32

And it's now indented at the same level as the function.

33

So this is the equivalent of this code, where we've got this print-

34

hello being inside the

35

my_function and this print-world being outside and at the same indentation level

36

as this function definition. Now,

37

the indentation gets a little bit more complicated when we have other blocks of

38

code. So for example, the, if/elif/else statements.

39

They have blocks of code,

40

which need to be indented to be inside the block.

41

For loops need to be indented to be inside the loop.

42

And it's very important that you get used to looking at blocks of code like

43

this. So for example, if we were to expand our simple

44

my_function and add a whole bunch of code into it,

45

then we would have to indent all of those lines of code by four spaces

46

represented by the four dots here. Now, if we wanted to have

47

another block of code inside this if statement,

48

then this line has to be indented,

49

a further four spaces.

50

So this is the function block,

51

this is the if block,

52

this is the elif block and you have to be able to see all of this while just

53

looking at the indentation. If we wanted to represent this,

54

then it's almost like creating a new folder. Let's call it the

55

If sky == clear.

56

And this if block goes inside my function, so it's indented.

57

Now, if I wanted to have a print statement,

58

let's call it print-blue inside this if statement.

59

So this is what should be executed if this function gets called

60

and if the sky is equal to clear, well

61

then this line is indented twice.

62

And that is the equivalent of four spaces in our code, like this.

63

Now, every time I've talked about indentation,

64

I've been talking about spaces and actually there's two ways of creating

65

indentation. You don't have to just use spaces.

66

You can also use tabs and that's created using the tab key on your keyboard,

67

which can look a little bit like this.

68

Now there's a lot of debate around spaces and tabs where some people prefer

69

using spaces to indent, other people prefer tabs to indent.

70

And there's a lot of people arguing in the coding community.

71

And I think this excerpt from one of my favorite shows really demonstrates this.

72

Richard what's wrong? Nothing, nothing, literally. It's all good.

73

Come on. Oh my God. Your roommates are right. You really hate spaces.

74

No, no, no, no. I don't. It's not hate, hate is a strong word. Um,

75

truth be told I do have a slight preference for tabs,

76

but that's only because I'm anal

77

and because I prefer precision.

78

Well, not to pick a fight here,

79

but if you really care about precision why don't you use spaces?

80

But whatever, once it goes through the compiler, it's the same thing, right?

81

Yeah. Yeah. Technically, yes.

82

Right.

83

I guess I just, I just don't understand why anyone would use spaces over tabs.

84

Like if it's all the same,

85

why not just use tabs? Because it could look different on other people's

86

computers. Tabs create smaller file sizes. All right.

87

I run a compression company. Trust me.

88

I've devoted my life to minimalizing file sizes. It's what I do. I mean,

89

I do not get why anyone would use spaces over tabs. I mean,

90

why not just use VIM over Emacs? I do use VIM over Emacs. Oh, God help us.

91

Okay. Uh, you know what? I just, I don't think this is gonna work.

92

I'm so sorry. Uh, I mean like what,

93

we're going to bring kids into this world with that over the head.

94

Stuff's not really fair to them. Don't you think? Kids?

95

we haven't even slept together. And guess what?

96

It's never going to happen now because there is no way I'm going to be with

97

someone who uses spaces over tabs.

98

So should you be using spaces or should you be using tabs?

99

The age old question and the extent to which people obsess over this question,

100

it really shows in this 2017 Stack Overflow developer survey,

101

where they are asked the developers, do you use spaces or do you use tabs?

102

And then they compare it against their annual salary

103

and somehow have managed to show that the people who use spaces seem to earn a

104

lot more than the people who use tabs.

105

Now the official guide from the Python community is in fact to use spaces.

106

So if you click on the section, tabs or spaces,

107

they tell you in no uncertain words,

108

that spaces are the preferred indentation method.

109

And in fact, in Python 3,

110

you can't mix a code file that uses tabs and spaces for indentation in the same

111

file. And it also tells you that in order to indent a line of code,

112

it should be indented using four spaces,

113

so four hits of the space bar. Now,

114

for a lot of people, that may seem quite inefficient because I have to hit my

115

space bar

116

four times in order to achieve a single indent.

117

And you've seen how much we indent throughout our code. So it seems very,

118

very inefficient.

119

But luckily in most code editors,

120

they actually have a setting that allows you to indent using spaces,

121

changing the indent size to four.

122

And when you hit the tab key,

123

it will automatically insert four spaces,

124

which you can tell when you're trying to highlight this space,

125

you can see the cursor is jumping four times.

126

This means that you get to hit tab once and that your code is in line with the

127

guidance because the hind the scenes

128

your code editor is inserting four spaces.

129

So I hope that clears up a little bit on tabs versus spaces.

130

And I highly recommend you to head over to the course resources and click on the link

131

to go to the style guide for Python code and take a look through the parts about

132

indentation. Now in the next lesson, I've got a quick quiz for you

133

to test your knowledge on indentation. So for all of that and more,

134

I'll see you on the next lesson.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.