Explain Indentation in python?


Indentation :-

indentation is a code separation process , all statements and block od codes are not seperated by curlibraces in python . python recognize spaces between statements , these spaces are called indentation.

  • Indentation refers to the spaces at the beginning of a code line.
  • Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important.
  • Python uses indentation to indicate a block of code.

Image result for indentation in python

Python uses whitespace (spaces and tabs) to define program blocks whereas other languages like C, C++ use braces ({}) to indicate blocks of codes for class, functions or flow control. The number of whitespaces (spaces and tabs) in the indentation is not fixed, but all statements within the block must be the indented same amount. In the following program, the block statements have no indentation.


Importance of indentation

Indentation is one of the most important aspects in any programming domain. But this is often the most neglected part during the programming and the developers are mostly reluctant to follow it. In my experience some of the most complex projects are become uncontrollable due to ONLY bad code indentation and wrong programming practice. We need to understand that programming is not only to solve the problem but it is also an art of coding. And the most important part is that, as a developer you must be passionate about coding and then only you will be able to include the art of coding in your development work.

  • Easier to read
  • Easier to understand
  • Easier to modify
  • Easier to maintain
  • Easier to enhance

The purpose of code indentation and style is to make the program more readable and understandable. It saves lots of time while we revisit the code and use it. A style guide provides a road map which the developer should follow and implement in coding. So in a group of developers, all the code generated will be of consistent in nature and reusable by any coder/developer. The worst part of coding is not to follow the style and indentation guide when the project is in the mid way of development. Some time it happens due to time pressure and many other factors. But we need to keep in mind that good code is always better even if we need to give some more time because in the long run it will save a lot of time. But unfortunately, it is often forgotten and the trouble comes at a later stage. And in that stage it is very difficult to rectify and recover.

As we know, a program is written only once during coding but it is read many times at a leter stage. Following are some future points when we refer the code again.

  • During reading an understanding the program
  • During code debugging
  • During adding new code to the existing program
  • During updating the existing program


Examples Based on Indentation


if age < 18:
  print("You can easily learn python in RLtute!")

Python will give you an error if you skip the indentation:


Syntax Error:

if 5 > 2:
print("Five is greater than two!")
The number of spaces is up to you as a programmer, but it has to be at least one.


again error :

if age > 18:
if age <18:

You have to use the same number of spaces in the same block of code, otherwise Python will give you an error:


Syntax Error:

if 5 > 2:
 print("Five is greater than two!")
        print("Five is greater than two!")

Related Topics