Skip to main content

Indentations in Python

 




INDENTATION : Indentation is nothing but giving proper spaces they are like {} braces don’t need to mention any {} braces to indicate a set of code or code so our compiler or interpreter need to understand from where to stop and where to continue again .our compilers or interpreters don’t know the order in which the statements of the code should be executed so we use indentations to specify compilers to execute them in order

** we all know that python is very easy and a simple language which means we don’t need to mention datatypes and syntaxes also will be very easy to understand when  compared to other languages but if you don’t give proper indentation  then it shows us hell, so we need to use proper indentation or else you will be ending up seeing that that “INDENTATION ERROR”  message .

Ø  Actually, indentation is nothing but giving spaces before the statements

Ø  Actually, we use tab or space for indentation in python, by default python uses 4 spaces which means a tab and it is not that we should only use 4 spaces it is based on us, we can use any number of spaces but throughout the code the spaces you give to one statement should be same with other statements also.

For example: let us consider a simple code snippet

 

1. p = input (“Indentation in python is very important: yes or no?”)

2. If p == yes:

3.     print (“true”)

4.  else: 

5.    print (“false”)

 

In the above code first line 1 gets executed and then it goes to line  2 and checks the condition now if the given condition is true then only  the 3rd line will  be executed or line  3 to be printed in c or java we use braces so that the third line  comes under statement but here if we move that statement three to four spaces (tab) then python  will consider that after the line 2 only line  3 should be executed and after that it comes to line 4 and if line 2 is wrong and line  5 will be executed if line  4 is corrected so again we use 4 spaces so that line 5 will be printed it means we are saying to compiler that only if its right then it should be printed or else 4 need to skip that line.

      

Comments

Contact form

Name

Email *

Message *