More Sequences and Other Things

What will we cover?
We introducec a new tool for entering Python programs. We look at the use of variables to store information for future use. Also how to combine a sequence of commands to perform a task.

OK, Now we know how to type simple single entry commands into Python and have started to consider data and what we can do with it. Let's see what happens when we type multiple commands into Python.

The joy of being IDLE

But before we do, if you installed Python version 1.5.2 or later (it tells you when you start up) you should find a tool called IDLE installed by default. This is basically a command prompt in a window. It has several advantages over using the DOS version:

There is a full tutor on using IDLE on the Python web site under the IDLE topic. There is also a gentler one which covers some of the same Python things we are discussing here as well, at Danny Yoo's web site. I'd suggest you start with Danny's one then once you feel more confident with IDLE go back to the official one at python.org.

If you are using MS Windows there is yet another option in the form of PythonWin which you can download as part of the win32all package. This gives access to all the Windows MFC low level programming functions and importantly, a very good alternative to IDLE. Pythonwin only works in Windows but in my opinion is slightly superior to IDLE. On the other hand IDLE is standard with Python so more people tend to use it and it works on most platforms. Whatever happens, it's nice to have a choice!

A quick comment

One of the most important of programming tools is one that beginners often feel is useless on first acquaintance - comments. Comments are just lines in the program which describe what's going on. They have no effect whatsoever on how the program operates, they are purely decorative. They do have an important role to play - they tell the programmer what's going on and more importantly why. This is especially important if the programmer reading the code isn't the one who wrote it, or, its a long time since he/she wrote it. Once you've been programming for a while you'll really appreciate good comments. From now on I'll be commenting the code fragments that I write. Gradually the amount of explanatory text will diminish as the explanation appears in comments instead.

Every language has a way of indicating comments. In BASIC it's REM at the beginning of a comment. Everything after the REM is ignored:

REM print "This never gets printed"
print "This gets printed"

You might recognise REM if you have ever written any MSDOS batch files, since they use the same comment marker.

Most BASICs also allow you to use ' instead of REM which is easier to type but harder to see. The choice is yours.

Python and Tcl both use a # symbol as their comment marker. Anything following a # is ignored:

v = 12     # give v the value 12
x = v*v    # x is v squared

Incidentally this is very bad commenting style. Your comment should not merely state what the code does - we can see that for ourselves! It should explain why it's doing it:

v = 3600    # 3600 is num of secs in an hour
s = t*3600  # t holds elapsed time in hours, so convert to secs

These are much more helpful comments.

Sequences using variables

Now either in IDLE or at the DOS or Unix command window Python prompt try typing this:

>>> v = 7
>>> w = 18
>>> x = v + w    # use our variables in a calculation
>>> print x

What's happening here is that we are creating variables ( v, w, x ) and manipulating them. Its rather like using the M button on your pocket calculator to store a result for later use.

We can make this prettier by using a format string to print the result:

>>> print "The sum of %d and %d is: %d" % (v,w,x)

One advantage of format strings is that we can store them in variables too:

>>> s = "The sum of %d and %d is: %d"
>>> print s % (v,w,x)   # useful if printing same output with different values

Order matters

By now you might be thinking that this sequence construct is a bit over-rated and obvious. You would be right in so far as its fairly obvious but its not quite as simple as it might seem. There can be hidden traps. Consider the case where you want to 'promote' all the headings in an HTML document up a level:

Now in HTML the headings are set by surrounding the text with
<H1>text</H1> for level 1 headings,
<H2>text</H2> for level 2 headings,
<H3>text</H3> for level 3 headings and so on.

The problem is that by the time you get to level 5 headings the heading text is often smaller than the body text, which looks odd. Thus you might decide to promote all headings up one level. Its fairly easy to do that with a simple string substitution in a text editor, substitute '<H2' with '<H1' and '</H2' with '</H1' and so on.

Consider though what happens if you start with the highest numbers - say H4 -> H3, then do H3 -> H2 and finally H2 -> H1. All of the headings will have moved to H1! Thus the order of the sequence of actions is important. The same is just as true if we wrote a program to do the substitution (which we might well want to do, since promoting headings may be a task we do regularly).

A Multiplication Table

I'm now going to introduce a programming exercise that we will develop over the next few chapters. The solutions will gradually improve as we learn new techniques.

Recall that we can type long strings by enclosing them in triple quotes? Let's use that to construct a multiplication table:

>>> s = """
1 x 12 = %d
2 x 12 = %d
3 x 12 = %d
"""     # be careful - you can't put comments inside 
>>>     # strings, they'll  become part of the string!
>>> print s % (12, 2*12, 3*12)

By extending that we could print out the full 12 times table from 1 to 12. But is there a better way? The answer is yes, let's see what it is.

Points to remember
  • IDLE is a cross platform development tool for writing Python programs.
  • Comments can make programs clearer to read but have no effect on the operation of the program
  • Variables can store intermediate results for later use

Previous  Next  Contents


If you have any questions or feedback on this page send me mail at: alan.gauld@btinternet.com