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 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 favorite shows really demonstrates this.
[Silicon Valley TV show excerpt captions] Richard: Huh. Winnie: Richard,
what's wrong?
Richard: Nothing.
Nothing.
Literally.
It's all good.
Come on. Huh. [laughs]
Winnie: Oh my God.
Richard: I' m sorry. Winnie: Your roommates were right.
You really hate spaces.
Richard: No no no no, I don't...it's not hate.
Hate is a strong word.
Truth be told, I do have a slight preference for tabs, but that's only because I'm anal and because
I prefer precision.
Winnie: Well, not to pick a fight here, but if you really care about precision, wouldn't you use spaces?
But whatever.
Once it goes through the compiler, it's the same thing, right?
Richard: Yeah, yeah.
Technically, yes.
I guess I just...I just don't understand why you...anyone would use spaces over tabs.
Like, if it's all the same, why not just use tabs?
Winnie: Because it could look different on other people's computers.
Richard: 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?
Winnie: I do use Vim over Emacs.
Richard: Oh, God help us!
Okay, uh, you know what?
I just I don't think this is going to work.
I'm so sorry.
I mean, like what?
We're going to bring kids into this world with that over their heads.
That's not really fair to them, don't you think?
Winnie: Kids, we haven't even slept together.
Richard: 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. [End of the excerpt]
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 really shows.
In this 2017 StackOverflow developer survey where they asked the developers, do you use spaces or do
you use tabs?
And then they compared 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 spacebar,
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
behind 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 in the next lesson.