ç度 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¹
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§okÉ~?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 ^u1§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 'um¬
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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 õ1A?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
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$1UYmu"ù´éõ)º.ó5"XVB kScript Engineer§LispkEVAL§Emacsr*Ð5" ¡ é 3Python¥§a(class)§¼ê(function)§¬(module) Ñ´é"§Ü `§¦'Java§C++ \OO( Object Oriented)"O4·ò\“oOO? Java OO?”?Ø¥§^LÒ "zóÑkg`":§ Python á õó`:" * Ð Python ¦^Có§ég,^C½öC++ó*ÐPythonA5"X? #¬§O\#êâa." i \ Python)ºìÚ±éN´i\^rgCC½öC++ó§S8 ¥ § ¦ \ § 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ù´ka. "ØUòstringa.CþDintegera.Cþ§ ´Ia.=" ùÚ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¿11AÏ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å52:ê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§ Ø2Oexpr2§Ò´ `expr2 ¥XJk¼êN^{§Ø¬1"XJexpr1 ´Ü6ý§@oâ¬Oexpr2 §Ü6LªÒ£expr2"éuor 5`§~Xexpr1 or expr2§XJexpr1 ´Ü6 ý§@oÒoLª£expr1 § Ø2Oexpr2§Ò´`expr2 ¥XJk¼ê N^{§Ø¬1"XJexpr1 ´Ü6b§@oâ¬Oexpr2 §Ü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" ¦vko5K§´lmg"4·5ueo: 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 ´ý§@oLªÒ´expr_true • 1|`² §XJcond ´b§@oLªÒ´expr_false • |`² §·` "P4XJexpr_true kU´bÿ§Øùo§ ÏkU»5§cond ´ýÿ§Lª%´expr_false")û ù¯K{Ò´: ( and (,) or (,))[0] ¡)ÒrLª)å5§UCO`k?§ÄK[0] `k?p"¥m )ÒÚÏÒ§´tuple Lª"ù(,)[´ý§Ï¦´¹ ktuple§ [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 ó¦^ö^ Ac §Ú1«Â´aq" o§# ¡@«ª§1«ÒÐ "
§1.4 L(list) list aquC++¥vector§C ó¥ê|§^u;^S("X§ c¥zUê§±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) Ovar 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§Ù{ í"XJindexulist ݧÒ3 V\"XJindexu0§Ò3m©?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.ØÓ§(JgCwwj"جù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§listdzexpr1 |¤§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¥=di1|¤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¥§kL«“rlist ¥ziÎ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¥mdxiÎ ë§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Î RLÎ 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 ±riÎ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«¹ktuple{´: (1§) ©"
¡kÏÒ§^5ÚüÕCþ«
tuple 'list 5UЧҴ`§Ø^JøÄS+nõU"
§1.7 S(sequence) sequence )string§list§tuple"¦Ñk±e Ï^ö" • in ä,object ´Ø´3sequence ¥" >>> 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] ÏLkÒeIfsequence§seq[start:end] ^+ L«ëüsequence ^* L«Esequence§"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§ # zpair^ÏÒ©
§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éØ3keyDö§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§XJDc=a§@ocÚa§?UcSNÓ¬ Ka"´XJb=a.copy()§@ob´Õá§UbجKa" 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()) §´eB§^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} ù«{±^5ldictionary ,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