13dec05 This tutorial was inspired by a few pages of text in David Beazley "Python Essential Reference" New Riders, 2001. Typography: A python script just ends. So we add a line of dashes. The first line of a script should at least be a comment identifying the name of the script. A commandline entry is preceeded by a $ (for the bash prompt) and end in a second $ which stands for pressing the (ENTER) key. Footnotes are numbered thus [1]. The symbol >>> means the following is entered into the python interpreter, whose prompt is a >>>. We end with a <<< to indicate the end of the line (EOL), which is also entered with the (ENTER)-key. Most commonly, the ascii newline character (written "\n") is written into the code by pressing (ENTER). ---------------------- # hello.py print "Hello World" ---------------------- Write[1] the above textfile and execute it thus $ python hello.py $ ---------------------- #!/sw/bin/python print "Hello World" ---------------------- If you name this file "foo" and make it executable by unix thus $ chmod 755 foo $ then $ foo $ will execute it. ---------------------- #interest.py p = 1000 #principal r = 0.05 n = 3 y = 1 while y <= n: p *= 1 + r print y, p y += 1 ---------------------- The loop counter y is iniatialized to 1 and incremented at the end of the loop body. The loop body is entered as long as y does not exceed n. Note the : is followed by newline, and then an indentation of at least one space is needed to set off the block. The newline and indentation rules of python replaces the {} block structure of C and its daughters. ---------------------- #interest2.py p=1000; r=0.05; n=5; y=1; while y <= n: p *= 1 + r print "%3d %0.2f" % (y,p) #like sprintf(" %3d %0.2f", y, p); y += 1 ---------------------- The newline can be replaced by a ; except after the : preceeding an indented block. Python drops some punctuation which you must remember to put back when translating a python script into C code. ---------------------- #interest3.py p=1000; r=0.05; n=5; y=1; while y <= n: p *= 1 + r ; print "%3d %0.2f" % (y,p) ; y += 1 ---------------------- Passing between vertical (easiy to edited) to horizontal (easy to read) is not as convenient in python as in C. ---------------------- #interest4.py p=1000; r=0.05; n=5; y=1; f=open("tabul","w") #for writing !! while y <= n: p *= 1 + r print >>f, "%3d %0.2f" % (y,p) y += 1 f.close() ---------------------- Informally speaking, "open" is a function that creates a file named "tabul", which is writeable. It returns a "handle" we have called "f". In OOPspeak, f is an instance of the open-class. Curiously, "print >>f" says "print to the file above" rather than using its name "tabul". This is an instance of the "haddock's eyes" principle in CS. The handle can do things (OOPspeak: the object has methods) such as "f.close()" the file. What alse f can do you'll have to discover elsewhere.[2] ---------------------- #interest5.py p=1000; r=0.05; n=5; y=1; f=open("tabula","w") #for writing !! while y <= n: p *= 1 + r f.write( "%3d %0.2f" % (y,p) ) y += 1 f.close() ---------------------- In keeping with haddock's eyes, instead of "print >> ..." we can use the method "f.write(...)". But $cat tabul$ and $cat tabula$ shows the difference in how the output file is formatted (with/without extra \n). ---------------------- tabul reads thus ---------------------- 1 1050.00 2 1102.50 3 1157.62 4 1215.51 5 1276.28 ---------------------- tabula reads thus ---------------------- 1 1050.00 2 1102.50 3 1157.62 4 1215.51 5 1276.28 ---------------------- ---------------------- #readfile.py f= open("tabula"); lin = f.readline(); while lin: print lin, ; lin = f.readline() f.close() ------------------------------------- The above is a python file reader. Since it works (try it!), we can decipher that readline() returns a line, as long as there is a line to be returned. If you leave off the ", ; lin=f.readline()" you can see what it's for. Don't forget to kill the endless loop with a CTL-C. Use the >>>CTL-D<<< to get out of python. ------------------------------------- Footnotes: ------------------------------------- [0] Readme: (I made these notes reading Beasley). interpreted >>>print "hello" <<< deferred #hello.py print "hello" automatic put #!/sw/bin/python in the first line and 755 it words: "sys.exit()" or "raise SystemExit" end python on last line EOL can sometimes be replaced by a ;, except after the : of a loop (EOL=end of line, which, depending on the unix system can be different control-letters.) commas have mysterious actions, sometimes they are necessary. string theory is very similar to BASIC with operators: as long as you start counting with the 0th character s="0123456" the fifth character in sss sss[5] is 5 the first 3 sss[3:] is 3456 after the 3rd sss[:3] is 12 exclusive slice sss[3:5] is 34 What do I need to get the Jumping Jack to work? Line positioning and overwriting. [1] While it is an excellent idea to learn vim along with python it is possible to read and write textfiles using the unix "cat" command. To read a text file foo just $ cat foo $ meaning: enter "cat foo" on the commandline. You can even write a file line by line (but you can't edit, not even as you type) by abusing the cat command thus $ cat > foo$ meaning: take the keyboard input and write it into a file named "foo". To stop doing this, enter ctl-D. You'll need VIM to edit. [2] Since most people ignore the advice "read the documentation" the makers of python have all sorts of tricks to facilitate learning their language on the hoof, as it were. >>>help(open)<<< will reveal quite a bit. First, "open()" is an alias for the class "file()". Read the second line "file(name[,mode[,buffereing]])-> file object" something like this: You must tell file() the name of a file. If that file doesn't exist yet, naming it makes it happen. Then it is optional to add, following a comma, the mode (later revealed to be 'r','w','a') And optional, once again, to add something about buffering. Then calling the class returns an instance of it, the file-object. Then, the help page goes into considerable detail. It is insructive to read it yourself.