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 ... ...
>> >>> print “H ello World!”H ello World!>>> print 2+35>>> Crtl-D to quit09/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‘• Sets09/07/06N VO SS 2006 6Statements• Assigmentsa = 1–a,b,c = 99, 41, 5–a,b,c = s–• Special statements– pass– global– print– del, raise, assert, exec09/07/06N VO SS 2006 7Control Flow Statements• If ... [elif ... ..." />
T HE US N ATIONAL V IRTUAL O BSERVATORY
Python Mini Tutorial
Shui Hung Kwok W. M. Keck Observatory September 7, 2006
NVOSS 2006
09/07/06
1
What is Python?
• • • • •
Programming language – Created by Guido van Rossum Interprets compiled byte code Dynamically typed – Type goes with values instead of containers First released in 1991 – Smalltalk:71, Perl:1987, Tcl:1988, Java:1990, PHP:1994 Current version: 2.4.3 (March 2006)
NVOSS 2006
09/07/06
2
Download and Installation
•
•
•
http:/ /www.python.org/download
See GettingPythonSOAP.html – fpconst and SOAPPy – MySQLDB – Python Imaging Library
Start python $ python
NVOSS 2006
09/07/06
3
Hello World!
Python 2.4.3 (#2, Jun 17 2006, 22:02:40)
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information. >>> >>> print “Hello World!” Hello World! print 2+3 >>> 5 >>> Crtl-D to quit
NVOSS 2006
09/07/06
4
Hello World!
•
• •
print “Hello World!”
Save in file ex1.py
$ python ex1.py Hello World!
NVOSS 2006
09/07/06
5
Interesting Data Types
•
• •
Sequences – Lists • a = [1,2,3] – Tuples • b = (1,2,3) • s “This is a string” = Dictionaries – ie {'key': 'value', 'a' : 'b'}, d['k'] = 'v‘ Sets
NVOSS 2006
mutable
inmutable
09/07/06
6
Statements
•
•
Assigments – a = 1 – a,b,c = 99, 41, 5 – a,b,c = s
Special statements
– pass
– global
– print
– del, r
del, raise, assert, exec
NVOSS 2006
09/07/06
7
Control Flow Statements
•
•
If ... [elif ... else] if a == c: print “a equals c” else: print “a and c are different”
While ... [else] while a == b: work () else: finalize ()
NVOSS 2006
09/07/06
8
For Loops
•
For loop for item in sequence: Statements else: statements
–
Example: for item in (1,3,5,7,9,15,2003): print item for item in xrange (1, 50): sum += item