|
 |
Python |
0. Getting Started
|
|
Note: This material was used as a reference
for the course CS3723-Fall14.
The page Calendar gives
a list of homework assignments for this course. You could work through these
assignments, though there is quite a bit of material not directly related
to Python. Also, answers are given to most of the homework.
Access to Python:
Python is available for most computer systems. In particular it's available
by default on Apple, Linux, or Unix systems. Python comes in two
main versions:
where "%" stands for the system prompt and "#" makes the rest
of the line a comment. On my systems this looks like (user input is
bold):
% python -V
Python 2.7.12
% python3 -V
Python 3.5.2 # Linux Xubuntu
Python 3.6.3 # Mac Air
I intend to use only Python 3 in this tutorial.
Some programs here may be tested with both Python 2 and 3.
If Python 3 causes any problems, I'll comment on that.
In these notes I'll mostly write python for the command, but
it will really be python3.
Any use of the print function in Python 3 will differ from
the use of the print keyword in Python 2, but adding the weird
import line: from __future__ import print_function # out of science fiction
will cause print in Python 2 to behave like print
in Python 3. So I'll always use the new style of print,
and if you try to run a program in Python 2, you must add that import
statement.
How to Get Started:
As a start you should type in the first Python program given
below and run it. This work has several objectives:
- Start working on the Python language. In particular, the Python
program you are to copy uses three specific programming
methods:
- Reading from a text file.
- Using regular expressions to extract fields from a string.
- Using Python's "list" data type. (A Python list has all the
features of an array and of a linked list, plus many more.)
- Learn to write and run Python programs.
Note: You can program in Python on almost any
platform: Python is usually already present on
Apple and Linux machines, and it is available for download on
Windows.
Note: A "copy" program may seem weird and useless,
but you really should type it in from scratch.
(There is significant benefit from typing it yourself.
If you make typing errors, so much the better.)
For lazy types, use a copy of this page with all the programs
accessible in Ascii:
Hidden Files.
Running a
Python Program: Assume your program is in the
file "h0.py" on a Unix/Linux system. Execute the command:
% python h0.py
where "%" stands for the prompt.
Better is to use the command:
% python h0.py -tt
With the "-t" option, Python issues a warning if
there are any tabs, and "-tt" makes any tabs an error.
Alternatively, you can add the following as the first line
of the file h0.py:
#!/usr/bin/python -tt
And then type:
% chmod +x h0.py # make h0.py executable
% ./h0.py # execute, using first line to find python
I mostly won't use this method (more trouble than it's
worth for writing sample programs), but it is very useful for
systems programming and larger production work.
The Program for
you to type in and run:
Type the program shown in the box below. Store it using the name
h0.py .
(The name must end in .py )
Copy the data file students.txt into the
same directory as the Python program. Then execute as shown above.
Here is the program that you are to type in and run.
It is given as an image:
Larger copy of the above image:
image
Notes:
- In order to produce just this output, the list "ave"
is not needed, since the program could compute a running sum.
I wanted to illustrate lists.
- Python mostly has no declarations of variables, and a given
variable can mostly be used for any type you like, or even
for several types in the same program.
- When you are inside parens (as in lines 8-9 and 28-29
above), you may indent on the second line in any way you like.
Otherwise, the indenting should be exactly as shown above,
with exactly 4 spaces for each level of indenting, and with
no tab characters. (I was able to set my editor so that the
Tab key always produces 4 spaces and no tabs.)
- My favorite
mistake is to leave off the ":" character just before a new
level of indenting, or to use ";".
- The "strip( )" in line 19 strips off any whitespace characters
from the start or end of the character string "line". In this
case there was only a newline at the end. The "strip( )"
method makes
a new copy of the string for use in line 19, but the variable
"line" retains the newline. You would need to write
"line = line.strip( )" to get rid of the newline
from the variable "line".
- Python regular expressions are mostly the same as in any other
scripting language, but there are significant notational differences.
Python has nothing like the "$" variables that can be used in Perl to
refer to the different matching groups. Instead you must use the
"group( )" method as shown on lines 13-15. Thus
"m.group(3)" takes on the role that
"$3" has in Perl. The result is a string, and
lines 13-15 use the function "float( )" to
convert this string to floating point (a double).
Parts of
Python Illustrated by this Program:
- Reading from a text file:
"students.txt" is a file of records in Linux/Unix where each
record is terminated with a newline ("\n").
(In Windows, records are often terminated with a carriage return
and a linefeed.)
Line 5 is an iterator that provides each record of the file
in sequence. The record retains its newline at the end (in Unix).
Lines 12-17 form a while loop that explicitly reads each record
and explicitly stores the record in the variable "line".
(The name "line" is an arbitrary choice.)
Below is a program that fakes having a file, "opening" it, and
"reading" from it. This can be convenient if you interact with
a terminal window that has Python but doesn't support input.
Notice that the only thing changed below is the new definition
of the variable f (in red).
- Formatted input using a regular
expression: Here we don't show the input, but show
how the fields of one string are extracted.
The regular expression (RE) below is what comes between the quote marks
in r"xxx" . In this case it is "xxx".
Each RE describes a collection of character strings. (The above
describes only a triple of "x"s.) In the example here, we want to
describe the contents of lines in the file. Short-hand notation gives
us "\s" for white space, "\d" for a digit,
and "\w" for a character
in a word (upper-lower case letter). Adding a "+" to the right
means "one or more occurrences of". Anything inside parentheses
are matched and available for use. (A parenthesis itself is
represented by "\(" or "\)".) The "compile" method makes the
RE ready for use, and the "search" method searches
for any matches available.
There's a lot more to this, and we'll go over it later.
- Making use of a Python list:
In Python, the common and versatile data structure "list" is a
combination of all the features of a linked list and of an array,
along with much more besides.
Suppose you
need to use Python from a browser:
The best case is to use the
ideone Python 2 and 3 simulator.
The link is set for Java by default. You have to change a box from
Java to Python or to Python 3.
You can copy the whole students.txt file into their spot for
stdin. You have to read from the file sys.stdin.
One way is to use the line
as shown above. (In place of line 10 in the original program.
You also need to delete or comment out line 4, the line that opens
the file.)
(Revision date: 2022-12-07.
Please use ISO 8601, the International Standard.)
|