Skip to 0 minutes and 4 secondsPython and indentation are like siblings who always fight. They like each other because they're so closely related, but they are always causing each other problems. Indentation problems are something that almost everybody experiences the first time they use Python. But if you follow the rules, they're easy to fix. So let's take some time to talk about why indentation is important, how it works, and some top tips to avoid problems. Indentation is important to Python, because it's how you determine scope. Scope is how Python knows what code belongs to what part of the program. Now, let's recap by looking at some code that you've already created. This program contains one indent between the if statement and the print("hello").

Skip to 0 minutes and 51 secondsThis indentation tells Python that the code print("hello") should only be run when the if statement is true. And any code is indented under the if will form part of that if statement's scope.

Skip to 1 minute and 6 secondsIf I were to type in some more lines of code under the if, it would also form part of that if statement's scope. But the third line doesn't, because it's not indented as part of the if. Now let's have a look at this program. There's an additional indent after the else and before the print("sorry I dont understand this"). Just like the if statement's scope, the print statement indented under the else is part of the else statement's scope and not part of the ifs. Now, the final print command, the one says print("bye"), is not indented under the if or the else. So it's not part of either scope.

Skip to 1 minute and 56 secondsWithout indentation, Python won't be able to determine what code to run as part of the if or the else. Indentation is a common theme throughout Python, and one you will use a lot in the coming weeks. You can create indents using the Tab key on your keyboard. And most IDEs will also try and help you indent your code automatically by adding indents when you press Enter after a colon. Now, something to watch out for is the number of spaces that your IDE uses to create an indent. For example, in Mu, it's four, but in Trinket, it's only two. Now either way, it doesn't really matter how many spaces you use. Python doesn't care, as long as you're consistent.

Skip to 2 minutes and 44 secondsSo if you start using four spaces for your indent, you always need to use four spaces. For example, in this snippet of code, we can see that the first indent has four spaces, but the second indent has only two. And you can see that the code doesn't line up. Now, we can see when we run this program that actually we get an indentation error. Python is telling us your indentation is wrong. So my top tips for indentation are use the Tab key on your keyboard to keep your indent spaces consistent. Don't fight with your IDE. Use the number of spaces it gives you. If it gives you four, use it. If it gives you two, use it.

Skip to 3 minutes and 32 secondsAnd let it indent the code for you. And finally, keep your code tidy. If it's not lined up, like this example, it's not going to work.

Indentation

Python and indentation are like siblings who always fight. They like each other because they are so closely related, but they are always causing each other problems.

Indentation problems are something almost everyone experiences the first time they use Python. However, if you understand and follow the rules they are very easy to fix. So let’s take some time to talk about:

  • why indentation is so important in Python
  • how it works and how to create indents
  • some tips to avoid problems

Indentation is important to Python because it’s how you determine scope. Scope is how Python knows what code belongs to what part of the program.

Indent?

Lets recap by looking at some code you have already created:

phrase = input("Talk to me > ")
if phrase == "hi":
    print("hello")
print("bye")

This small program contains one indent: it’s before the code print("hello") and it is under if phrase == "hi":

if phrase == "hi":
    print("hello")

The indentation tells Python that the code print("hello") should only be run if phrase == "hi" is True. Any code that is indented under the if will form part of that if statement’s scope.

if phrase == "hi":
    print("hello")
    print("this is also part of the if's scope")
print("not part of the if's scope")

Have a look at the program below, where there is another indent: after the else before print("sorry I dont understand this").

phrase = input("Talk to me > ")
if phrase == "hi":
    print("hello")
else:
    print("sorry I dont understand this")
print("bye")

Using the same scope rules as the if statement, the print statement indented under the else is part of that else statement’s scope.

else:
    print("sorry I dont understand this")

The final print("bye") is not indented under the if or the else, so is not part of the if or the else. That makes sense as you want this line to run no matter what phrase the user entered.

Without the use of indentation, Python would not be able to determine what code to run as part of the if or the else. This is a common theme throughout Python and one you will use a lot in the coming weeks.

How it works

To create an indent using Mu or Trinket, use the TAB key on the keyboard. This will create a space and indent your cursor. Mu and Trinket (and most code editors) will also try and help you indent your code, automatically putting in indents when you press Enter after a :.

Although the TAB key will insert an indent, Mu and Trinket don’t insert an actual TAB character for the indent: like most code editors, they will use spaces. If you are using Mu you will notice that Mu creates four spaces for an indent, whereas if you are using Trinket, it only uses two spaces. Most of the time you don’t need to worry about this though – all you need to do is press TAB.

Zoom into the editor in Mu, with the indentation after an 'else' statement highlighted with a red box, and labelled '4 spaces'

Zoom into the editor in trinket, with the indentation after an 'else' statement highlighted with a red box, and labelled '2 spaces'

Python doesn’t mind whether you use two spaces or four spaces (or any other number of spaces) as long as you are consistent. For example if you start off using four spaces for an indent, then you should always use four spaces.

In the example below, four spaces have been used for the first indent, but only two for the second, and you can see that as a result the code doesn’t “line up”.

phrase = input("Talk to me > ")
if phrase == "hi":
    print("hello")
  print("how are you?")

Running this program will result in an error stating IndentationError: unindent does not match any outer indentation level.

Note: All Python requires is that you are consistent.

Tips for indentation

  1. Use the Tab key on your keyboard. This will ensure you are using same number of spaces per indent.
  2. Don’t fight with your editor: let it indent code for you where possible, and use the number of spaces it gives you.
  3. Keep your code tidy! If it’s not lined up it’s not going to work.

Share this video:

This video is from the free online course:

Programming 101: An Introduction to Python for Educators

Raspberry Pi Foundation