Udit Vashisht
Author: Udit Vashisht

Chapter 5- Indentation

Chapter 5- Indentation

  • 2 minutes read
Chapter 5- Indentation

Like many other languages, python is also a block-structured language.


Block is basically a group of statements in a code script. A block in itself can have another block or blocks, hence making it a nested block. Now, to differentiate one block of code from another, we need some kind of separator or indication etc. Programming languages like C++, Java etc. use braces {} to define hence differentiate a block of code.


But Python is different and uses indentation to define a block of code. Indentation is basically the distance from the very beginning of the line in term of whitespaces. You can choose as many whitespaces as you wish, but that should be consistent throughout your code. Generally, people use 4 white-spaces and most of the code-editors come with a preset of 4 white-spaces on the press of the tab button (but that setting is editable). So, basically, the statements which are at an equal distance from the beginning of the line will be part of one block. e.g.

This belongs to block 1
    This belongs to block 2
        This belongs to block 3
    This belongs to block 2
This belongs to block 1
        This belongs to block 3

Indentation is otherwise used in many languages for code-readability but in Python in addition to making the code more readable, it defines a block of code too.

In addition to indentation, all the conditional statements, loops, functions and other structure introducing blocks ends with a colon followed by indentation:-

while true:

You must practice indentation and pay special attention to it because in the beginning one error which you will get the most will be IndentationError:-

IndentationError: unindent does not match any outer indentation level


Most of the code-editors and even IDLE are programmed to auto-indent your code i.e. if you will write a conditional statement followed by a colon and hit return/enter, it will auto-indent the next line for you.

Table of Contents

Chapter 4 - Print function

Chapter 6 - Data Types & Variables

Related Posts

Chapter 2 - Quick setup
By Udit Vashisht

It doesn’t matter which OS you are using, Python3 comes with multiple OS compatibility.

Install on Windows PC

Follow the steps in this post to install on Windows PC.

Install on MacOS/Linux

Follow the steps in this post to install on MacOS or Linux.

Ditching IDLE


Read More
Pie Charts in Python | Matplotlib Tutorial in Python | Chapter 3
By Udit Vashisht

Matplotlib Tutorial in Python

Chapter 3 | Creating Pie Charts in Python

In this chapter, we will learn to create Pie Charts in Matplotlib in Python. Pie Charts are an intiutive way of showing data, wherein each category is shown as a portion of the pie. Let ...

Read More
How to host and schedule your python script on PythonAnywhere?
By Udit Vashisht

In our last post here, we learned how to create a python script which will automatically delete your messages from gmail account based on a query.

The complete ready-to-use code can be found here.

Follow the steps in the above tutorial to download the ‘credentials.json’ file and ...

Read More