[Tutor] RE: Tutor digest, Vol 1 #2 - 4 msgs

Martijn Faassen M.Faassen@vet.uu.nl
Tue, 09 Mar 1999 10:59:06 +0100

David Ascher wrote:
> On Mon, 8 Mar 1999, Martijn Faassen wrote:
> > Alan Gauld wrote:
> > > Explain the colon!!!
> >
> > I agree a beginner tutorial should have a good explanation of 'block
> > structure' in programming, though. In Python this is marked by the : and
> > an indented block, but saying 'explain the colon' mystifies me. :)
> Let me try to emphasize that I'd much rather see explanations than
> defensive reactions.  If the questions are there, they indicate a need
> which isn't met.

Whoops. I didn't mean to send out a 'defensive reaction'. I genuinely
was puzzled by 'explain the colon' and it took me a second more careful
reading to get what was being talked about. Also it seemed that Alan
Gauld understood the concept just fine now, so I didn't venture an
explanation in response (though I agreed there ought to be one).

Independently (I haven't read David Ascher's response yet) here's my
attempt at block structure, if not for Alan then for posterity. :)

In an imperative computer language like Python and many others (don't
bother about what 'imperative' means right now), the concept of 'block
structure' is very important. A block of code is a sequence of
statements in a programming language that 'belongs together'. Generally
this means that whenever the first statement the block is executed, all
the other statements in the block will be executed as well, one by one.
There are exceptions to this rule in Python; I'll get to some of these

A lot of programming language mark blocks by saying 'okay, here a block
begins', and 'here a block ends' by using special characters such as '{'
and '}'. In Python, the block structure is defined by:

a) a colon announcing that here a new block starts. Compare this to the
use of the colon in natural language, see for instance the colon in the
previous paragraph.

b) indentation (from the left side of the screen, using spaces (usually
4) or 'tab' characters). All statements in the block have the same
indentation, except those of blocks that are contained, which have
greater indentation.

An example (not in Python) of a block containing 3 statements:

my block:

A block may also contain other blocks inside them. This would look like

my block:
    my subblock:

So, blocks can be used to structure a program. What would be the use of
this structuring capability? In Python, we can define functions, with
the statement 'def'. The statement 'def' announces a function definition
follows. First after 'def' Python expects a function name, then a list
of arguments marked by '(' and '), and finally a colon, announcing the
block that follows.

For instance:

# function without arguments (so the space between '(' and ')' is empty,
# to keep things simple
def print_stuff():
    print "Hello world."
    print "We are explaining block structure."
    print "We're done now."

Once you have this function and Python has processed it, it can be used
anywhere else in the program, just by calling it like this: 


This then makes Python execute the statements in the function
'print_stuff', and the result will be:

    Hello world.
    We are explaining block structure.
    We're done now.

A program that runs on straight forever can't do much; it can't respond
to changing circumstances in the outside world (the user or the data it
may be reading from disk or from a network). A program needs to be able
to behave differently in different circumstances. For this purpose we
have the 'if' statement, another great example of the use of block
structure. 'if' is always followed by a description of a condition (for
instance, the value of 'a' is smaller than that of 'b'). If the
condition happens to be the case (if the condition is 'true', in
programmer speak), then the block that follows is executed. If the
condition isn't the case or false, then the block that follows is
skipped and not executed. An example:

a = 3
b = 4
if a < b:
    print "'a' is smaller than 'b'."
    print "We put in another statement just to show we can."

If we execute this program, of course a (3) will always be smaller than
b (4), so we'll always get the output:

    'a' is smaller than 'b'.
    We put in another statement just to show we can.

(as you may have figured, a block can also just be just a single
statement; we don't need the second 'print' statement here :)

This is a trivial example. A more complicated example would not always
behave the same, as the value of a and b would depend on other things
(for instance they are the result of some calculation, involving data
that may be different each time this program is executed).

We can put blocks in blocks, as we saw before. An example (also of a
function with two arguments, called 'a' and 'b'):

def is_a_smaller_than_b(a, b):
    if a < b:
        print "Yes, a is smaller than b"
    print "We are done."

If you call this function like this:

    is_a_smaller_than_b(3, 4)

gets you the folllowing output:

    Yes, a is smaller than b
    We are done.

If we call it like this:

    is_a_smaller_than_b(100000, 1)

We get this output:

    We are done.

As the 'if' block was not executed.

Blocks are therefore a very useful and even essential way to structure
your Python programs. It is easy to recognize a block in Python; they're
always announced by a colon (:) and they are always indented.

Of course, I'm trying to explain too many basic things at once, so I may
have left posterity completely confused. If you happen to be completely
confused, feel free to ask questions about anything I talked about (or
things I didn't talk about :).