Python program indentation

What is the importance of identation in python??

It basically makes the compiler aware of which block or statement the given piece of code is under. For example there might be a for loop, and within that loop there might be another loop or if-else statements. So if we indent the code, the compiler will understand which block the code is under. It’s just the way python is. In programming languages like C, indentation is optional, but recommended to ensure readability.

Indentation is very important in python…not only it makes the code easily readable but it’s necessary in python otherwise you’ll get Indentation error. Indentation actually conveys the program structure. Different languages have different techniques for indentation for example in C / C++ a block of code is represented by curly braces
whereas in python indentation is uniform…
a example to show the importance of indentation is shown below:

x = int(input(“Input a number”))
if x%2==0:

   print("number is divisible by 2")


   print("number is not divisible by 2")

this indentation is really important…Programming is just experimenting…so try to change the indentation and write without proper indentation, automatically you will get error…

Hope it helps…!!:grinning:


Basically, when you write a program we need to define its structure on how the code should be executed when its run. Each language has its own way of defining structure for different blocks of code, for example in C++ we use curly braces ( { … } ) to define structure of its components, Similarly in python we use indentations to define each components body and structure and that’s a rule. we can’t use brackets of any type to define structure in python.

( Although you can use indentation in C++ but thats majorly because to increase the readability of the code, and for easy navigation to particular part of code. )

Just for getting a small idea about indentations for each block of code that we write in python, refer to this image below :


Hope this helps! :+1:

Just to add it bit more, for indentation in python 4 spaces is the ideal one.It’s not like it is mandatory but ideal.

Hope it helps!!:grinning:

1 Like

Indentation is a way in python programming to write codes. For eg. In C/c++ we put curly brackets “{ }” and between them we write our code or loop statements. In python through indentation compiler understands the code’s start and end point. Also it makes the reading the code easy

Indentation is considered good-practice to improve readability of any language. So from the reader’s point of view indentation makes the code easier to read and understand.

Now, most languages make indentation purely a matter of style and they use block markers (such as keywords or parentheses) to tell the compiler(or interpreter) where a block of code starts and stops.I thinks this clears your doubt :slightly_smiling_face:

Hello priyank,
In c,c++ like programming languages we use braces such as {,} to differentiate between blocks.
Instead in python ,we use indentation to differentiate between blocks.
so, that the compiler can understand it .

Indentation is used to make the interpreter aware about the block of statements that needs to be executed.The common word used for indentation is blank spaces.It is been used while writing functions,conditional statements,looping statements etc.If proper indentation is not given then,on compiling the program an indentation error will be displayed.

In c programming instead of indentation curly brackets are used to represent a block of statements.Also indentation improves the readability of the code.

In python, indentation is required for indicating what block of code a statement belongs to.
for example, while i<10:
in the above example, the print statement is inside while loop to indicate this indentation is used.