Title: Python Tutorial 1
1Python Tutorial 1
??? Yu-Tsao Hsing
- Laboratory for Reliable Computing
- Department of Electrical Engineering
- National Tsing Hua University
- Hsinchu, Taiwan
2Who am I ?
- Yu-Tsao Hsing (???)
- Email ythsing_at_larc.ee.nthu.edu.tw
- http//larc.ee.nthu.edu.tw/ythsing
- ?? H01
3Reference
- Python ?? (Learning Python), Mark Lutz David
Ascber, OREILLY??, ISBN957-8247-35-4 - ??Python, ???, ????, ISBN957-466-280-2
- Python ????, Guido van Rossum, ????http//larc.ee
.nthu.edu.tw/jcyeh/python/cdoc/tut/tut.html
4Procedure
- 7/14 Python Tutorial (1) PM 7001000
- 1. Introduction
- 2. How to start ?
- 3. Embedded object types
- 7/21 Python Tutorial (2) PM 100400
- 4. Control structure
- 5. Function
5Procedure (cont.)
- 7/28 Python Tutorial (3) PM 100400
- 6. Modules
- 7. Packages
- 8. Basic I/O
- 8/4 Python Tutorial (4) PM 100400
- 9. Exceptions
- 10. Classes
61. Introduction
7Python
- 1991 ?? Guido van Rossum ?????????????
- ??? Zope ????? PythonLabs ?????
- ?? Python ? C ?????????
- ???? Scripting Language
- ? Java ?????? Jython ????,??? .NET ??
8Why we need learning python?
- ????? (Object Oriented)
- ??????? (Interpreted, Interactive)
- ??? (Cross Platform)
- ???? (Easy Learning)
- ?????? (Clear Syntax and Structure)
- ????? (Extended and Embedded)
- ???? (Open Source)
9Applications
10Applications
11Graphic User Interface
12Resources Online
- Python Language Website
- http//www.python.org/
- ???????????...
- Dive Into Python
- http//diveintopython.net/
- ??????...
- Zope Python User Group
- http//zpug.org/
- ? WiKi ??????...
- JPython Website
- http//jpython.sourceforge.net/
- Java ?????????...
- Starship Python
- http//starship.python.net/
- ??????...
- Non-Programmers Tutorial For Python
- http//www.honors.montana.edu/
- jjc/easytut/easytut/easytut.html
- Python FAQ
- http//www.python.org/doc/FAQ.html
- ????????????...
- ActiveState
- http//www.activestate.com/
- ?? Python?Perl?Tcl ???...
132. How to Start
14Some Rules
- gtgtgt ???python?????
- ???Unix (or PC)???
- python???????
- ?????
- .py python??????
- .pyc ???????python?????
15Install
- Unix
- ????
- PC
- http//www.python.org/ftp/python/2.3.4/Python-2.3.
4.exe - ??????????
- ?????
16Hello Python !
- In Unix
- python
- gtgtgt print Hello python !
- Hello python !
- ??Ctrl D????
- In PC
- ??gtPython 2.3gt IDLE(python GUI)
- gtgtgt print Hello python !
- Hello python !
- ????????????
17????python???
- ????????????LaRC.py
- ????????? python??
- python LaRC.py
- ???(Unix?)?????????
- !/usr/local/bin/python
- ???
- chmod X LaRC.py
- LaRC.py
18??????
- Python???????????
- ???dir()?????????????
- gtgtgt x 1
- gtgtgt y LaRC
- gtgtgt dir( )
- __builtins__, __doc__, __name__, x, y
- gtgtgt dir(x)
- ??????????????
193. Embedded Object Type
20Python?????
- Python???????, ???????(Module), ??(Statement),
???(Object) - ?????????
- ?????????
- ????????????
- Everything in python is object !!
21??????
???? ???? ??
?? Number 3.1415, 1234, 999L, 34j
?? String LaRC, NTHUs
?? List 1, 2, three, 4
?? Dictionary foodspam, tasteyum
Tuple Tuple (1, LaRC, 4, U)
?? File text open(eggs, r).read()
223.1 Number
23Number-Introduction
- ??, and ??????
- ?C????, ????and?????????
- ???????
?? ??
1234, -24, 0 ????
999999999999L ???
1.23, 3.14e-10, 4E210, 4.0e210 ???
0177, 0x9ff 8???16????
34j, 3.04.0j, 3J ????
24Number-??????
- ????????
- ????10???????
- ?????or??????????
- ??????
- ?? in Python long integer in C (32 bits)
- ??? in Python Double float in C
- ????L (or l), ?????????
- 16???8????
- 8?????0 ???????0, ????
- 16?????0x (or 0X)
- ??
- ??????j (or J)??
- ??????????
25Number-????????(1/3)
- ???Python??????
- ???????????
- ??????????????????
??? ??
x or y, lambda args expression or?????, ????????
x and y and?????
not x ???????
lt, lt, gt, gt, , ltgt, ! is, is not in, not in ????? ???? ???????
26Number-????????(2/3)
??? ??
x y or ?????
x y xor?????
x y and?????
x ltlt y, x gtgt y ?x??or????y???
x y, x - y ??, ??
x y, x / y, x y ??, ??, ????
-x, x, x ??, ??, ???????
xi, xij, x.y, x() ??, ??, ??????, ????
(), , , Tuple, ??, ??, ?????
27Number-????????(3/3)
- ????
- ?????????????Q_Q
- ???()???
- ??????????????
- ?????????????
- ?????python?????????????
- Python????????????????
- ?? lt ??? lt ??? lt ??
- ?????
28Number-????(1/3)
- ???? ?????????
- Python
- gtgtgt a 3 ????lt????
- gtgtgt b 4
- gtgtgt b / 2 a ?? ((4/2)3)
- 5
- gtgtgt b / (2.0 a) ?? (4/(2.03))
- 0.8
- ????
- ??2.0a, ??2.0, ?a?3gt3.0, ??5.0
- ??b/5.0, ??5.0, ?b?4gt4.0, ??0.8
- If ?? 2 ???,?4/5???0
29Number-????(2/3)
- ???? ??C??, ?????
- For ????, ???????
- gtgtgt x 1 001
- gtgtgt x ltlt 2 100, ??????
- 4
- gtgtgt x 2 011, 001 OR 010
- 3
- ??? ??????
- ??, ???, ???!!! (?????????)
- gtgtgt 9999999999999999999999999999999 1
- OverflowError integer literal too large
- gtgtgt 9999999999999999999999999999999L 1
- 10000000000000000000000000000000
30Number-????(3/3)
- ?? ????
- gtgtgt 1j 1J
- (-1 0j)
- gtgtgt 2 1j 3
- (23j)
- gtgtgt (2 1j) 3
- (63j)
- ?????
- ????math??????
- gtgtgt import math
??C??includeltxxx.hgt - gtgtgt math.pi p
- 3.14159265359
- gtgtgt abs(-42), 24, pow(2,4)
- (42, 16, 16)
313.2 String
32String-Introduction
- ?????
- Immutable sequence ??????
- ???????????
- ???Lists????
- ???????
- S1 ???
- S2 spams ???
- Block ?????
- ??? or ??? ?? ????? !!
- ?????????????????
33String-???????
- ????
- S1 S2 ??
- S2 3 ????
- for x in S2 ???????
- m in S2 ????
- ?????????
- S2 i ????
- S2 i j ????
- len(S2) ????
- ?????????
- a s parrot dead ???????
34String-????(1/2)
- ??????
- gtgtgt len ( LaRC )
- 4
- ???????
- gtgtgt LaRC EE
- LaRCEE
- ?????? LaRC LaRC LaRC
- gtgtgt LaRC 3
- LaRCLaRCLaRC
- ????? !!
- Python ??????
- LaRC 3 ???????
35String-????(2/2)
- ???????????????
- gtgtgt mylab LaRC
- gtgtgt R in mylab
- 1 means
true - gtgtgt for letter in mylab ???????
- print letter
- L
??print???? - a
- R
- C
- We will discuss the detail in next week
36String-????????(1/3)
- ????C??, ?? 0 n-1
- ?????????? -n -1
- Offset_p Offset_n n
- gtgtgt mylab LaRC
- gtgtgt mylab0, mylab-2 ??????
- ( L , R ) tuple
- gtgtgt mylab13 ????
- aR
- ????? (lower buond), (upper bound)
- gtgtgtmylab1, mylab-1 ????
- ( aRC , LaR ) tuple
37String-????????(2/3)
0
1
2
-2
-1
I
H
a
v
e
a
D
r
e
a
m
- ???? (Si)
- ??????????
- ???????????????
- S-2 Slen(S)-2
- ???? (Sij)
- ????????????????
- ?????????0?len(S)
- S-1 ??????????????????
38String-????????(3/3)
- ???????1?????
- cat echo.py
- Import sys
??C??includeltxxx.hgt - Print sys.argv
- Print sys.argv1
- python echo.py a b c
- echo.py , a , b , c
- a , b , c
- ???????2?????????????
- gtgtgt LaRC Lab for Reliable Computing\n
- gtgtgt LaRC-1
- Lab for Reliable Computing
39String-????
- ?????!
- gtgtgt mylab LaRC
- gtgtgt mylab3 K
- Traceback (most recent call last)
- File ltpyshell9gt, line 1, in toplevel-
- TypeError object doesnt support item assignment
- ???, ?? !!
- ???????????
- gtgtgt mylab mylab-1 K!
- gtgtgt mylab
- LaRK!
- gtgtgt mylab mylab4 is a bird
mylab-1 - LaRK is a bird!
40String-????(1/3)
- gtgtgt That is d s! (1, bird )
??C?sprintf - That is 1 bird!
tuple - ????? !!
- ??????
s ?? X 16????(??)
c ?? e ???????(??)
d 10???? E ???????(??)
i ?? f ???????
u ???? g ??????(??)
o 8???? G ??????(??)
x 16???? ????
41String-????(2/3)
- gtgtgt Mylab LaRC
- gtgtgt I am in s now! Mylab
- I am in LaRC now!
- gtgtgt d s d ? (1, , 1)
- 1 1 ?
- gtgtgt s - s - s (33, 3.1415926, 1, 2, 3)
- 33 3.1415926 1, 2, 3
- s??????????????????
- ????????_at_O_at_
- ?????????????
- gtgtgtI 3.1415926
- gtgtgtI s I
- gtgtgtI
- 3.1415926
- ??????????
- ???????????
42????-???????
- Python????????-string
- gtgtgtimport string
- gtgtgtS immediaTely
- gtgtgtstring.upper(S)
????? - IMMEDIATELY
- gtgtgtstring.lower(S)
????? - immediately
- gtgtgtstring.find(S, mm)
??mm?string???? - 1
- gtgtgtstring.atoi(151), 151
????????????? - ( 151, 151 )
- gtgtgt LaRC 151, string.atoi(151) 2
- (LaRC151, 153)
- gtgtgtstring.join(string.split(S, mm), ll)
?mm??, ??ll???? - illediaTely
- ?????Python?Help?
43String-????(3/3)
- ????
- gtgtgtSARS We are the World
- gtgtgtSARS
- WearetheWorld
- gtgtgtBig Oh, what is this ?
- It is too big !!!
- gtgtgtBig
- Oh, what is this ? \n It is too big !!!
\newline ??? \n ????
\\ ??? \v ????
\ ??? \t ????
\ ??? \r ????
\a ?? \f ??
\b ???? \0XX 8????
\000 ??? \xXX 16????
44???????-?????????
- ??python???????????, ?????????, ??????????,
???????????, ????? - ????????????
- Python?????????????????
- ?????????
- ?????????, ????, ??
- ?????key?????????
- ??, ??, ????????????_at_O_at_
- ???????, ?????????
- ???????
- ?????? (shared object reference)
453.3 List
46List-Introduction
- ?????????
- ??????
- ???????(??, ??, ????)
- ????????
- ????????, ?????
- ?????, ????????gt?????,??
- ????, ??????
- ??????, ?????
- ????????????????
- ?????????
- ??????????????
- ????????????????
- ???????, ??????????????
47List-??????
- ??????, ????????????
- ??? ?????????
- ?????C?????
- ????????
- ??????(??)
- ???, ?????????, ???????
- ?????????????
- ?????????????or????
- ?????????????
- ????copy????
- ????????????????
48List-???????(1/2)
- ????
- L1
??? - L2 0, 1, 2, 3
4?????? - L3 abc, def, ghi ????
- len (L2)
?? - L1 L2 ??
- L2 3
???? - For x in L2
???? - 3 in L2
???????? - ??????????
- L2 i , L3 i j
????? - L2 i j
????
49List-???????(2/2)
- ?????? lt??????
- L2.append(4) ????
- L2.sort( ) ??
- L2.index(1) ??
- L2.reverse( ) ??
- del L2 k ????
- L2 i j
- L2 i 1 ?????
- L2 i j 4, 5, 6 ????
- range(4), xrange(0, 4) ??????? / tuple
50List-????(1/2)
- ??
- gtgtgt len( 1, 2, 3 )
- 3
- ??
- gtgtgt 1, 2, 3 4, 5, 6
- 1, 2, 3, 4, 5, 6
- ????
- gtgtgt Oh 4
- Oh, Oh, Oh, Oh
????? - ??????
- gtgtgt for x in 1, 2
- print x
- 1
- 2
51List-????(2/2)
- ??, ??????
- ??????? ???????
- ??????? ???????
- ????????????????Q_Q
- ????
- Ans. ?????
- ?????, ??
- ?????, ??list()
- gtgtgt 1, 2 34 ?? 1,2
34 - 1, 2 34
- gtgtgt 1, 2 list( 34 ) ?? 1, 2
3, 4 - 1, 2, 3, 4
52List-??????????
- ??????????
- gtgtgt mylab larc, Larc?, LaRC!
- gtgtgt mylab 2
- LaRC!
- gtgtgt mylab -2
- Larc?
- gtgtgt mylab 1 ??????????
- Larc?, LaRC!
- ???????, ??????????
53List ??????(1/3)
- ???????????????
- gtgtgt mylab larc, Larc?, LaRC!
- gtgtgt mylab 1 is ????????
- gtgtgt mylab
- larc, is, LaRC!
- ?????? ???????????????
- gtgtgt mylab 0 2 Im, a
- gtgtgt mylab ?0,1??????
- Im, a, LaRC!
- ??????????
- gtgtgt mylab 1 2 student, in
- gtgtgt mylab
- Im, student, in, LaRC!
54List-??????(2/3)
- ??????
- ????(qualification) (.)
- ?????????
- ????
- ?????, ??????????, ????
- gtgtgt mylab.append( nthu )
- gtgtgt mylab
- Im, student, in, LaRC!, nthu
- ???? mylab mylab nthu
- ??? mylablen(mylab) nthu
- ??? mylablen(mylab) nthu
- ??????
- ???????, ???????????
- ??, ??????
55List-??????(3/3)
- ??
- ??????, ????????
- gtgtgt mylab.sort( )
??ASCII??? - gtgtgt mylab
- Im, LaRC!, in, nthu, student
- ??
- gtgtgt del mylab-1 ??????
- gtgtgt mylab
- Im, LaRC!, in, nthu
- gtgtgt del mylab-1 ??????
- gtgtgt mylab
??mylab-1 - nthu
563.4 Dictionary
57Dictionary-Introduction
- ?key??, ?????
- ?????(associative array), ??(hash)
- Key, ?????????(value)???????
- ?????????, ?????key not ???
- ??????, ????????
- ??????????????
- Key??????????, ??????
- ??????value?, ?????
- ????, ???, ??????
- ??????, ???????? (????)
- ?????
- ????????(??), ?????????
- ??????, ???????????
- ???????(???)
- ????????????lt????????
- ???(Hash Table)
58Dictionary-???????
- ????
- D1
??? - D2 LaRC1, NTHU2
????????? - D3 TWLaRC1, NTHU2 ????
- D2NTHU, D3TWLaRC ?key??
- D2.has_key(NTHU)
?????? - D2.keys()
key?? - D2.values()
????????? - len(D2)
???? - ??????
- D2key new
??/?????? - Del D2key
??
59Dictionary-????
- ??key???
- gtgtgt D2 LaRC1, NTHU2, TW3
- gtgtgt D2NTHU
- 2
- ????????
- gtgtgt len(D2)
- 3
- Key?????
- gtgtgt D2.hes_key(TW)
- 1
- ??key (??????)
- ?????????, ???
- gtgtgt D2.keys()
- LaRC, NTHU, TW
60Dictionary-??????
- ????key????, ????key??????
- ?????
- gtgtgt D2NTHU EE, CS
- gtgtgt D2
- LaRC1, NTHUEE, CS, TW3
- ?????
- gtgtgt del D2TW
- gtgtgt D2
- LaRC1, NTHUEE, CS
- ?????
- gtgtgt D28 floor
key??????? - gtgtgt D2
- 8Floor, LaRC1, NTHUEE, CS
61Dictionary-?????
- gtgtgt table Python Guido van Rossum,
- Perl Larry Wall,
- Tcl John Ousterhout
- gtgtgt language Python
- gtgtgt creator tablelanguage
- gtgtgt creator
- Guido van Rossum
- gtgtgt for lang in table.keys()
- print lang, \t, tablelang
- Perl Larry Wall
- Tcl John Ousterhout
- Python Guido van Rossum
- ????????, ??for??????keys()?????
623.5 Tuple, File
63Tuple-Introduction
- ??????
- ??---????
- ???????????
- ????????or????
- ????????
- ????????
- ?????????????????
- ????????????????
- ???function?????
- ??????????
64Tuple-??tuple??
- ( )
?tuple - T1 (0, )
1????tuple(??????) - T2 (0, 1, 2, 3) 4????tuple
- T3 0, 1, 2, 3
4????tuple(?????) - T4 (abc, (def, ghi)) ??tuples
- T1 i , T3 i j ?????
- T1 ij , len(t1) ????, ??
- T1 T2 ??
- T2 3 ????
- for x in T2 ????
- 3 in T2 ????
65File-Introduction
- ???????
- ???????????
- ?????C???stdio?
- Output open(/tree/temp.dat, w)
- Input open(data.dat, r)
- ??
- r????
- w???????
- a??????
66File-????????
- S input.read( ) ??????
- S input.read(N) ??N????
- S input.readline() ?????
- L input.readlines() ????????????
- Output.write(S) ???????
- Output.writelines(L) ??????????
- Output.close() ????
- gtgtgt myfile open(myfile.txt, w) ?????
- gtgtgt myfile.write(hello text file\n) ?????
- gtgtgt myfile.close()
- gtgtgt myfile open(myfile.txt, r) ?????
- gtgtgt myfile.readline()
????? - hello text file\n
- gtgtgt myfile.readline()
??? ???? -
673.6 ??
68???????(1/3)
??
???
??
??
??
???
????
???
??
??
??
???
??
??
Tuple
69???????(2/3)
- ??????
- Assignment manipulates references
- x y does not make a copy of y
- x y makes x reference the object y references
- ????, ????????
- Example
- gtgtgt a 1, 2, 3
- gtgtgt b a
- gtgtgt a.append(4)
- gtgtgt print b
- 1, 2, 3, 4
70???????1
a 1, 2, 3
b a
a
1
2
3
4
a.append(4)
b
71???????2
a 1
a
1
b a
b
new int object created by add operator (11)
2
a
a a1
old reference deleted by assignment (a...)
1
b
72???????(3/3)
- gtgtgt L1 1, 2
- gtgtgt L2 1, 2
- gtgtgt L1 L2, L1 is L2
- (1, 0)
- ??
- ??????????
- is ????????????
- ???
- ? xx, 1
- ? , , , 0.0, None ??C???NULL
- ??
- ??????????
- ??????, ??????????
- ???Tuple?????????
- ??????????
73?????(1/2)
- ????()?????
- ????
- gtgtgt L 1, 2, 3
- gtgtgt M X, L, Y M X, L , Y
- gtgtgt M
- X, 1, 2, 3, Y
- gtgtgt L1 0
- gtgtgt M
- X, 1, 0, 3, Y
- ??????
- ?????????????
- gtgtgt L 1 L.append(L)
- gtgtgt L
- 1,
74?????(2/2)
- ??????
- ????, ?????????????
- gtgtgt L 4, 5, 6
- gtgtgt X L 3
- gtgtgt Y L 3
L L L L, L, L - gtgtgt X
- 4, 5, 6, 4, 5, 6, 4, 5, 6
- gtgtgt Y
- 4, 5, 6, 4, 5, 6, 4, 5, 6
- gtgtgt L1 0
- gtgtgt X
- 4, 5, 6, 4, 5, 6, 4, 5, 6
- gtgtgt Y
- 4, 0, 6, 4, 0, 6, 4, 0, 6
75Summary
- ????
- Python???
- ????python
- Unix
- PC
- ??????
- ??, ??, ??, ??, tuple, ??
- Whats in next week ?
- ???????
- ??
- ???
- ?????HW???