In previous lessons,
I've already mentioned how important it is in Python to be aware of your
indentation. So we know that when we create a function like this,
that every line that comes after this definition
that is indented is going to be inside this function.
So by indented, I mean
it's shifted to the right by four spaces like this.
So if you imagine each dot as a space, then this is a single block of code.
And if we want to continue adding to this block of code inside my function,
then we would continue adding lines of code, which are indented.
So you have to almost visualize for yourself this invisible line around these
blocks of code, which are indented. Now,
if my code was written like this on the other hand,
then the block of code is in fact only this part. And this print statement will
not get triggered when this function gets triggered because it's independent
from this function because it's not indented. Now, a simple way
that I like to think about this kind of indentation is kind of like the file
structure you see when you go into finder on the Mac,
or when you go into Explorer on Windows.
If we have a function,
it's kind of like a folder and anything that goes inside the function,
for example, if I throw this file inside the function,
then you notice that it gets indented, right?
And this way it shows to you very,
very clearly that these two files print-
hello and print-world are living inside this folder,
my function. And this is the equivalent of this code.
We have this folder say, my_function, and it contains these two lines of code.
Now, on the other hand,
if I take this print world and I put it outside of the function folder,
then you can see that this is completely independent from that folder.
And it's now indented at the same level as the function.
So this is the equivalent of this code, where we've got this print-
hello being inside the
my_function and this print-world being outside and at the same indentation level
as this function definition. Now,
the indentation gets a little bit more complicated when we have other blocks of
code. So for example, the, if/elif/else statements.
They have blocks of code,
which need to be indented to be inside the block.
For loops need to be indented to be inside the loop.
And it's very important that you get used to looking at blocks of code like
this. So for example, if we were to expand our simple
my_function and add a whole bunch of code into it,
then we would have to indent all of those lines of code by four spaces
represented by the four dots here. Now, if we wanted to have
another block of code inside this if statement,
then this line has to be indented,
a further four spaces.
So this is the function block,
this is the if block,
this is the elif block and you have to be able to see all of this while just
looking at the indentation. If we wanted to represent this,
then it's almost like creating a new folder. Let's call it the
If sky == clear.
And this if block goes inside my function, so it's indented.
Now, if I wanted to have a print statement,
let's call it print-blue inside this if statement.
So this is what should be executed if this function gets called
and if the sky is equal to clear, well
then this line is indented twice.
And that is the equivalent of four spaces in our code, like this.
Now, every time I've talked about indentation,
I've been talking about spaces and actually there's two ways of creating
indentation. You don't have to just use spaces.
You can also use tabs and that's created using the tab key on your keyboard,
which can look a little bit like this.
Now there's a lot of debate around spaces and tabs where some people prefer
using spaces to indent, other people prefer tabs to indent.
And there's a lot of people arguing in the coding community.
And I think this excerpt from one of my favourite shows really demonstrates this.
Richard what's wrong? Nothing, nothing, literally. It's all good.
Come on. Oh my God. Your roommates are right. You really hate spaces.
No, no, no, no. I don't. It's not hate, hate is a strong word. Um,
truth be told I do have a slight preference for tabs,
but that's only because I'm anal
and because I prefer precision.
Well, not to pick a fight here,
but if you really care about precision why don't you use spaces?
But whatever, once it goes through the compiler, it's the same thing, right?
Yeah. Yeah. Technically, yes.
Right.
I guess I just, I just don't understand why anyone would use spaces over tabs.
Like if it's all the same,
why not just use tabs? Because it could look different on other people's
computers. Tabs create smaller file sizes. All right.
I run a compression company. Trust me.
I've devoted my life to minimalizing file sizes. It's what I do. I mean,
I do not get why anyone would use spaces over tabs. I mean,
why not just use VIM over Emacs? I do use VIM over Emacs. Oh, God help us.
Okay. Uh, you know what? I just, I don't think this is gonna work.
I'm so sorry. Uh, I mean like what,
we're going to bring kids into this world with that over the head.
Stuff's not really fair to them. Don't you think? Kids?
we haven't even slept together. And guess what?
It's never going to happen now because there is no way I'm going to be with
someone who uses spaces over tabs.
So should you be using spaces or should you be using tabs?
The age old question and the extent to which people obsess over this question,
it really shows in this 2017 Stack Overflow developer survey,
where they are asked the developers, do you use spaces or do you use tabs?
And then they compare it against their annual salary
and somehow have managed to show that the people who use spaces seem to earn a
lot more than the people who use tabs.
Now the official guide from the Python community is in fact to use spaces.
So if you click on the section, tabs or spaces,
they tell you in no uncertain words,
that spaces are the preferred indentation method.
And in fact, in Python 3,
you can't mix a code file that uses tabs and spaces for indentation in the same
file. And it also tells you that in order to indent a line of code,
it should be indented using four spaces,
so four hits of the space bar. Now,
for a lot of people, that may seem quite inefficient because I have to hit my
space bar
four times in order to achieve a single indent.
And you've seen how much we indent throughout our code. So it seems very,
very inefficient.
But luckily in most code editors,
they actually have a setting that allows you to indent using spaces,
changing the indent size to four.
And when you hit the tab key,
it will automatically insert four spaces,
which you can tell when you're trying to highlight this space,
you can see the cursor is jumping four times.
This means that you get to hit tab once and that your code is in line with the
guidance because the hind the scenes
your code editor is inserting four spaces.
So I hope that clears up a little bit on tabs versus spaces.
And I highly recommend you to head over to the course resources and click on the link
to go to the style guide for Python code and take a look through the parts about
indentation. Now in the next lesson, I've got a quick quiz for you
to test your knowledge on indentation. So for all of that and more,
I'll see you on the next lesson.