THE US NATIONAL VIRTUAL OBSERVATORY
P ython Mini T utorial
Shui H ung Kwok
W. M. Keck O bservatory
September 7, 20 0 6
09/07/06N VO SS 2006 1What is P ython?
• P rogramming language
– Created by G uido van Rossum
• Interprets compiled byte code
• D ynamically typed
– T ype goes with values instead of containers
• First released in 1991
– Smalltalk:71, P erl:1987, T cl:1988, Java:1990 , P H P :1994
• Current version: 2.4.3 (March 2006)
09/07/06N VO SS 2006 2D ownload and Installation
• http:/ /www.python.org/download
• See G ettingP ythonSO AP .html
– fpconst and SO AP P y
– MySQ LD B
– P ython Imaging Library
• Start python
$ python
09/07/06N VO SS 2006 3H ello World!
P ython 2.4.3 (#2, Jun 17 2006, 22:02:40)
[G CC 4.0.1 (Apple Computer, Inc. build 53 41)] on darwin
T ype "help", "copyright", "credits" or "license" for more information.
>>>
>>> print “H ello World!”
H ello World!
>>> print 2+3
5
>>> Crtl-D to quit
09/07/06N VO SS 2006 4H ello World!
print “H ello World!”
• Save in file ex1.py
• $ python ex1.py
• H ello World!
09/07/06N VO SS 2006 5Interesting D ata T ypes
• Sequences mutable
– Lists
• a = [1,2,3 ]
– T uples
• b = (1,2,3 ) inmutable
• s = “T his is a string”
• D ictionaries
– ie {'key': 'value', 'a' : 'b'}, d['k'] = 'v‘
• Sets
09/07/06N VO SS 2006 6Statements
• Assigments
a = 1–
a,b,c = 99, 41, 5–
a,b,c = s–
• Special statements
– pass
– global
– print
– del, raise, assert, exec
09/07/06N VO SS 2006 7Control Flow Statements
• If ... [elif ... ...
Voir