DEV Community

Abhishek Prajapati
Abhishek Prajapati

Posted on

Importance Of Indentation In PYTHON | apcode

Hi, I am Abhishek. Today, in this article I am going to talk about what is indentation and how important it is in Python programming language. In other programming languages, curly braces are used instead of an indentation.

What is the indentation ?

In Python indentation is nothing but the amount of whitespace before a statement. Indentation is used in Python to define the scope of a code block.

What is code block ?

In a python program a set of statements that has a special means can be considered as code block. For example-

code-image1

Here we have two code blocks-- an if-block and an else-block.

Now both the print statement should have the same amount of space before them to be in the respective code blocks.

If the number of space changes, an error will be generated by the Python interpreter.
code-image-2

Output:

error-image

NOTE: You can have any number of spaces before the statements in a code block. But for one block the number of spaces should be the same. And at least one space is required to define a code block.

I hope you got a clear idea about how and why indentation is used in the Python programming language. Thank You !

Top comments (0)