[Tutor] Indentation

Bob Gailer bgailer@alum.rpi.edu
Wed Jun 4 12:41:16 2003


--=====================_7799775==.ALT
Content-Type: text/plain; charset="us-ascii"; format=flowed

At 04:39 PM 6/4/2003 +0200, Guillaume wrote:
>I don't understand what's the meaning of this word and his roll in a prog: 
>what does
>indentation bring to a prog?

roll? did you mean role?

"Indent" means to provide some spaces on the left of a line. Why? Certain 
python statements (called Compound Statements in the langref) control the 
execution of 1 or more subsequent statements:

for i in range(3):
   print i

i = 0
while i < 3:
   i += 1
   print i
print 'Done'

if i > 3:
   print 'yes'
   a = 4
else:
   print 'no'
   a = 5

also applies to try-except; try-finally; def and class

To tell the interpreter that statements are under control of a Compound 
Statement one indents them. Thus in the while example above i += 1 and 
print i are indented two spaces, showing that they are to be repeated while 
i < 3. The only rule is that indentation must be consistent within a set of 
statements. Thus

if i > 3:
   print 'yes'
   a = 4
else:
     print 'no'
     a = 5

is acceptable, whereas

if i > 3:
   print 'yes'
     a = 4
else:
   print 'no'
     a = 5

is not.


Bob Gailer
bgailer@alum.rpi.edu
303 442 2625

--=====================_7799775==.ALT
Content-Type: text/html; charset="us-ascii"

<html>
<body>
At 04:39 PM 6/4/2003 +0200, Guillaume wrote:<br>
<blockquote type=cite class=cite cite><font face="Arial Narrow, Helvetica">I
don't understand what's the meaning of this word and his roll in a prog:
what does </font><br>
<font face="Arial Narrow, Helvetica">indentation bring to a
prog?</font></blockquote><br>
roll? did you mean role?<br><br>
&quot;Indent&quot; means to provide some spaces on the left of a line.
Why? Certain python statements (called Compound Statements in the
langref) control the execution of 1 or more subsequent
statements:<br><br>
for i in range(3):<br>
&nbsp; print i<br><br>
i = 0<br>
while i &lt; 3:<br>
&nbsp; i += 1<br>
&nbsp; print i<br>
print 'Done'<br><br>
if i &gt; 3:<br>
&nbsp; print 'yes'<br>
&nbsp; a = 4<br>
else:<br>
&nbsp; print 'no'<br>
&nbsp; a = 5<br><br>
also applies to try-except; try-finally; def and class<br><br>
To tell the interpreter that statements are under control of a Compound
Statement one indents them. Thus in the while example above i += 1 and
print i are indented two spaces, showing that they are to be repeated
while i &lt; 3. The only rule is that indentation must be consistent
within a set of statements. Thus<br><br>
if i &gt; 3:<br>
&nbsp; print 'yes'<br>
&nbsp; a = 4<br>
else:<br>
&nbsp;&nbsp;&nbsp; print 'no'<br>
&nbsp;&nbsp;&nbsp; a = 5<br><br>
is acceptable, whereas<br><br>
if i &gt; 3:<br>
&nbsp; print 'yes'<br>
&nbsp;&nbsp;&nbsp; a = 4<br>
else:<br>
&nbsp; print 'no'<br>
&nbsp;&nbsp;&nbsp; a = 5<br><br>
is not.<br><br>
<x-sigsep><p></x-sigsep>
Bob Gailer<br>
bgailer@alum.rpi.edu<br>
303 442 2625<br>
</body>
</html>

--=====================_7799775==.ALT--