The Personal Web Pages of Chris X. Edwards

XED's Python Lessons for Beginners

--------------------------

Lesson Four - If Whitespace Is What You Need

After reading the previous lesson you should be enthusiastic about controlling your software's behavior by using invisible syntax. It is a neat idea. The question is how is this done?

As with other languages, Python has a concept of code blocks. Other languages tend to isolate these blocks by using a beginning and ending marker. For instance Bash uses "if" to start an if block and "fi" to end one. Isn't that cute? Python uses indentation as an explicit declaration of what block lines are a member of. Let's look at a comparison of a Python program with its equivalent in Bash and Perl.
 # Bash                
 if [ -n $MAYBE ]; then
 echo $MAYBE           
 fi                    
 echo $YES              
    
# Perl
if ($MAYBE) {
print $MAYBE;
}
print $YES;
# Python
if MAYBE:
____print MAYBE
print YES

Don't worry about the specifics of these code fragments. The important thing to notice is that Python has a fundamentally different mechanism for signaling that code is part of some block. The underscores shown in the Python code are mandatory whitespace. Most commonly they are 4 spaces. There is some flexibility with that, but 4 spaces is suggested.

Perhaps you have experience with Bash or Perl and the examples look funny because there is no indentation. Typical "good programmers" would have included the same indentation as a Python program, but Python just makes this mandatory and is then able to derive quite a bit of meaningful syntax from it. Here are some examples.
# This is ok
if MAYBE:
_print MAYBE
print YES
# This is not ok
if MAYBE:
____print MAYBE
__print YES
# This is ok
if MAYBE:
____print MAYBE
____print YES

The first example shows that the amount of indentation is immaterial to the Python interpreter. The critical point is that it must simply be consistent. Therefore example 2 produces an error. Is the "print YES" statement part of the conditional block or is it something else? It's not following the rules so it makes no sense. In the last example, the "print YES" statement is now clearly part of the conditional block executed only if MAYBE exists (as with the preceeding line).

Since I've shown how the if command works we can demonstrate the whitespace requirements of Python while illustrating the rest of the if statement functionality.
if LIKELY:
    print 'I thought so.'
    if FUN:                  # A new if statement at a new level.
        print "Let's do it!" # Executes only if LIKELY and FUN.
    else:                    # Refers to the if in the same column.
        print "Let's think about it!"
elif POSSIBLE:               # Back to the original if's level.
    print "That's interesting."
elif IMPOSSIBLE:
    print 'This will never happen.'
else:
    print 'Probability unknown.'
print 'This always happens.'

The if statement checks to see if LIKELY is set to a value. If so, it executes its block. If not, it moves down to see if POSSIBLE is set. It tries IMPOSSIBLE in the same way. If none of those are set, then the else block is executed. The last line is at the same indentation level as the if statement and its subcomponents, so it is not part of the if statement block. It always gets executed.

Now an interesting subtle problem occurs with this type of scheme. In other languages, it is possible to have a conditional not do anything. Because the block is defined by special symbols or words, you can just omit the contents and it will be assumed that nothing is to occur.

In Python like the other languages, some commands (if, elif, else, and many others) require a block. The problem with Python is that if the block is empty, then it simply looks like an indentation error.
if CONDITIONS_GOOD:
print "Let's go!"  # This is an error!!! Indentation is now required.

The answer is this strange looking, but necessary command:
if IMPOSSIBLE:
    pass  # This means do nothing, but the indentation requirement is ok.
else:
    print 'It could be done.'
The pass command is often handy during development when you haven't written the final functionality yet.

--------------------------
Previous LWM Home Python Lessons Index Next
This page was created with only free, open-source, publicly licensed software.
This page was designed to be viewed with any browser on any system.
Chris X. Edwards ~ March 2006