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.
But before we do, if you installed Python version 1.5.2 (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:
If you are using MS Windows there is yet another option in the form of PythonWin which you can download. This gives access to all the Windows low level programming functions and importantly, a very good alternative to IDLE. It only works in Windows but in my opinion is slightly superior to IDLE. On the other hand IDLE is very new and in subsequent releases may overtake PythonWin. Whatever happens, it's nice to have a choice!
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.
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
Now recall that we can type long strings by enclosing them in triple quotes? Lets 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?
Points to remember |
---|
|
Previous  Next  Contents
If you have any questions or feedback on this page
send me mail at:
alan_gauld@xoommail.com