Python Patrol Tutorial
Copyright (C) 1999 by Steve Litt, All rights reserved. Material provided
as-is, use at your own risk.
Python Basics
Check that Python is Installed
Do this command:
python
You should see a message and prompt similar to this:
Python 1.5.1 (#1, Sep 3 1998, 22:51:17) [GCC 2.7.2.3] on linux-i386
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>>
|
If that message does not appear, do what it takes to get python installed
and accessible.
Hello World
Create a file called hello.py. It should look like this:
Be absolutely sure not to have any spaces to the left of the word
"print". Python uses indentation sensitive syntax, and will fail if
there are any spaces or tabs to the left of the print statement.
Run it with this command:
python hello.py
It should print the words "Hello World" to the screen. If it doesn't, make
sure python the file you made is called hello.py, it's in the current directory,
it contains the text shown in the box above, and contains no space before
the command.
Make it an Executable
For many uses, including CGI, your program must be self-executing. First,
add a line to the top of the file telling the OS to run the program with
Python, and telling it where to find Python, as shown below:
#!/usr/bin/python
print "Hello World"
|
The top line assumes that your Python executable is in /usr/bin. If
it's somewhere else, substitute that path.
Now make the file executable with this command:
chmod a+x hello.py
Finally, run the command like this:
./hello.py
It should print the words "Hello World" to the screen. If not, troubleshoot.
Notice that a semicolon is NOT required at the end of the line. Semicolons
are necessary ONLY to separate multiple commands on the same line. Otherwise,
the end of the line ends the command.
Add a Variable
Insert code to add variable myname, and print it.
#!/usr/bin/python
myname = "Steve Litt"
print "Hello World"
print myname
|
This should print a line saying "Hello World", followed by a line saying
"Steve Litt" to the screen. If not, troubleshoot.
Notice these things:
-
It prints newlines automatically. If you wish to suppress the newline printing,
end the print line with a comma, in which case it prints an extra space
but no newline.
-
To avoid the space at the end of the print, do string concatenation.
Add Comments
#!/usr/bin/python
myname = "Steve Litt"
#This is a comment
# So is this
#And this is a comment
print "Hello World" #This is an inline comment
print myname
|
Note the following:
-
Comments begin with a pound sign, hash symbol, #
-
Comments alone on their own lines are not subject to indentation sensitive
syntax.
-
Inline comments are preceeded to a pound sign, and are to the right of
an executable.
Simple String Concatenation
#!/usr/bin/python
longname = "Anderson"
print longname
longname = longname + "berg"
print longname
|
This program should print a line saying "Anderson", followed by another
line saying "Andersonberg". If not, troubleshoot.
Python can do incredible things with strings, including all the regular
expression activities as seen in Perl. These will be presented later in
this tutorial.
Create a Loop and Handle Indentation
Create a new file called loop.py, put the following text into it, and chmod
it executable. Run it as ./loop.py.
#!/usr/bin/python
ss = 1
while ss < 10:
print "Inside loop, subscript is", ss
ss = ss + 1
print "Finished looping."
|
This should print out nine lines stating the subscript, and a final
line saying "Finished Looping". If anything goes wrong, make sure the while
statement ends with a colon and check your indentation.
Test at Top
The while loop tests at the top, meaning the test var must be set before
entry to the loop to determine whether the loop will be entered at all.
Testing at the bottom can be simulated by incrementing at the top of the
loop, meaning the new value will be processed regardless of the results
of the test on the new value. Test at the bottom also can be accomplished
with break statements, as will be discussed later in this tutorial.
Indentation Sensitive Syntax NOTE THIS
Python has no begin and end statements. In Python, program flow statements
(if, for, while) and subroutine definitions (def) end when indentation
returns to its previous level. Indentation errors will cause the program
to fail. Let's use the loop.py program as an illustration.
Everything intended to be subservient to the while statement (in other
words, all the statements to be looped through), should be indented a uniform
amount to the right of the while statement. That indentation can be either
hard tabs, or (my preference), a certain amount of spaces. For the sake
of future maintainers of your code, use one or the other consistently.
As a generality, whenever a Python statement ends in a colon (like the
while statement above), it's expected to have subservient statements, and
those subservient statements must be consistently indented relative the
statement with the colon.
Advantages of Indation Sensitive Syntax
Those of us educated in the 1980's and 1990's couldn't help but notice
the contempt of our instructors for computer languages depending on indentation.
There were chuckling references to Cobol and RPG. We were taught that in
this brave new world of C, and all the following code would run:
if (i < 10)
{
printf("Less than 10\n");
i = 20;
}
or
if (i < 10)
{
printf("Less than 10\n");
i = 20;
}
or
if (i < 10){
printf("Less than 10\n");
i = 20;
}
or
if (i < 10){
printf("Less than 10\n");
i = 20;
}
or even
if (i < 10) {
printf("Less than 10\n");
i = 20;
}
Then we were urged to write readable code, even though the compiler would
accept almost anything. Without the compiler to catch indentation mistakes,
succeeding generations of maintenance programmers slowly made the code
unreadable. Not so with Python. The above if statement would be ultra
readable:
if (i < 10):
printf("Less than 10\n")
i = 20
No bracket matching, no strange indentation -- everything in a block looks
like a block.
I'm not saying indentation sensitive syntax is perfect. It can be maddening
to fail because of an extra space. But in these days of inadequate documentation,
the simplicity and self-documentation of Python syntax can be welcome.
Insert an If Statement Inside the Loop
#!/usr/bin/python
ss = 1
while ss < 10:
if ss % 3 == 0:
print "Inside loop, subscript is", ss,
print "Divisible by three"
elif ss % 2 == 0:
print "Inside loop, subscript is", ss,
print "Divisible by two"
else:
print "Inside loop, subscript is", ss,
print "Divisible neither by three nor two"
ss = ss + 1
print "Finished looping."
|
This should put out the following:
Inside loop, subscript is 1 Divisible neither by three nor two
Inside loop, subscript is 2 Divisible by two
Inside loop, subscript is 3 Divisible by three
Inside loop, subscript is 4 Divisible by two
Inside loop, subscript is 5 Divisible neither by three nor two
Inside loop, subscript is 6 Divisible by three
Inside loop, subscript is 7 Divisible neither by three nor two
Inside loop, subscript is 8 Divisible by two
Inside loop, subscript is 9 Divisible by three
Finished looping.
If there are problems, troubleshoot. Pay special attention to indentation
and colons
Note the following:
-
If statements can be followed by any number of elif statements (else
if), and one else statement. If, elif and else all have colons, and statements
to all three are indented relative to the if, elif or else.
-
Python's indentation and lack of block specifiers makes for much more readable
code.
Use the Pass Statement
Occasionally you may want an empty if or else statement. Python cannot
accommodate an empty statement because it has no block enders like Pascal,
C, Java, etc. So Python gives us the pass statement, whose purpose is to
serve as an empty statement. Consider this:
#!/usr/bin/python
month = 3 #var in if statement must have been declared
if month == 1:
pass
elif month < 6:
print "early"
else:
print "late"
|
Here January does nothing, months Feb thru May print "early", and months
June thru December print "late". This is not a particularly good example,
but this is how the pass statement is used.
Use the Break (but not the Continue) Statements
Break statements immediately leave the loop.Continue statements abandon
the remaining statements in the loop and skip back up to the test. Both
are violations of structured programming, which states a control structure
should have one exit point. Both are close cousins of the dreaded goto
statement (which Python doesn't have). Both can create very harsh maintenance
problems later. In this author's opinion, the continue statement does not
have enough value to be used ever. Better to work around its absense with
if statements.
On the other hand, break statements can sometimes improve readability
and even maintainability. This is especially true when two different conditions
can stop the action. Create this break.py program, make it executable and
execute it. Note that the strategic placement of the break statement prevents
an extra comma after the last number. Note that the while 1 would go on
forever if not for the break statement.
#!/usr/bin/python
ss = 1
while 1:
print ss,
if(ss == 9):
break;
print ",",
ss = ss + 1
print "."
|
The output of this program should be:
1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 .
If it isn't, troubleshoot.
Lists
Hello World
Create, make executable and run this hello.py:
#!/usr/bin/python
seasons = ["Winter","Spring","Summer","Fall"]
print seasons
|
The output will be:
['Winter', 'Spring', 'Summer', 'Fall']
If not, troubleshoot.
What you have done here is created a list. Lists are mutable (changable),
both in terms of changing individual members and in terms of adding, deleting
or rearranging members.
Accessing List Members
Now run this program:
#!/usr/bin/python
seasons = ["Winter","Spring","Summer","Fall"]
print len(seasons) #there are 4 seasons
print seasons[0] #element 0, the 1st element
print seasons[2] #element 1
print seasons[len(seasons)-1] #last element
print seasons[-1] #last element
print seasons[0:2] #elements 0 and 1, (1st & 2nd)
print seasons[1:3] #elements 1 and 2
print seasons[1:9] #1 thru last, overrange not error
print seasons[-1:9999] #last element
print seasons[-1:len(seasons)] #last element
print seasons[-3:-2] #3rd &2nd to last elements
# print seasons[len(seasons)] would produce an error, as
# the last element is seasons[3].
|
-
len(listname) returns the number of elements in the list. Since the list
is zero based, the number of elements equals one plus the index of the
last number.
-
listname[x] represents the xth member of the list, where the first member
is listname[0].
-
listname[s:e] is called "slicing" the list, and represents a subset list,
of all listname elements with indexes s <= x < e. In other words,
starting with the s element of the original list, and containing all members
up to, but not including, the e member. Please remember all these subscripts
are zero based. If the ending number is too big, it simply delivers the
remainder of the list including and after element s.
-
listname[-s:-e] is the a subset list starting with the member whose index
len(listname) - s, and ends BEFORE len(listname) - e.
-
Other combinations are possible. Experiment.
-
Accessing a single element beyond the upper limit produces an error.
Looking Up List Members by Value
#!/usr/bin/python
seasons = ["Winter","Spring","Summer","Fall"]
n = seasons.index("Summer")
print n
print seasons[n]
|
Use this with care. If the argument to index is not a value in the list,
the program fails to run. also, index pulls up only the first element with
that value. To be sure all such elements have been deleted, use listname.count(string)
to get a count of the number of times it appears, then run through a loop
to get them all. Note also that this prevents a non-occurring string from
bombing the program.
Inserting List Members
Using the insert list method, members can be inserted anywhere in a list,
including the beginning or the end.
#!/usr/bin/python
seasons = []
seasons.insert(len(seasons), "Spring") #append to empty list
seasons.insert(0, "Winter") #insert at beginning of list
seasons.insert(len(seasons), "Fall") #append to end of list
print seasons
seasons.insert(2, "Summer") #insert before seasons[2], "Fall"
print seasons
|
In this exercise, you inserted elements at the beginning, at the end,
and in the middle.
Appending List Members
#!/usr/bin/python
seasons = []
seasons.append("Winter") #append element
print seasons
seasons = seasons + ["Spring", "Summer"] #append a list to a list
print seasons
seasons.append("Fall") #append element
print seasons
|
In this exercise you used the append command to append elements, and
you used the plus (+) operator to append an entire list.
Deleting a List Member
#!/usr/bin/python
seasons = ["Winter","Spring","Summer","Fall"]
print seasons
del seasons[2]
print seasons
seasons = ["Winter","Spring","Summer","Fall"]
print seasons
del seasons[1:3]
print seasons
|
Add a For Loop
#!/usr/bin/python
seasons = ["Winter","Spring","Summer","Fall"]
for x in seasons:
print x, "season."
|
The output here should be:
Winter season.
Spring season.
Summer season.
Fall season.
Practical Exercise: A Data Generator
This code outputs 25 different names, with phony numbers, to stdout. This
can be used as test data for a program.
#!/usr/bin/python
lastnames = ["Albright","Barclay","Chavez","Dugan","Eng"]
firstnames = ["Allen","Betty","Charles","Donna","Edward"]
empno = 1001
outstring = ""
for first in firstnames:
for last in lastnames:
print "%04d,%s,%s" % (empno, last, first) #comma delimeted
empno = empno + 1
|
Note the following:
-
Nested for loops
-
Enhanced print statement very much like C's printf(). Note the use of the
percent sign between the format string and the list of args, and the fact
that the args are in parens.
-
Slightly modified, this program is actually useful for producing test data.
It's 11 lines, counting blank lines.
-
With a name per alphabet letter, and a list of middle names (another nest
in the loop), it can output 26*26*26 different names, for a total of 17,576
records.
Strings
#!/usr/bin/python
mystring = "house"
print "Original string", mystring
mystring = mystring + "keeper"
print "After appending keeper:", mystring
print "Letters 0 to but not including 4:", mystring[0:4]
print "Letters 5 to but not including 9:", mystring[5:9]
|