SaralGyaan, Saral Gyaan, Saral Gyan is website which provides free tutorials of Python for absolute beginners in English and Hindi, Learn Python in Hindi, Learn Python for absolute beginners, Learn Python, bootstrap, django, git

Hari S
Author: Hari S

Chapter 5- Indentation

Chapter 5- Indentation

  • Feb. 13, 2019, 4:05 p.m.
  • 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

If __name__ == '__main__' in Python
By Hari S | 4 weeks, 1 day ago

What does if __name__ == ‘__main__‘in python means?

In python, you must have come accross the following code :-

if __name__ == '__main__': main() 

In this tutorial, we will learn in detail what does the above statement means. As per official documentation:

‘__main__‘ is the name of the ...

Read More
Use Python to send Email
By Hari S | 1 month ago

Use Python to send Emails

In this tutorial we will learn to use Python to send emails. If you are new to python, you can see our beginner’s series. In case you are having trouble installing Python, you can see our posts on installing python on MacOS ...

Read More
Chapter 7- Numbers, Conversions and Operators
By Hari S | 6 months, 2 weeks ago

In the previous chapter, we learned about various data types. From this chapter onward, we will learn in detail about each data type.


This data type stores the numeric values and is immutable. For the beginners, it is sufficient to understand that immutable objects are those which can not ...

Read More