Python - Google Sites

12 downloads 11 Views 2MB Size Report
的度 len(dog)得到的度? 但道. 到度EmailBody 的class 的len(a_email_body). 的的度的. 的点. • __getitem_
Python ÆS)P X’ [email protected] or [email protected]

2003c0826F

[email protected]

12,208

Python ÆS)P



8¹ 1˜ Ù Python0 0

1

§1.1 m© . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2

§1.1.1 XÛ$1Python§S . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2

§1.2 ˜ƒé– . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

§1.3 Äa. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

§1.3.1 .(integer) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

§1.3.2 2:.(float) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

§1.3.3 Nonea. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

§1.3.4 Ü6Lˆª . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

§1.4 L(list) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

6

§1.4.1 Mïlist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

6

§1.4.2 listeIÚflist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

6

§1.4.3 flistJ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

7

§1.4.4 ?nlist{ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

8

§1.4.5 ^list [Ù¦~^êâ( . . . . . . . . . . . . . . . . . . . . . . .

10

§1.4.6 list comprehension . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

12

§1.5 iÎG(string) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

13

§1.5.1 iÎGL« . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

13

§1.5.2 =†¤Ù¦a. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

14

§1.5.3 iÎGöŠ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

14

§1.6 |(tuple) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

22

§1.7 S(sequence) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

23

§1.8 i;(dictionary) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

23

§1.8.1 {ü~f . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

24

§1.8.2 dictionaryöŠ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

24

§1.9 §S6§ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

27

§1.9.1 ©|(if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

27

§1.9.2 ̂(for§while§break§continue§range() . . . . . . . . . . . . .

29

[email protected]

1i,208

Python ÆS)P

8¹ §1.10 ¼ê . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

32

§1.10.1 ļê^{ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

32

§1.10.2 ëê‡êŒÀ§ëêk%@Š . . . . . . . . . . . . . . . . . . . . . .

33

§1.10.3 UC¼êëêDŠ^S . . . . . . . . . . . . . . . . . . . . . . . . . .

34

§1.10.4 ‡êŒCëê . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

34

§1.10.5 Doc String ¼ê£ã . . . . . . . . . . . . . . . . . . . . . . . . . . .

36

§1.10.6 lambda¼ê . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

36

§1.10.7 ¼êŠ^(scope) . . . . . . . . . . . . . . . . . . . . . . . . . . .

37

§1.10.8 i@¼ê(nested) . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

38

§1.10.9 functionëêD4 . . . . . . . . . . . . . . . . . . . . . . . . . . . .

38

§1.11 ¬(module)ڝ(package) . . . . . . . . . . . . . . . . . . . . . . . . . . .

39

§1.11.1 Mmodule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

39

§1.11.2 Noémodule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

41

§1.11.3 package() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

42

§1.12 name space(·¶˜m) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

42

§1.13 a . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

46

§1.13.1 Ÿo´object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

46

§1.13.2 Ÿo´class oject . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

48

§1.13.3 property Úmethord . . . . . . . . . . . . . . . . . . . . . . . . . . . .

50

§1.13.4 bound methord Úunbound methord . . . . . . . . . . . . . . . . . .

52

§1.13.5 U«(inherit) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

54

§1.13.6 ·¤ Cþ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

54

§1.13.7

Abstrace Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

55

§1.13.8 hkattribute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

55

§1.13.9 Python ¥class AÏmethord

. . . . . . . . . . . . . . . . . . . . .

56

§1.13.10 ˜ AÏ­1¼ê^{Þ~ . . . . . . . . . . . . . . . . . . . .

63

§1.13.11 new style class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

75

§1.14 É~?n(exception) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

82

§1.14.1 Ÿo´É~?n§Ÿo‡kÉ~?n . . . . . . . . . . . . . . . . .

82

§1.14.2 Ó¼exception(É~) . . . . . . . . . . . . . . . . . . . . . . . . . . .

84

§1.14.3 Ñexception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

88

[email protected]

1ii,208

Python ÆS)P

8¹ 1 Ù mu Python ¦^ óä

91

§2.1 ¦^Emacs ?6Python §S . . . . . . . . . . . . . . . . . . . . . . . . . . .

91

§2.1.1 SCpython mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

91

§2.1.2 python mode ÄA5 . . . . . . . . . . . . . . . . . . . . . . . . .

92

§2.1.3 ~^õUÞ~ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

92

§2.2 Ù¦?6ì . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

94

§2.3 NÁ§S . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

95

§2.3.1 ¦^DDD Úpydb NÁpython §S . . . . . . . . . . . . . . . . . . . .

95

1n Ù Python ~^ ¬

96

§3.1 S˜¬ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

96

§3.1.1 ~^¼ê . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

96

§3.1.2 a.=†¼ê . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

98

§3.1.3 ^u‰1§SS˜¼ê . . . . . . . . . . . . . . . . . . . . . . . . . 100 §3.2 ÚöŠXڃ'N^ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 §3.2.1 ‹m©‡ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 §3.2.2 Ö©‡ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 §3.2.3 '4©‡ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 §3.3 regular expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 §3.3.1 {üregexp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 §3.3.2 iÎ8Ü . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 §3.3.3 ­E . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 §3.3.4 ¦^©iÎG

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110

§3.3.5 ¦^re ¬ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 §3.3.6 p?regexp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 §3.3.7 ©|(Group) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 §3.3.8 Compile Flag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 §3.4 ^struct¬?n?›êâ . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 §3.5 ^Cmd ¬?{ü·-1 . . . . . . . . . . . . . . . . . . . . . . . . 118 §3.5.1 {ü~f . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 §3.5.2 ½Â%@·- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 §3.5.3 ?nEOF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 [email protected]

1iii,208

Python ÆS)P

8¹ §3.5.4 ?n˜·- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 §3.5.5 ·-1gÄÖà

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121

§3.5.6 UCIOÑ\ÑÑ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 §3.5.7 UCJ«Î . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 §3.5.8 Jø3‚ÏõU . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 §3.5.9 $1Shell õU . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 §3.5.10

getattr õU¦^ . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123

§3.6 ?n·-1À‘

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

§3.6.1 ˜‡{ü~f

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

§3.6.2 ‘këê·-1À‘ . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 §3.6.3

optparser ¬ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

§3.7 'užm¬

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136

§3.8 socket ?§ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 §3.8.1 0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 §3.8.2 {ü~f . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 §3.8.3 Mïsocket

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139

§3.8.4 ”½socket

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140

§3.8.5 ïáë . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 §3.8.6 Âxêâ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 §3.8.7 ämë . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 §3.8.8 Ù¦9ϼê . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 §3.8.9

TCP ÚUDP «O . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145

§3.8.10 'uTCP ÑÖì{K . . . . . . . . . . . . . . . . . . . . . . . . . 146 §3.9 Âe‡ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 §3.10 POP3 Æ0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 §3.10.1 ˜‡{ü~f . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 §3.10.2 ¦^POP3 ÆÂe‡ . . . . . . . . . . . . . . . . . . . . . . . . . 151 §3.10.3 õ1A?n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 §3.10.4 POP3Ï&L§¥G . . . . . . . . . . . . . . . . . . . . . . . . . 152 §3.10.5 POP3¥·- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 §3.11 ^poplib ¬Âe‡ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 [email protected]

1iv,208

Python ÆS)P

8¹ §3.12 SMTP Æ0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 §3.12.1 SMTP˜‡~f . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 §3.12.2 uxe‡·- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 §3.12.3 Ù¦·- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 §3.12.4 ESMTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 §3.12.5 ¹@y . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 1o Ù Tkinter ?§

167

§4.1 Tkinter0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 §4.1.1 Ÿo´Tkinter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 §4.2

Hello Tkinter §S . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 §4.2.1 {ü~f . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 §4.2.2 ,˜‡{ü§S

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170

§4.3 Widget ˜ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 §4.4 Geometry Manager(AÛ+nì) . . . . . . . . . . . . . . . . . . . . . . . . . 172 §4.4.1 Pack +nì . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 §4.4.2 Grid +nì . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 §4.5 Widget ª . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 §4.5.1 ôÚ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 §4.5.2 iN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 §4.5.3 ©i‚ªz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 §4.5.4 >µ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 §4.5.5 àI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 §4.6 ¯‡Ú¯‡½ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 §4.6.1 ˜‡{ü~f

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186

§4.6.2 ¯‡?n¼ê . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 §4.6.3 ¯‡

g . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188

§4.6.4 Ә‡¯‡õ‡?n¼ê . . . . . . . . . . . . . . . . . . . . . . . . 191 §4.6.5 ÚWM ƒ'¯‡”½ . . . . . . . . . . . . . . . . . . . . . . . . . . 192 §4.7 ~^A^§S¦^Widget . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 §4.7.1 ÄI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 §4.7.2 èü . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 [email protected]

1v,208

Python ÆS)P

8¹ §4.7.3 óä9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 §4.7.4 G9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 §4.7.5 IOé{µ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 1 ÊÙ Python * Ð

205

§5.1 ^C?*Ь . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205

[email protected]

1vi,208

Python ÆS)P



ã 3-1 žm¼êƒm'X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 3-2 ràÚÑÖìà¼êN^6§ . . . . . . . . . . . . . . . . . . . . . . . . 139 4-1 Tkinter XÚ

gã . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167

4-2 hello1.py $1(J . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 4-3 hello2.py $1(J . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 4-4 border1.py $1(J . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 4-5 cursor1.py $1(J . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 4-6 menubar2.py $1(J . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 4-7 menubar3.py $1(J . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 4-8 toolbar1.py $1(J . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 4-9 statusbar1.py $1(J . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 4-10 inputpassword.py $1(J . . . . . . . . . . . . . . . . . . . . . . . . . . . 203

[email protected]

1vii,208

Python ÆS)P



L‚ 1.5-1 iÎG¥=¿iÎ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

14

1.5-2 iÎG=†Ù¦a. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

14

1.5-3 äiÎG´Äáu,«aO¼ê . . . . . . . . . . . . . . . . . . . . . .

16

1.13-4 Œ±­1ŽâöŠÎ¼ê . . . . . . . . . . . . . . . . . . . . . . . . . . .

63

3.1-1 Python ­^S˜¼ê . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

97

3.1-2 a.=†¼ê . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

99

3.2-3 XÚN^Þ~ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 3.4-4 struct ¬¥‚ª½Â . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 3.4-5 i!^S½Â . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 3.5-6 ·-1?6¯$… . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 3.7-7 L«žm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 3.8-8 ^u=†i!^S¼ê . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 3.8-9 TCP ÚUDP «O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 3.11-10 poplib ·-o( . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 4.5-1 tkFont |±option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 4.6-2 ¯‡£ãÞ~ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 4.6-3 £ã¯‡ëê . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189

[email protected]

1viii,208

Python ÆS)P

1˜Ù

Python0 0 python´˜«)º5¡•é–Šó"pythonk±eA:" )º . „Ý ú§mu±Ïá§NÁN´§g·*Ð5"dukevalõU§? §SŒ±3$1žUYmu"ù´éõ)º.Šó5"XVB kScript Engineer§LispkEVAL§—EmacsrŒ*Ð5" ¡• é– 3Python¥§a(class)§¼ê(function)§¬(module) Ñ´é–"§Ü `§¦'Java§C++ \OO( Object Oriented)"O4·ò\“Ÿo‰OO? Java OO?”?Ø¥§^LÒ "z‡ŠóÑkˆg`":§ Python á  õŠó`:" Œ* Ð Python ¦^CŠó§ég,^C½öC++Šó*ÐPythonA5"X? #¬§O\#êâa." Œi \ Python)ºìڙŒ±éN´i\^rgCC½öC++Šó§S‘8 ¥ § ¦ \  § S Œ ± | ±  ? § § * Ð õ U " y 3 Ò kPython  ¬ Œ ± * ÐVIMÚEmacsõU" S Ø PythonØ%)ºì´é" Ä a. ØI‡¯k(²(declare)Cþa.§†¦^CþҔéõÙ¦)º.Š óVB§VB Script§JScript§PERL˜" ra . ¦+CþØ^(²(declare)a.§´˜ Cþk Ч@où‡Š´k˜‡a. "ØUòstringa.Cþ†DЉ˜‡integera.Cþ§ ´I‡a.=†" ùÚVB§VBScript§JScript§PERLؘ§ÚJava ”" [email protected]

11,208

Python ÆS)P

˜ Python0

§1.1 m© §1.1.1 XÛ$1Python§S Python ´ ˜ « ) º .  ? § Š ó § Ú Ù ¦ ) º . ? § Š ó § XPerl§VB§Bash§Awk§Sed Ñkaq§$1{Xe" • 1˜«{´pª"b\´3Unix ‚¸e§$ ´·-1J«ÎÒ" $python Python 2.2 (#1, Apr 12 2002, 15:29:57) [GCC 2.96 20000731 (Red Hat Linux 7.2 2.96-109)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> print "Hello World" Hello World >>> #U CTRL+D òÑ 3Windows ¥kaq‚¸"©¥Œõê~fÑ´3pª$1">>>Ú...L «Python J«ÎÒ"Bash eéõ·-1?6·-Ñ´Œ±¦^§XC-a §C-e §C-w §Ï¦‚Ñ´¦^readline ¥" • „Œ±r§S3˜‡©‡¥§^python )ºìN^" $cat test.py print "Hello World" $python test.py Hello World $cat test.py | python #+öŠ Hello World $python < test.py #Ñ\­½• Hello World • r§SŠ˜‡Œ±$1Unix ©‡" $cat test.py #!/usr/bin/python print "Hello World" $ls -l test.py -rw-r--r-1 chunywan mnw $chmod +x test.py $ls -l test.py [email protected]

12,208

40 Sep 12 13:34 test.py

Python ÆS)P

§1.2 ˜ƒé– -rwxr-xr-x $./test.py Hello World

1 chunywan mnw

40 Sep 12 13:34 test.py

5¿1˜1AÏ5ºŠé§ù´Unix eÏ^{"Perl§Tl/Tcl§Sed§awk§Bash Ñ´ùo^" Š5¿´5ºŠé§# ¡L«5º"Úbash˜"5ºŠé3NÁ§Sžÿ§ ´ék^§„Œ±Jp§SŒÖ5"

§1.2 ˜ƒé– a(class)§¼ê(function)§¬(module) Ñ´é–"“˜ƒÑ´é–§é–Ñk¶ i”§ù´·˜‡o("

§1.3 Äa. §1.3.1 .(integer) wå5”êLˆªÒ´."k±e$Ž öŠÎ

¿Â

~f

(J

+ − ∗ / % ∗∗ divmod

\{ ~{ ¦{ Ø{  ˜ Ø{

1+1 1-1 2*10 10/3 10/3 2**4 divmod(10§3)

2 0 20 3 1 16 (3§1)

§1.3.2 2:.(float) wå5”2:êLˆªÒ´2:ê"Ú.aq"5¿§1´.§1.0´2:."Ü 6Lˆª1 == 1.0 ´ýŠ"5¿§2:ê$Ž´kØ "^u‰ÆOŽžÿ§I‡ Ù¦¬|±"ù´Ï2:$ŽìUL«k ê"

§1.3.3 Nonea. None´˜‡AÏ~þ§L«Ñ†"3±

Œ±w¦^?"

§1.3.4 Ü6Lˆª Python¥vkbooleana."ҔCŠó¥§Ø 0± §Ù¦Ñ´ý"´Python¥ “b”kéõ§) [email protected]

13,208

Python ÆS)P

˜ Python0 None,0,0.0,"" (˜iÎG),[](˜list),()(˜tuple),{}(˜dictionary) Python¥k±eÜ6$ŽÎ" and or not

Ü6† Ü6½ Ü6‡

>>> def getTrue(): ... print "debug: true" ... return 1 ... >>> def getFalse(): ... print "debug: false" ... return 0 ... >>> if getTrue() and getFalse(): ... print "ok" ... else: ... print "bad" ... debug: true # k$1 getTrue debug: false # XJ getTrue ˆ£ý,@o$1getFalse bad # ‡(J´ getFalse ˆ£(J. >>> if getFalse() and getTrue(): ... print "ok" ... else: ... print "bad" ... debug: false #k$1 getFalse bad #XJ getFalse ˆ£b,@oÒØ$1 getTrue >>> Š5¿´and Úor §éuand 5`§~Xexpr1 and expr2§XJexpr1 ´Ü6b§@ oÒoLˆª†ˆ£expr1 §N´˜iÎG§½ö˜list§ Ø2OŽexpr2§Ò´ `expr2 ¥XJk¼êN^{§Ø¬‰1"XJexpr1 ´Ü6ý§@oâ¬OŽexpr2 §‡Ü6LˆªÒˆ£expr2"éuor 5`§~Xexpr1 or expr2§XJexpr1 ´Ü6 ý§@oÒoLˆª†ˆ£expr1 § Ø2OŽexpr2§Ò´`expr2 ¥XJk¼ê N^{§Ø¬‰1"XJexpr1 ´Ü6b§@oâ¬OŽexpr2 §‡Ü6LˆªÒˆ £expr2"ùÒk˜«~^AÏLˆª: [email protected]

14,208

Python ÆS)P

§1.3 Äa. and or ¦k˜‡ék^ŠÂ´: XJcond ¤á§@oˆ£expr_true§ÄKˆ£expr_false" ¦vkЇŸo5K§´l†mg"4·‚5u˜eŸo: cond

expr_true

expr_false

o(J

ý

ý

?Û(ØOŽ)

expr_true

b b

?Û(ØOŽ) ?Û(ØOŽ)

ý b

expr_false expr_false

ý ý

b b

b ý

expr_false expr_false

lù‡LþŒ±w: • ޘ|`² §XJcond ´ý§@o‡LˆªÒ´expr_true • 1|`² §XJcond ´b§@o‡LˆªÒ´expr_false •  ˜|`² §·`œ "P4XJexpr_true kŒU´bžÿ§Ø‡ùo‰§ ϏkŒU‹»5ŠÂ§cond ´ýžÿ§‡Lˆª%´expr_false")û ù‡¯K{Ò´: ( and (,) or (,))[0]  ¡)Òr‡Lˆª)å5§UCOŽ`k?§ÄK[0] `k?p"¥m )ÒÚÏÒ§´˜‡tuple Lˆª"ù(,)[´ý§Ï¦´¹ k˜‡ƒtuple§ [0] L«JÑù‡ƒ5"3$´§Ï~ù‡Lˆ ª´^3'{üœ¹e§{üDŠŠé§expr_trueÚexpr_false ˜„Ñ´~ þ§ …´ý" >>> x= cond and "OK" or "BAD" >>> x ’OK’ >>> cond = 0 >>> x= cond and "OK" or "BAD" >>> x ’BAD’ >>> Ón: or and kaqŠÂ§´Ø‡¦^ù«Lˆª§Ø´Ïù‡LˆªØÜ{§ ´Ï ¦ØÎÜS."?§ŠóÚg,Šókaq/§Ò–·‚²ž¬`§“r§¯Œ”§ [email protected]

15,208

Python ÆS)P

˜ Python0 ´·‚ج`§“r§?ê”§½ö“r§?  ”§Ø´Ï@o`Ò´†§´ÏØ ÜS."@Ò¬¯§ŸoØÜS.§S.l=p5ºÏ´lC Šó¥5"C Šó¥k cond ? true_expr: false_expr ù´˜‡nöŠÎ§¯õC Šó¦^ö^ A›c §Ú1˜«ŠÂ´aq" oƒ§# ¡@«ª§1˜«ÒÐ "

§1.4 L(list) list aquC++¥vector§C Šó¥ê|§^u;^S("X§ ˜c¥z‡U꧌±L« [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ] ˜c¥z‡¶¡§Œ±L« [ ’January’, ’February’, ’March’, ’April’, ’May’, ’June’, ’July’, ’August’, ’September’, ’October’, ’November’, ’December’ ] z‡(϶i§Œ±L« [ ’Monday’,’Tuesday’, ’Wednesday’, ’Thursday’ , ’Friday’, ’Saturday’, ’Sunday’]

§1.4.1 Mïlist a=[’1’,’2’] ^[])å5§L«˜‡list§¥mˆ‡ƒŒ±´?Ûa.§^ÏÒ©…"

§1.4.2 listeIÚflist listeIl0m©§ÚCŠóaq§´O\ KeI¦^" -len ... -2 -1 0 1 ... len-1

1˜‡ƒ ... ê1‡ƒ  ˜‡ƒ 1˜‡ƒ 1‡ƒ ...  ˜‡ƒ

>>> a=[0,1,2,3,4,5,6] [email protected]

16,208

Python ÆS)P

§1.4 L(list) >>> i=-len(a) >>> while i < len(a): ... print "a[",i,"]=",a[i] ... i=i+1 ... a[ -7 ]= 0 a[ -6 ]= 1 a[ -5 ]= 2 a[ -4 ]= 3 a[ -3 ]= 4 a[ -2 ]= 5 a[ -1 ]= 6 a[ 0 ]= 0 a[ 1 ]= 1 a[ 2 ]= 2 a[ 3 ]= 3 a[ 4 ]= 4 a[ 5 ]= 5 a[ 6 ]= 6 5¿whileŠé ?"¬3 >>> a[-8] Traceback (most recent File "", line IndexError: list index >>> a[7] Traceback (most recent File "", line IndexError: list index

¡0 "a[-8] Úa[9] ѬÚå."

call last): 1, in ? out of range call last): 1, in ? out of range

§1.4.3 flistJ ŒÏLeI½‰Œ§^uJј‡list˜Ü©"eIL² ˜§˜‡å© ˜§ ˜‡(堘§¥m^kÒ©…"XJ؍½å© ˜§å© ˜´0 §XJ؍½(å  ˜§(堘´−1"flist L«)å© ˜?ƒ§˜—(堘§´Ø)(å  ˜ƒ" KeIŒ±ë†½Âå© ˜§½ö(堘" >>> a[1:4] [email protected]

17,208

Python ÆS)P

˜ Python0 [1, >>> [0, >>> [1, >>> [0, >>> [0, >>> [1,

2, 3] a[:] 1, 2, 3, a[1:] 2, 3, 4, a[:3] 1, 2] a[:] 1, 2, 3, a[1:-1] 2, 3, 4,

4, 5, 6] 5, 6]

4, 5, 6] 5]

flist ´5list ˜‡€§˜‡B§UCflist جUC5list"

§1.4.4 ?nlist{ • L.append(var) §J\ƒ" >>> >>> >>> [0, >>>

a=[0,1,2,3,4,5] a.append(6) a 1, 2, 3, 4, 5, 6]

• L.count(var) OŽvar 3L¥Ñygê" >>> a=[0,2,2,2,3,3] >>> a.count(2) 3 >>> a.count(3) 2 • len(L) ˆ£L Ý" >>> a=[1,2,3] >>> len(a) 3 • L.extend(list) §òlistJ\3L

¡"

>>> a=[1,2,3] >>> a.extend([4,5,6]) [email protected]

18,208

Python ÆS)P

§1.4 L(list) >>> a [1, 2, 3, 4, 5, 6] • L.index(var) ˆ£var3L¥ ˜§eçKÑÉ~" >>> a=[1,2,3,4,4,5] >>> a.index(4) 3 >>> a.index(6) Traceback (most recent call last): File "", line 1, in ? ValueError: list.index(x): x not in list • L.insert(index,var) 3index?§\var§Ù{ƒ• í"XJindexŒulist ݧÒ3 V\"XJindexu0§Ò3m©?V\" >>> >>> >>> [1, >>> >>> [1, >>> >>> [1,

a=[1,2] a.insert(0,1) a 1, 2] a.insert(100,100) a 1, 2, 100] a.insert(2,3) a 1, 3, 2, 100]

• L.pop() ˆ£ ˜‡ƒ§¿…í؁ ƒ§¿…íØTƒ" >>> >>> 3 >>> 0 >>> [1, >>>

˜‡ƒ"L.pop(index) ˆ£index?

a=[0,1,2,3,] a.pop() a.pop(0) a 2]

• L.remove(var) évar§¿…í؃§eçÑÉ~" [email protected]

19,208

Python ÆS)P

˜ Python0 >>> a=["a","b","a","c","a","d"] >>> a.remove("a") >>> a [’b’, ’a’, ’c’, ’a’, ’d’] >>> a.remove("a") >>> a [’b’, ’c’, ’a’, ’d’] • L.reverse() òLS" >>> >>> >>> [5,

a=[1,2,3,4,5] a.reverse() a 4, 3, 2, 1]

• L.sort() §òaüS§a¥ƒea.ØÓ§(JgCw˜wj"˜„جùo‰"Q ,\ÑØ(½list¥ƒa.´Ä˜§\„‡üS§@o\ϖ´Ÿo(J? >>> >>> [1, >>> >>> >>> [1,

a.sort() a 2, 3, 4, 5] a=[1,"abc",2,"xxx"] a.sort() a 2, ’abc’, ’xxx’]

L.sort(func) §ò^funcŠ'¼ê§òüS"func(x,y)§ˆ£−1§0§1©OL «u§u§Œu"

§1.4.5 ^list [Ù¦~^êâ( ^list[æÒstack >>> >>> >>> >>> >>> [0, >>> 2

a=[] a.append(0) #ØÒ a.append(1) a.append(2) a 1, 2] a.pop() #ÑÒ

[email protected]

110,208

Python ÆS)P

§1.4 L(list) >>> a.pop() 1 >>> a.pop() 0 >>> ^list[èqueue >>> >>> >>> >>> [2, >>> 0 >>> 1 >>> 2 >>> []

a.insert(-1,0) #\è a.insert(-1,1) a.insert(-1,2) a 1, 0] a.pop() #Ñè a.pop() a.pop() a

½ö >>> >>> >>> >>> >>> [0, >>> 0 >>> 1 >>> 2 >>> 3

a.append(0) a.append(1) a.append(2) a.append(3) a 1, 2, 3] a.pop(0) a.pop(0) a.pop(0) a.pop(0)

[email protected]

111,208

Python ÆS)P

˜ Python0 ^list[ätree >>> leaf1=[0,1] >>> leaf2=[2,3] >>> leaf3=[4,5] >>> leaf4=[6,7] >>> branch1=[leaf1,leaf2] >>> branch2=[leaf3,leaf4] >>> root=[branch1,branch2] >>> root [[[0, 1], [2, 3]], [[4, 5], [6, 7]]]

§1.4.6 list comprehension Š{: [ for k in L if ] ŠÂ: returnList=[] for k in L: if : returnList.append() return returnList; ˆ£˜‡list§listƒdz˜‡expr1 |¤§if Šé^uLȧŒkŒÃ" >>> a=["123","456","abc","Abc","AAA"] >>> [ k.center(9) for k in a ] [’ 123 ’, ’ 456 ’, ’ abc ’, ’ Abc #a¥=di1|¤iÎG§¿r¦C¤Œ" >>> [ k.upper() for k in a if k.isalpha() ] [’ABC’, ’ABC’, ’AAA’] #a¥=dŒi1|¤iÎG§¿r¦C¤" >>> [ k.lower() for k in a if k.isupper() ] [’aaa’] #a¥=kêi¤iÎG§¿r¦C¤êa." >>> [ int(k) for k in a if k.isdigit() ] [123, 456]

’, ’

AAA

’]

LÈ(filter)§ÚN(mapping)ڏ˜" ¦+ù‡^{é{ü§´%›©k^§§SC{'§ŒÖ5r"kéõžÿ§·‚ ¦^̂Šé§==‰é{ü¯œ§´Ì‚Šé´éØÐ֧Ϗ̂ŠéØÎ [email protected]

112,208

Python ÆS)P

§1.5 iÎG(string) Üg,Šó¥S."~Xµþ¡~f¥§k˜‡L«“rlist ¥z‡iÎG¥iÎ C¤Œ”"´XJ^̂ŠéL«Ò´: “M˜list§éulist ¥z˜‡ƒ§r ù‡iÎGC¤Œ§V\#ïlist¥"” „kéõ~f§4·‚ég,A^ù«^{" rlist¥¤kƒ\ 1 [ k+1 for k in list] JÑ list ¥¤kêCþ [ k for k in list if type(k)==types.IntType] r list ¥¤kêƒO\˜ [ k + 1 for k in list if type(k)==types.IntType ] ·š~Uù«Lˆªœ

§1.5 iÎG(string) §1.5.1 iÎGL« ^üÚÒ½VÚÒ¤iÎG ’hello’ "world" k˜ iδÃ{^…Ñ\§¤±^iÎ|ÜL«AÏiÎ"XL1.5-1" ù´ÚC Šó›©aq§„kecho ·-§Java ŠóÑ´ùoL«˜‡AÏiÎ" XJ‡Ñ\˜‡›©iÎG§˜1ؐB§Ò^ü1"ü‡iÎG¥md˜xiÎ ë§L«˜‡iÎG"X "abc" \ ’def’ L«“abcdef”§5¿Ú"abc"+"def"´k«O§‘+L«ü‡iÎGë˜å§^˜ xiÎë§L«˜‡iÎG" XJ‡Ñ\˜‡›©›©iÎG§Ò^n‡ëYVÚÒG"X """ this is a very very long string. And it contains a lot line. it is suitable to write document of function. """ [email protected]

113,208

Python ÆS)P

˜ Python0 L«iÎ

¿Â

5

\newline

Ã



^uëõ‡á 1§¤˜‡ 1

\\ \’ \" \a \b \f \n \N{name} \r \t \v \u{xxxx} \ooo \xhh

\ ’ " 0x07 0x08 0x0c 0x0a

“è

0x0d 0x09 0x0b 0xooo 0xhh

‡m üÚÒ VÚÒ $ ò‚ Formfeed (FF) †1 Unicode iÎ £ Y²›LÎ R†›LÎ Unicode?è ooo L«l?›iÎ hh L«›8?›iÎ

ASCII ››iÎ ASCII ››iÎ ASCII ››iÎ ASCII ››iÎ éuUnicode ASCII ››iÎ

éuUnicode

L 1.5-1: iÎG¥=¿iÎ ¼ê¶¡

õU

~f

(J

float(str) int(str) int(str,base) long(str) long(str,base)

C¤2:ê C¤. C¤base?›ê C¤. C¤base?›ê

float("1e-1") int("12") int("11",2) int("12") int("11",2)

0.1 12 2 12 2

L 1.5-2: iÎG=†Ù¦a.

§1.5.2 =†¤Ù¦a. §1.5.3 iÎGöŠ éü‡iÎG >>> "Hello" + " " + "World" ’Hello World’ Œ=† >>> "this is test".capitalize() #Äi1Œ [email protected]

114,208

Python ÆS)P

§1.5 iÎG(string) ’This is test’ >>> "THIS IS TEST".capitalize() ’This is test’ >>> "THIS IS TEST".lower() #ÜC¤ ’this is test’ >>> "this is test".upper() #ÜC¤Œ ’THIS IS TEST’ >>> "This Is Test".swapcase() #Œp† ’tHIS iS tEST’ iÎGÝ >>> len("Hello World") 11 iÎGfG Œ±r˜‡iÎGw¤˜‡¹kéõiÎlist§ùŒ±^ÚlistƒÓªf G"eIl0m©"Kê“L‡•" >>> str="Hello World" >>> str="Hello" >>> i=-len(str) >>> while i < len(str): ... print "str[",i,"]=",str[i] ... i=i+1 ... str[ -5 ]= H str[ -4 ]= e str[ -3 ]= l str[ -2 ]= l str[ -1 ]= o str[ 0 ]= H str[ 1 ]= e str[ 2 ]= l str[ 3 ]= l str[ 4 ]= o >>> kҌ±L«‰Œ§fiÎG"kÒü>êL«e.Úþ.§e.Úþ.Ñ´ŒÀ§ "Žþ3-1§e.0" [email protected]

115,208

Python ÆS)P

˜ Python0 >>> str="Hello" >>> str[:] ’Hello’ >>> str[1:2] ’e’ >>> str[1:-1] ’ell’ >>> str[-1:2] ’’ Ke.´“L ˜§Ø“LŒ"¤±lù‡¿Âþù§−1 ‡'2 Œ"str[-1:2]´˜ iÎG"AT5¿¦fiÎG´ý5iÎG€§UCiÎG´Ø¬UC 5iÎG"،±^fiÎGªUC5iÎG"X: >>> a="abcd" >>> b=a[1:2] >>> b="x" #UC b >>> a ’abcd’ #جK a >>> a[1]=’abc’ # Traceback (most recent call last): File "", line 1, in ? TypeError: object doesn’t support item assignment >>> äiÎG´Äáu,«aO L1.5-3¥{Œ±äiÎG¥´ÄÎÜ, ^‡"iÎG÷v^‡§Òˆ£1§Ä K§ˆ£0" ¶¡



s.isalnum() s.isalpha() s.isdigit() s.islower() s.isupper() s.istitle() s.isspace()

¤kiÎÑ´êi0123456789 ½öi1A-Z§a-z ¤kiÎÑ´i1a-z ½öA-Z ¤kiÎÑ´êi0123456789 ¤kiÎÑ´i1a-z ¤kiÎÑ´Œi1A-Z ¤kücÑ´Äi1Œ§”‡title ¤kiÎÑ´˜xiÎ\n,\t,\r,’ ’ L 1.5-3: äiÎG´Äáu,«aO¼ê

[email protected]

116,208

Python ÆS)P

§1.5 iÎG(string) 3iÎG¥éfG • s.find(substring,[start [,end]]) XJ駈£¢ÚЧL²´3=ké "XJ騧ˆ£−1 #ok ¡abc§a mÞ ˜ 3, >>> "ok abc abc abc".find("abc") 3 #l ˜4± m©é"ƒu s[4:].find("abc") >>> "ok abc abc abc".find("abc",4) 7 #ƒu s[4:9].find("abc") >>> "ok abc abc abc".find("abc",4,9) -1 #ƒu s[4:10].find("abc") >>> "ok abc abc abc".find("abc",4,10) 7 • s.rfind(substring,[start [,end]]) Úfind()aq§´´‡•é" >>> "ok abc abc abc".rfind("abc") 11 • s.index(substring,[start [,end]] Úfind()aq§´XJéØsubstring§Ò )˜‡ValueErrorÉ~"ü«ªØÓ3u†Ø?nØÓ" >>> "ok abc abc abc".index("abc") 3 >>> "ok abc abc abc".index("abcd") Traceback (most recent call last): File "", line 1, in ? ValueError: substring not found in string.index • s.rindex(substring,[start [,end]] Ús.index()aq"ØÓƒ?3u´‡•é" • s.count(substring,[start [,end]]) ˆ£ésubstringgê" >>> "ok abc abc abc".count("abc") 3 [email protected]

117,208

Python ÆS)P

˜ Python0 ‚ªziÎG Ä  ^ { ´ s % §tuple L « ˜ ‡ ë ê  L § a q uCŠ ó ¥printf§ rtuple ¥z˜‡Š^iÎGL«§L«‚ªds 5(½" >>> print "%s’s height is %dcm"%("Charles",180) Charles’s height is 180cm 5¿%s,%dÚCŠó¥printf´aq§âЉconversion specifier§(=†½Â) y3X­?Øconversion specifier"conversion specifier k±eA‡Ü©¤§ %[][][]| [][]| w X é E , § Ù ¢  ‡ ˜ ‡% ˜ ‡conversion typeÒ Œ ± " Ù ¦  Ñ ´ Œ À "conversion typeXe: • d L«kÎқ?›ê" >>> "%d and %d" % (-1,2) -1 and 2 • i Ód • o L«ÃÎÒl?›ê" >>> "%o and %o" % (16,8) 20 and 10 • u L«ÃÎÒê" >>> "%u and %u" %(-10,10) ’4294967286 and 10’ 5¿−10 )º¤ÃÎÒêžÿ§L«˜‡éŒê"ù´Ï3OŽÅ¥^ ÖèL«Kê§Kêp ´1§3)º¤ÃÎÒêžÿ§Ò¬éŒ" • x L«ÃÎқ8?›() >>> "%x and %x" % (100,200) ’64 and c8’ • X L«ÃÎқ8?›(Œ) >>> "%X and %X" % (100,200) ’64 and C8’ [email protected]

118,208

Python ÆS)P

§1.5 iÎG(string) • e 2:꧉ÆLˆª{() >>> "%e and %e"%(-100,200) 2.000000e+02 • E 2:꧉ÆLˆª{(Œ) >>> "%E and %E"%(-100,200) 2.000000E+02 • f L«2:ê >>> "%f and %f"%(-0.00001,200.0) ’-0.000010 and 200.000000’ • g L«2:ê§XJ´u0.0001½öØ °Ý§Ò^‰ÆL«{" >>> "%g and %g"%(-0.00001,2000000.0) ’-1e-05 and 2e+06’ • c rASCII êL«ü‡iÎ >>> "%c and %c"%(67,68) ’C and D’ Œ^uCêASCIIiÎ • r L«˜‡iÎG§ù‡iÎG¦^repr()ˆ£Š >>> "%r"%({"one":1,"two":2}) "{’two’: 2, ’one’: 1}" Úprint expr(obj) ‹>> "\t a

\t".lstrip() \t".rstrip()

[email protected]

121,208

Python ÆS)P

˜ Python0 ’ a’ >>> "\t a ’a’

\t".strip()

#‡python 2.2.3¥O\ ˜‡ŒÀëêchars§L«íØÑy3chars¥iÎ"Î ‡s.strip ƒus.strip("\t \n\r")" iÎGÜ¿(join)Ú©)(split) s.join(words)§words´ ˜ ‡  ¹ k i Î G tuple ½ ölist "join^sŠ  © … Î òwords¥iÎGëå5§Ü¿˜‡iÎG" >>> "\n".join(["Hello","World","Python","Said"]) ’Hello\nWorld\nPython\nSaid’ >>> print "\n".join(["Hello","World","Python","Said"]) Hello World Python Said s.split(words)§words´˜‡iÎG§L«©…Î"splitöŠÚjoinƒ‡"ò^s© )¤˜‡list" >>> "This is a book".split(" ") [’This’, ’is’, ’a’, ’book’]

§1.6 |(tuple) P4±e5K§ÄþҌ±Ýºtuple" • • • •

tuple´~þlist"tupleØUpop§remove§insert{" tuple ^() L«§Xa=(0§1§2§3§4)§)Ҍ±ŽÑ" tuple Œ±^eIˆ£ƒ½öftuple" tuple Œ±^uõ‡CþDŠ" >>> >>> 1 2 >>> >>> >>> 1 2

a,b=(1,2) print a,b t=(1,2) a,b=t print a,b

[email protected]

122,208

Python ÆS)P

§1.7 S(sequence) >>> a,b=b,a+1 >>> print a,b >>> 2,2 • L«¹k˜‡ƒtuple{´: (1§) ©"

¡k‡ÏÒ§^5ÚüÕCþƒ«

tuple 'list 5UЧÒ´`§Ø^JøÄS+nõU"

§1.7 S(sequence) sequence )string§list§tuple"¦‚Ñk±e˜ Ï^öŠ" • in ä,‡object ´Ø´3˜‡sequence ¥" >>> x=12 >>> l=[12,13] >>> if x in l : print "x is in l" ... x is in l >>> str="abcd" >>> if "a" in str: print " ok " ... ok >>> if 2 in T : print "OK" ... OK • • • • • •

sequenceÝlen(seq) ÏLeIÙ¥˜‡ƒ" seq[i] ÏL‘kÒeIfsequence§seq[start:end] ^+ L«ëü‡sequence ^* L«­E˜‡sequence§"a"*3 L«aaa§(1,2)*3L«(1,2,1,2,1,2) Œ±¦^list comprehension"

§1.8 i;(dictionary) dictionary ´ ˜ ‡ à S  ; (  " z ˜  ƒ ´ ˜ ‡pair§  )keyÚvalueü ‡ Ü © "key a . ´integer½ östring1 "value a . ‘ B Ÿ o Ñ Œ ± "dictionary¥ v k ­ E key"dictionary¥  z ˜ ‡  ƒ ´ ˜ ‡tuple§tuple¥ k ü ‡  ƒ § ù ‡tuplepair§pair¥c¡´key§ ¡value"Œ±^D[key] ªvalue" 1

½ö?ÛӞ¹k hash Ú cmp

[email protected]

{é–

123,208

Python ÆS)P

˜ Python0

§1.8.1 {ü~f # M{ü dictionary ¦^ {} s)Ò)å5§ # z‡pair^ÏÒ©…§pair¥key3c§value3 §kÒ©…" >>> pricelist={"clock":12,"table":100,"xiao":100 } # Ú^ dictionary ¥˜‡ƒ valuež§^ key ‰eI " >>> pricelist["clock"] 12 >>> del pricelist["clock"] >>> pricelist {’table’: 100, ’xiao’: 100} # dict(L) Œ±E˜‡ dictionary # Ù¥ L ´˜‡ list§ L¥z˜‡ƒ´˜‡tuple§ # tuple¥kü‡ƒ§c¡´key§ ¡value" >>> pricelist=dict([(’clock’,12),("table",100),("xiao",100)]) >>> pricelist {’table’: 100, ’xiao’: 100, ’clock’: 12} >>> pricelist=dict([(x,10*x) for x in [1,2,3]]) >>> pricelist {1: 10, 2: 20, 3: 30} #e阇Ø3key‰DŠöЧKO\˜‡pair" >>> pricelist["apple"]=12 #O\˜‡ ƒ >>> pricelist {’table’: 100, ’apple’: 12, ’xiao’: 100, ’clock’: 12} #XJÖØ3key§¬ÑÉ~" >>> pricelist["appled"] Traceback (most recent call last): File "", line 1, in ? KeyError: appled

§1.8.2 dictionaryöŠ b a={"one":1,"two":2,"three":3,"four":4} ˜˜dictionary ¼ê¶¡D.clear() >>> a.clear() >>> a {} [email protected]

124,208

Python ÆS)P

§1.8 i;(dictionary) ˜˜˜‡dictionary D.copy() ˜‡dictionary ˜‡€ >>> a {’four’: 4, ’three’: 3, ’two’: >>> b=a.copy() >>> b {’four’: 4, ’one’: 1, ’three’: >>> b["four"]=5 >>> b {’four’: 5, ’one’: 1, ’three’: >>> a {’four’: 4, ’three’: 3, ’two’: >>> c=a >>> c["four"]=5 >>> a {’four’: 5, ’three’: 3, ’two’: >>> c {’four’: 5, ’three’: 3, ’two’:

2, ’one’: 1}

3, ’two’: 2}

3, ’two’: 2} 2, ’one’: 1}

2, ’one’: 1} 2, ’one’: 1}

€"lþ¡~fŒ±w§XJ†DŠc=a§@ocÚa˜§?UcSNÓ¬ Ka"´XJb=a.copy()§@ob´Õá§UbجKa" D.get(key) eD¥kkey§@oˆ£D[key]§ÄKˆ£None >>> a {’four’: 4, ’three’: 3, ’two’: 2, ’one’: 1} >>> print a.get("abc") None >>> print a.get("four") 4 >>> print a.get("abc",100) 100 D.get(key,default) ÚD.get(key)ØÓ3u§eÃkey§Kˆ£default§ Ø´None "D.get(key)ƒuD.get(key,None)" [email protected]

125,208

Python ÆS)P

˜ Python0 D.haskey(key) ^uäD ¥´Ä¹kkey§ƒukey in D.keys()" >>> a {’four’: 4, ’three’: 3, ’two’: 2, ’one’: 1} >>> print a.has_key("one") 1 >>> print a.has_key("abc") 0 D.items() ˜‡list§Ù¥z‡ƒ´˜‡¹kü‡ƒtuple(pair)§tuple¥c¡´key§ ¡´value >>> a {’four’: 4, ’three’: 3, ’two’: 2, ’one’: 1} >>> a.items() [(’four’, 4), (’three’, 3), (’two’, 2), (’one’, 1)] D.copy()ƒudict(D.items()) §´e–B§^copy()¯" D.keys() list§¹kD¥¤kkey" >>> a {’four’: 4, ’three’: 3, ’two’: 2, ’one’: 1} >>> a.keys() [’four’, ’three’, ’two’, ’one’] a.copy()„Œ±¤dict([(k,a[k]) for k in a.keys() ]) >>> dict([ (k,a[k]) for k in a.keys()]) {’four’: 4, ’one’: 1, ’three’: 3, ’two’: 2} ,§XJ==´ copy§Ø‡ùoZ§^a.copy()§Ï¯" D.values() list§¹kD¥¤kvalue" >>> a {’four’: 4, ’three’: 3, ’two’: 2, ’one’: 1} >>> a.values() [4, 3, 2, 1] [email protected]

126,208

Python ÆS)P

§1.9 §S6§ D.update(E) lE¥#êâ"¿g´ for k in E.keys(): D[k]=E[k] ~Xµ >>> a {’four’: 4, ’three’: 3, ’two’: 2, ’one’: 1} >>> b={"five":5,"three":30} >>> b {’five’: 5, ’three’: 30} >>> b.update(a) >>> b {’four’: 4, ’five’: 5, ’one’: 1, ’three’: 3, ’two’: 2} ù«{Œ±^5l˜‡dictionary •,˜‡dictionary DŠ" D.popitem() ˜‡pair§¿…íئ"eD´˜§ÑÉ~" >>> print a.popitem() (’four’, 4) >>> print a.popitem() (’three’, 3) >>> print a.popitem() (’two’, 2) >>> print a.popitem() (’one’, 1) >>> print a.popitem() Traceback (most recent call last): File "", line 1, in ? KeyError: popitem(): dictionary is empty

§1.9 §S6§ ?§Šó§S6§kn«§^S§©|§Ì‚"

§1.9.1 ©|(if Š{ [email protected]

127,208

Python ÆS)P

˜ Python0 if : ½ö if : ½ö if