[pythonvis] More on ... Re: Re: Indentation of code

  • From: "Richard Dinger" <rrdinger@xxxxxxxxxx>
  • To: <pythonvis@xxxxxxxxxxxxx>
  • Date: Sat, 10 May 2014 07:56:22 -0700

Sorry, I forgot some of our members are new to programming.  I will try to 
explain below about indentation in Python.

A program is organized into a hierarchy of statements.  I think help files are 
the closest example of a hierarchical organization that non programmers have 
encountered.  So first, take a look at how help files are organized.

When you first open a help file, you are at level 0 and are shown a list of 
topics and books.  You can arrow up and down through the list at level 0.  If 
you press right arrow on Book A that book opens and displays a sublist of one 
or more topics and books that are part of Book A.  This sublist is indented to 
level 1.  The sublist is indented to indicate that it is part of Book A.  The 
structure can continue to as many levels as needed.

Programs are organized in a somewhat similar manner.  A Python program is a 
sequence of statements that instruct the computer on how to solve some problem. 
 Some Python statements are like topics and do not enclose other statements.  
And some are like books that do enclose other statements.

Here is a very simple code fragment:

if x < 0:
    print ‘x is negative’

In the above code, x is a variable that contains some value.  The if statement 
tests to see if x is less than zero.  If it is true a message is printed.  
Since the print statement is enclosed by the if statement, the print is 
indented two spaces.

Adding another if inside the first adds another indentation level:

if x < 0:
  if y < 0:
    print ‘x and y negative’

Indentation must be consistent within a given file.  The most common 
indentation standards are two spaces or four spaces per level.  I prefer two 
because it does not use up page width as fast.  If you are using a Braille 
display, you may want to use a single space for similar reasons.

JAWS can be configured to announce the level every time it changes and will 
speak ‘left edge’, ‘tab1’, ‘tab2’ and so forth.  JAWS can alternatively play a 
note or other indications.  EdSharp can be set to speak change in indentation 
‘in 1’, ‘out 1’.  Both are silent if there is no change in indentation.


Richard

From: Charles Rivard 
Sent: Thursday, May 08, 2014 10:05 AM
To: pythonvis@xxxxxxxxxxxxx 
Subject: [pythonvis] Re: Indentation of code

Are there different levels on indentation?  If so, wouldn't the program run 
differently when different lengths of indentation is used?  Or are you speaking 
only of representation for reading purposes rather than for actual code 
writing?  Thanks.

---
Be positive!  When it comes to being defeated, if you think you're finished, 
you! really! are! finished!
  ----- Original Message ----- 
  From: Richard Dinger 
  To: pythonvis@xxxxxxxxxxxxx 
  Sent: Wednesday, May 07, 2014 9:41 PM
  Subject: [pythonvis] Indentation of code

  A close relative to Sharon’s code format issue is how many spaces to use in 
code examples.  This may be controversial.

  Although four spaces is the most common indentation, I suggest two spaces per 
indentation level will work better especially for email.  Two and four spaces 
are both common standards, but four spaces eats too much page territory.

  Richard

Other related posts:

  • » [pythonvis] More on ... Re: Re: Indentation of code - Richard Dinger