What is identation error in python ? Why we use tab?


#1

Can any one explain the identation errors in detail , and why we use tab in place of space bar ?


#2

Any block of code after a colon is indented by four spaces, in the instance of an indentation error it means your code should have been indented on a specific line but is not. :slight_smile:


#3

I can't understand it , this i have google also and get the same typical definition .
Please explain in easy way..


#4

Okay let's say I had a small blok of code that checks if I am older than 10,

age = 15

if age > 10:
    print "Yes"
else:
    print "No"

Because of the two colons after the if condition and else statement the IDE will automatically start 4 spaces ahead on a new line like what you are seeing it has done to my print statement. But, if I reverted the indentation and put all my code on the same indentation level,

age = 15

if age > 10:
print "Yes"
else:
print "No"

This would result in an indentation error because any code after a colon needs to be indented. :slight_smile:

Note: However if / elif / and else: statements must be on the same indentation level,

if condition:
    # code
elif condition:
    # code
else:
    # default condition

#5

Now i understand it completely..
Thanx for replying


#6

You're welcome. :slight_smile:


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.