[Tutor] Indentation

Alan Gauld alan.gauld@blueyonder.co.uk
Wed Jun 4 15:25:06 2003


> I don't understand what's the meaning
> of this word 

Indentation is simply the measure of how far in from 
the left side a line starts

Thus, this line has zero indentation - it starts at the left margin

    This line has an indent of 4 spaces from the left margin

    This line has an indent of one tab space.

How the above shows up will dpend on whether you use Rich Text 
or HTML mail formatting. If you use plain text format it will 
look OK, otherwise pretty meangless!

I'll Try again using dashes instead of spaces:

NO Indent, at ythe margin
----This has 4 character indent
--------This has eight character Indentation.

Think of the word dent. The text is "dented in " from the side.

> and his roll in a prog: what does indentation bring to a prog?

It helps organise things visually. By grouping related 
statements together with the same level of indentation we
can better understand the flow of the program. For example:

if Color in (red, blue, green):
----Print Color
----Display shape
else:
----Print 'Unknown color'

It is easy to see which statements get executed if the 
color is one that we are interested in.

Now most languages treat indentaton as just a visual clue, 
the interpreter ignores it, however Python actually reads 
the indentation, much the same way as we do, and uses it 
to group statements together into what is called a block.

Therefore in Python it is very important to get your 
indentation consistent, because if a line is even one 
extra space indented Python will think it is seeing a 
new block of code and get confused, printing an error 
as a result.

HTH,

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld