/* */ Click to Join Live Class with Shankar sir Call 9798158723

indentation in python ?


Understanding indentation is very important in python indentation refers to spaces that are used in the beginning of a statement. The statements with same indentation belong to same group called a suite. By default python uses four spaces but it can be increased or decreased by the programmers. Consider the statements given below.


Example of Indentation
        If x==1:
----print(‘a’)
----print(‘b’)
----if k==2:
--------print(‘c’)
--------print(‘d’)
Print(‘end’)

 
  • Hear :
    If x==1:
    Print(‘end’)
    Belong to same group as they do not have any spaces before them so, after executing the if statement, python interpreter goes to the next statement i.e print(‘end’) even if the statement “ if x==1 is not executed , interpreter will execute print(‘end’) statement as it is the next executable statement in our program.

  • In the next level: the following statements are typed with 4 spaces before them and hence they are at the same level(suite).
    print(‘a’)
    print(‘b’)
    if y==2:
    these statements are inside if x==1 statement hence if the condition is ture is satisfied then the above 3 statement are executed . thus the third statement if y==2 is executed only if x==1 is ture
  • at the next level we can find the following statement
    print(‘c’)
    print(‘d’)
    these two statements are typed total eight spaces before them and hence they belonging to the same group (or suite) since they are inside if y==2 satement they are executed only if condition y==2 is true . hear we are given aexampe with table
    Output when x==1 y==0 Output when n==1 k==2
    A A
    B B
    End C
    . D