Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Getting Python Code Indentation Right

Indentation is very important in Python and PEP 8 lays out a couple of rules for it:

  • Use four consecutive spaces to indicate indentation
  • Prefer spaces over tabs.

This lesson will show you how to apply these two rules and the different ways you can use indentation to improve readability.

00:00 Now we get to talk about indentation. Python is different from a lot of languages in that indentation is used to group statements together. You don’t see curly brackets following the function call, so indentation is how scope is defined. PEP 8 defines an indentation as four consecutive spaces, and to use spaces over tabs.

00:20 While we could dive into the tabs versus spaces debate, you can compromise by adjusting the settings of your text editor to output four spaces instead of a tab character when you hit the Tab key.

00:31 You can see that my editor is set up to faintly show four space characters every time I hit the Tab key, and it’ll also delete four consecutive space characters if I hit the Delete key. This way you won’t annoy yourself by hitting Space four times every time you need to indent something.

00:49 A larger issue can arise if you use a mix of tab characters and spaces. In Python 2, you could add a -t flag before your script, which would give warnings if it detected a mix of tabs and spaces.

01:04 You could also go in and add a -tt flag.

01:09 That would actually cause an error. The error is helpful because it tells you exactly where the inconsistencies are present so you can go fix them.

01:20 Python 3 kicks it up a notch and doesn’t allow a mix of tabs or spaces and you’ll get an error no matter how you run your script. Okay, let’s cover indenting after a line break.

01:54 Because there’s an implied line break here, it helps to align the next line with the delimiter, like so. If you have an if statement that spans multiple lines, however, this can be a problem. Let’s take a look.

02:18 Because if is two characters and then you have the space and the opening parentheses, this can be confusing to read because the indented line will line up with the rest of the code.

02:28 PEP 8 also has two different ways to handle this. One way is to go ahead and add a comment after you close the statement.

02:39 This gives you a natural break and it’s easy to follow. Another way is just to add some more indentation. Let’s get rid of that and add another indent right there.

02:49 And this does the same thing. Another method of indenting is to use a hanging indent, which is an indent for every line following the first line. If you go to this function up here, you could see that this would be kind of like a hanging indent.

03:03 But be careful if you’re going to do this with a function, to not have any arguments on the first line. These should instead be brought down to the next line, like so. You may notice that now this is the same issue that was present with the if statement, so you can solve it the same way by adding another indent here.

03:21 Finally, when dealing with lists and dictionaries, it can be tricky to know where to put the closing bracket.

03:40 Like before, you have two options here. Either have the closing bracket lined up with the first character of the first line that opened it, or you can indent it to line up with the first non-whitespace character of the previous line. And that’s all there is to indentation!

03:58 This is one of the first things we’ve covered that offers options at almost every step, so the key here is to be consistent. You should pick one method and stick to it as much as possible—except, of course, if you’re on a project that’s using the other method. In the next video, we’ll talk about comments. Thanks for watching.

W Patrick Jones on April 22, 2019

Nothing on multi-line string assignements? That and really long SQL statements are the ones that always confuse me on where to indent.

Joe Tatusko RP Team on April 22, 2019

I usually bring the indent to the where the opening quotation character is on the next line to keep it in a nice block.

For the long SQL statements I’ll generally start a multiline string with nothing in the first line, then have everything non-indented to keep it grouped like it would be outside of Python (if I can’t use something like SQLAlchemy to be more Pythonic)

Abby Jones on June 25, 2019

I’ve set up my VIM env to limit tabs to 4 spaces. Is that still okay?

Joe Tatusko RP Team on June 26, 2019

Of course! I have all my editors set to turn pressing the tab key into 4 spaces. No need to bash the spacebar 4 times everytime you need to indent. The big thing is what character is actually saved in the file, 4 spaces or one tab

Become a Member to join the conversation.