Jeremy Sanders a écrit :
Here is a brief simple introduction to Python I wrote for a computing course
for graduate astronomers. It assumes some programming experience. Although
it is not a complete guide, I believe this could be a useful document for
other groups to learn Python, so I'm making it available for others to
download, and modify for their own needs (some of the content is site
specific).
May I emit some observations and suggest a couple of corrections ?
"""
To make it executable type chmod +x test.py, then run it by typing its
name, test.py on the unix prompt
"""
Unless the current directory is in the path, this won't work:
bruno@bibi ~ $ cat toto.py
#!/usr/bin/python
print 'hello'
bruno@bibi ~ $ chmod +x toto.py
bruno@bibi ~ $ toto.py
-bash: toto.py: command not found
bruno@bibi ~ $ ./toto.py
hello
bruno@bibi ~ $
">>a+b # the value is printed at the prompt"
s/value/result/
"Numbers can be integers (int, whole numbers) or floating point (float)"
s/Numbers/Numeric objects/
"Strings are collections of characters"
s/Strings/String objects/
"Lists are collections of any types of variable (even lists)"
List objects are ordered collections of any type of objects (even other
lists)
"Tuples are like lists but they cannot be changed"
s/Tuples/Tuple objects/
<side-note>
Semantically, a tuple is more a kind of record - a dict indexed by
position - than an immutable list. That is: lists are homogenous ordered
collections of arbitrary length. Neither the length of the collection
nor the position of an object in it have special meaning. While tuples
are fixed-length heterogenous ordered structures where both the number
of items and their positions are meaningfull. Canonically, a DB table
can be represented as a list of tuples.
</side-note>
"Files correspond to files on the disk"
File objects correspond to OS files.
>>import sys
type(sys.stdin)
<type 'file'>
"""
Note that immutable objects (like numbers, strings or tuples) do not
have this property.
>>a = 10 # makes a point to object 10
"""
NB : here '10' is not the id of the object, it's its value. So it should
be: # makes name 'a' point to an int object
"""
>>b = a # makes b point to object 10
a = 11 # makes a point to object 11
print b # prints 10
"""
Hem... This has nothing to do with ints being immutables:
a = [1] # makes 'a' point to a list
b = a # makes 'b' points to the same object
a = [1] # makes 'a' points to *another* list
print "a is b ? %s" % (a is b)
"""
In Python subroutines, procedures and functions are basically the same thing
"""
NB : The type is 'function'. They *always* return something
(implicitely, the None object).
"None is a special value meaning ``nothing''"
s/value/object/
"You can test whether something is None by using is None"
There's always only one single None object, so you can test whether
something is None by using 'is None'.
"""
a = ['foo', 'fred', 42]
for i in a:
print i
"""
Traditionaly, identifier 'i' is used as the current index in C-like
loops. Using it in this context might be a bit confusing :
a = ['foo', 'fred', 42]
for obj in a:
print obj
"""
As an aside, there is a shortcut version of loops called a list
comprehension which is very convenient:
"""
List comps are a "shortcut" for building lists. They are not a "shortcut
version of loops".
"""
filename = 'stupid.dat'
try:
f = open(filename)
except IOError: # the file did not open
print "The filename", filename, "does not exist!"
"""
Actually, the file may exist, but the program may not be able to open it
for other reasons...
filename = 'stupid.dat'
try:
f = open(filename)
except IOError, e: # the file did not open
print "could not open file %s : %s" % (filename, e)
My 2 cents...