Python Mini Tutorial
22 pages
English

Python Mini Tutorial

Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres

Description

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 1 What 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 2 D 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 3 H 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 4 H ello World!
print “H ello World!”
• Save in file ex1.py
• $ python ex1.py
• H ello World!
09/07/06N VO SS 2006 5 Interesting 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 6 Statements
• 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 7 Control Flow Statements
• If ... [elif ... ...

Sujets

Informations

Publié par
Nombre de lectures 76
Langue English
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
NVOSS 2006
09/07/06
9
Exceptions
assert expr, [ message] try ... except ... else try ... finally raise ...
NVOSS 2006
09/07/06
10
Functions/Methods
def functionName  (params): Parameters def func1 (a,b,c): def func2 (a,b,*t): # t is a tuple def func3(u,*v,**w): # v is a tuple, w is a dictionary Invocation func1 (1,2,3) func1 (c=5, b=2, a=2) func2 (1, 2, 99, 98, 95) # t=(99,98,95) func3 (1, 2, 99, h=9, k=8) # t=(99), w = {‘h’:9,’k’:8}
NVOSS 2006
09/07/06
11