Indentation is very important to understand when writing code in Python. Without proper white space indenting in your Python code, you will end up seeing a lot of “IndentationError” messages, as well as your code failing to compile. Indentation is however a simple concept to understand, so let’s take a look at how to use Indentation in Python properly.
NOTE: This tutorial requires that Python is already installed, as well as an IDE (Integrated Development Environment), such as PyCharm. It’s also recommended that you have a basic understanding of How to Use Python if this is your first time learning Python.
With that said, let’s get started with this free Python Indentation tutorial.
What is Python Indentation?
Indentation refers to adding sufficient white space before a statement (a line of code).
But why is this even necessary when using Python?
Python uses indentation to better understand your code, as well as for you to or someone else reading your code, to have a much easier time identifying sections of code by browsing over it.
Think of Indentation as page numbers in a book. If all page numbers were to be removed, it would be very difficult to find a certain paragraph in the book.
Without indentation in the code, Python would not know which statement to execute next or which statement belongs to which block, which would lead to an “IndentationError“.
Indentation is most important when it comes to “if else”, “While loops” and “For Loops”, but is found elsewhere as well.
Let’s take a look at a basic if statement example:
if 8 > 3:
print("Eight is greater than three!")
#>> IndentationError: expected an indented block after 'if' statement on line 1
Because there is no indentation, this immediately results in an “IndentationError: expected an indented block” error.
Single Space Indentation
Now, let’s try this again, but this time add a single space before the print statement:
if 8 > 3:
print("Eight is greater than three!")
#>> Eight is greater than three!
Perfect, it works! It successfully executed “Eight is greater than three!”
Multi-space Space Indentation using TAB
Instead of using Spacebar to make a single indentation, we can also use TAB to make 4x single spaces”
if 8 > 3:
print("Eight is greater than three!")
#>> Eight is greater than three!
This too will give the same working result of “Eight is greater than three!”
We can therefore conclude that Python needs at least one indentation space, in order for this if statement to work, but can also make use of TAB to automatically create 4 spaces for us.
It’s up to you as a programmer, which you prefer to use. I personally prefer TAB, as it’s a more obvious indentation, compared to a single space, which can be overlooked.
Python Requires Uniform Indentation
Whichever spacing method you choose, please note that you must keep continuity in the indentation, throughout your code.
For example, you cannot mix a single space with a 4x TAB space:
if 8 > 3:
print("Eight is greater than three!")
print("You’re right, eight is greater than three!")
#>> print("You’re right, eight is greater than three!")
#>> IndentationError: unexpected indent
If you execute this code, only half of it will work while the other half will get an “IndentationError: unexpected indent” error.
In order to get it working correctly, its needs to have identical indentation as follows:
if 8 > 3:
print("Eight is greater than three!")
print("You’re right, eight is greater than three!")
#>> Eight is greater than three!
#>> You’re right, eight is greater than three!
This time it worked perfectly, printing out both lines of code successfully.
Now, let’s take a look at a slightly more advanced “if else” statement:
if 3 > 8:
print("Eight is greater than three!")
else:
print("Eight is NOT greater than three!")
#>> Eight is NOT greater than three!
The following code will work correctly, showing us that we must not also add a TAB indentation before the else condition.
Code Blocks & Indentation
With all the above in mind, there is a pattern to indentation, which can refer to as Code Blocks.
Take a look at the following “if else” statements:
apples = 6
oranges = 9
if apples < oranges:
print("We need more apples!")
else:
print("We need more oranges!")
print("Ok, I'll go get some")
#>> We need more apples!
#>> Ok, I'll go get some
If we break them down into Code blocks it would like like this:
# Code Block 1 begins
# Code Block 1 continues
# Code Block 1 continues
# Code Block 2 begins
# Code Block 1 continues
# Code Block 2 continues
# Code Block 1 continues
Here is a commented version of the above combined for you to easily understand
apples = 6 # Code Block 1 begins
oranges = 9 # Code Block 1 continues
if apples < oranges: # Code Block 1 continues
print("We need more apples!") # Code Block 2 begins
else: # Code Block 1 continues
print("We need more oranges!") # Code Block 2 continues
print("Ok, I'll go get some") # Code Block 1 continues
This is how the Python interpreter sees code, showing us that all statements with the same indentation spacing, belong to the same Code Block.
Other programming languages like C, C++, Java use braces { } to define the Code Blocks, whereas Python uses indentation to highlight the blocks of code, making it a critical concept to understand for your code to work properly in Python.
Python Indentation Conclusion
I hope you enjoyed this tutorial on how Python Indentation works. We first looked into why indentation is important, not only to make it easier for us and others to read the code, but also that Python requires it to properly function.
Python Indentation can be either a single space using SPACE BAR, or 4 spaces using TAB. It can however not be a combination of the two, so whichever you choose needs to be consistent throughout your code, especially in Python Data Types such as if/else statements. Using TAB to alight other elements such as your Python Comments is also very useful in keeping your code clean and easy to read.
If you enjoyed this content and would like others to benefit from it as well, make sure to share it on your favorite Social Media platform, using the share buttons below.