Frequently, you will want to do perform a relatively simple task with you computer, but, the task will be extremely cumbersome or impossible to do with c-shell, and you will dread spending all the time to write a full blown c program.
High level interpreted languages are made for these types of problems. I use python because it is free, cross-platform, incredibly easy, and the language scales well with the size of your problem. You can write scripts from quick-and-dirty hacks up to full-blown object-oriented programs, all using python.
Python has its own interactive command line where you can type code and get output in real time. To start it, simply type `python' on the command line and press return. Inside this environment, you can do basic math:
Python 2.3.4 (#1, Oct 26 2004, 16:42:40)
[GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 2+2
4
>>>
And test out quick snippets of code:
>>> x = 3
>>> y = x + 4
>>> print x,y
3 7
>>>
If you get no further in this tutorial, you should learn to love strings in Python. Compared to hard languages like C and Fortran, strings are da bomb.
They are easy to assign:
>>> x = "I don't like Spam!"
>>> print x
I don't like Spam!
>>>
Easy to split into parts:
>>> x.split()
['I','don't','like','Spam!']
>>>
Easy to add to:
>>> y = x + 'Neither do I.'
>>> print y
I don't like Spam!Neither do I.
>>>
Easy to compare:
>>> if x != y:
... print "They are not Equal."
...
They are not Equal.
>>>
You can slice them:
>>> print x[0:1],x[8:12],x[13:]
I like Spam!
You can do much, much more, including searching them, multiplying them, reversing them, and converting them to and from upper/lower case. But, I should move on.
You can quit out of the interactive python by pressing CTRL-D. Usually one will type commands into a file and run that file from the LINUX/UNIX command line.
There is a tradition in computing to always make ones first program be one that prints "Hello World". Far be it for me to break with tradition.
Let's make our first python program. Open you favorite text editor (some choices are emacs, vi, pico, and nedit) and type in the following lines:
#!/usr/bin/env python
# This is a program to print Hello World
print "Hello World!"
That's all there is to it. Save the file as helloworld.py. To run it, we need to make the file executable. From the command line:
% chmod +x helloworld.py
% helloworld.py
Hello World!
%
The first line in the file, #!/usr/bin/env
python
, tells the computer to treat all following commands
as python code. It is required. Thereafter, a `#' sign in the
file starts a comment. Any text after the `#' on the same line
will be ignored.
Let's make helloworld.py interactive. Edit the helloworld.py program as follows:
#!/usr/bin/env python
# This is a program to ask us for our name and greet us.
x = raw_input("what is your name? ")
print "Hello, %s!" %(x)
And run it:
% helloworld.py
what is your name? Nicholas
Hello, Nicholas!
%
There is a lot going on in this script:
x = raw_input("what is your name? ")
: This line
has a function, raw_input()
. This function takes one
argument, a string of text which will be printed to the screen,
and then waits for the user (you) to type some text. Lastly, this
function returns a string containing the text you type.
The second thing on this line is a variable assignment.
x =
creates a new variable named `x' (funny how that
works). `x' has the value that is returned by
raw_input()
, in this case a string.
print "Hello, %s" %(x)
This print statement contains a format string, %s
, identical to
those used in C. After the closing double-quote, you must give python the
arguments to convert the format string for output. The %(x)
does
this. If we had two format statements, we would need to give two variables like
so: %(x,y)
.
One often wants to read from the command line and use those values in your program. To do so, we need to use some extra modules in python:
#!/usr/bin/env python
# Now I can read from the command line
import sys
x = sys.argv[0]
y = sys.argv[1]
print x,y
And here is the output:
% helloworld.py
Traceback (most recent call last):
File "helloworld.py", line 7, in ?
y = sys.argv[1]
IndexError: list index out of range
I forgot to give a second argument. Thus, the line
y = sys.argv[1]
causes an error and quits. Note python gives a
reasonably useful error message. It tells you what line the error occured on (7)
and what caused the error: (IndexError: list index out of range
).
Let's try again:
% helloworld.py Nicholas helloworld.py Nicholas %
import sys
. `sys' is one of the many python
modules. This loads it into memory so you can use the functions
present within it.
sys.argv
is a list (called arrays in C). Like C,
lists are indexed starting at zero. `sys.argv' contains all the
command line arguments.
Sometimes you may find it easier to run command line tasks within python. You can do it like so:
#!/usr/bin/env python
# Now I can run command line stuff
import os
os.system("sleep 5")
Okay, so this program doesn't really do anything. However, notice the
import os
command. This is needed to load the os
module. There are all sorts of functions in os
, though here
we only care about the system()
function. Whatever you would
run on the command line can be put inside quotes as the argument to the
function. Here, sleep 5
just waits for 5 seconds and
returns a prompt.
You may also want to take the output from some command line task and
read it into python. There is more than one way to skin the cat here, but
I use a module called commands
. It is easy to use:
#!/usr/bin/env python
# grab the output of a commandline task
import commands
x = commands.getoutput('echo "Hello World"')
print x
And here is what I get when I run it:
% helloworld.py
Hello World
%
The only new thing here is the commands
module and the
commands.getoutput()
function. You can use it just like
os.system()
except the output will be returned and stored
in the variable x
. Incidentally, x
is a
string.
This is what you will probably want to do 90% of the time. Fortunately, it is incredibly easy in python:
#!/usr/bin/env python
# Now I can read from a file
import sys
fp = open(sys.argv[1],"r")
data = fp.readlines()
fp.close()
for line in data:
print line,
Here is the text I used for "file1.txt",
and here is what I get when I run helloworld.py:
% helloworld.py file1.txt
We are the
Knights Who Say
Ni!
%
open()
is another function. It takes two
arguments: the first is a filename (here we are using
sys.argv[1]
as the filename), and an option on how to
open the file; "r" stands for read. This function returns a file
pointer. That file pointer is assigned to the variable called
fp
.
data = fp.readlines()
This reads ALL the lines
from the file and stores them in a variable called
data
as a list.
fp.readlines()
readlines() is a method of fp. A
method is a function associated with an object. In this case,
readlines() reads the data from the file pointer fp
.
fp.close()
- close() is another method. It closes
the file we were reading from.
for line in data:
This is a for loop. All for
loops in python have the format: for <variable> in
<list>
. This steps through the list
data
, one element at a time and assigns the variable
line
to each element. lastly, we print out the lines
of the file with print line,
. The comma prevents
print
from inserting an additional newline. Each
line
already contains a '\n' character, so we don't want a
second one.
Indentation - Unlike virtually every other programming language, you don't need braces ({,}) or begin/end statements to designate the scope of the for loop. You simply indent some number of spaces. It doesn't matter how many, so long as you are consistent.
When you aren't reading from a file, you are probably writing to a file. It can be done like so:
#!/usr/bin/env python
# Now I can write to a file
import sys
fp = open(sys.argv[1],"r")
data = fp.readlines()
fp.close()
fp = open(sys.argv[2],"w")
for line in data:
fp.write(line)
fp.close()
The output when I run it:
% helloworld.py file1.txt file2.txt
% cat file2.txt
We are the
Knights Who Say
Ni!
%
fp = open(sys.argv[2],"w")
Again we are using the
open()
function. Except this time, the second
argument is `w', meaning we want to write to this file. Any
existing text in this file is now gone forever. If you want to
append to a file, use the `a' option.
fp.write(line)
Another method of the fp file
pointer. This writes the given string of text to the file.
The python tutorial:, http://docs.python.org/tut/tut.html, contains lots more information including standard data types and other basics. I think it is excellent, except that I don't think it covers input/output very well. (Funny how that's exactly what we covered here).
Happy Pythoning!