Python 123 introduction

J

Jeremy Sanders

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).

HTML version:
http://www-xray.ast.cam.ac.uk/~jss/lecture/computing/notes/out/python_123/
Postscript LaTeX output:
http://www-xray.ast.cam.ac.uk/~jss/lecture/computing/notes/out/python_123.ps
PDF LaTeX output:
http://www-xray.ast.cam.ac.uk/~jss/lecture/computing/notes/out/python_123.pdf
LaTeX source:
http://www-xray.ast.cam.ac.uk/~jss/lecture/computing/notes/out/python_123.tex

Jeremy
 
D

dakman

This is great! A excellent tutorial for somone who has prior experience
in programming and is starting out in python. My friend keeps wanting
me to teach him python, I think this would be the perfect link for him.
Thanks.
 
J

Jeremy Sanders

This is great! A excellent tutorial for somone who has prior experience
in programming and is starting out in python. My friend keeps wanting
me to teach him python, I think this would be the perfect link for him.

I'm glad you think it is useful. It needs a bit of cleaning up as it assumes
things such as python being in /usr/local/bin... I may try to improve this
later.

Jeremy
 
S

skip

dakman> This is great! A excellent tutorial for somone who has prior
dakman> experience in programming and is starting out in python. My
dakman> friend keeps wanting me to teach him python, I think this would
dakman> be the perfect link for him.

I'm not trying to minimize Jeremy's efforts in any way, but how is his
tutorial a significant improvement over the original
(http://www.python.org/doc/current/tut/)?

Skip
 
J

Jeremy Sanders

I'm not trying to minimize Jeremy's efforts in any way, but how is his
tutorial a significant improvement over the original
(http://www.python.org/doc/current/tut/)?

It's not intended as a replacement, but what I wanted to do was write a
quick 2 hour course for people to work through. It overlaps quite a bit
with the tutorial, but I tried to minimise any detail.

I just publicised it in case anybody wanted something similar.

Jeremy
 
J

John Salerno

dakman> This is great! A excellent tutorial for somone who has prior
dakman> experience in programming and is starting out in python. My
dakman> friend keeps wanting me to teach him python, I think this would
dakman> be the perfect link for him.

I'm not trying to minimize Jeremy's efforts in any way, but how is his
tutorial a significant improvement over the original
(http://www.python.org/doc/current/tut/)?

Skip

well, not to minimize the efforts of guido's tutorial, but sometimes it
seems less of a tutorial and more of a summary of python's features, so
it never hurts to have things explained in a different way...
 
B

Bruno Desthuilliers

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.
<type 'file'>

"""
Note that immutable objects (like numbers, strings or tuples) do not
have this property.
"""

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

""""""

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top