-
Notifications
You must be signed in to change notification settings - Fork 38
/
index.html
156 lines (152 loc) · 894 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<!DOCTYPE html>
<!-- Created by pdf2htmlEX (https://github.com/coolwanglu/pdf2htmlex) -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8"/>
<meta name="generator" content="pdf2htmlEX"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<link rel="stylesheet" href="styles/base.min.css"/>
<link rel="stylesheet" href="styles/fancy.min.css"/>
<link rel="stylesheet" href="styles/Index.css"/>
<script src="js/compatibility.min.js"></script>
<script src="js/pdf2htmlEX.min.js"></script>
<script>
try{
pdf2htmlEX.defaultViewer = new pdf2htmlEX.Viewer({});
}catch(e){}
</script>
<title>Python 旋風之旅 (正體中文版)</title>
</head>
<body>
<div id="sidebar">
<div id="outline">
</div>
</div>
<div id="page-container">
<div id="pf1" class="pf w0 h0" data-page-no="1"><div class="pc pc1 w0 h0"><div class="t m0 x0 h1 y0 ff1 fs0 fc0 sc0 ls0 ws0">Python<span class="_ _0"> </span><span class="ff2">旋風之旅</span></div><div class="t m0 x1 h2 y1 ff3 fs1 fc0 sc0 ls0 ws0">A<span class="_ _1"> </span>Whirlwind<span class="_ _1"> </span>T<span class="_ _2"></span>our<span class="_ _1"> </span>of<span class="_ _1"> </span>Python</div><div class="t m0 x2 h3 y2 ff4 fs2 fc0 sc0 ls0 ws0">[<span class="ff5">美</span>]<span class="_ _3"> </span>Jak<span class="_ _4"></span>e<span class="_ _3"> </span>V<span class="_ _2"></span>anderPlas<span class="_ _3"> </span><span class="ff5"> 著<span class="_ _5"> </span><span class="ff6">同濟大學蘋果開發俱樂部</span> 譯校</span></div>
<div class="t m0 x2 h3 y2 ff5 fs2 fc0 sc0 ls0 ws0">
<br>
[正體中文] Will 保哥 (<a href="https://www.facebook.com/will.fans/">Facebook</a>) (<a href="https://twitter.com/Will_Huang">Twitter</a>)
</div>
</div>
<div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf2" class="pf w0 h0" data-page-no="2"><div class="pc pc2 w0 h0"><div class="t m0 x3 h1 y3 ff2 fs0 fc0 sc0 ls0 ws0">譯者序</div><div class="t m0 x4 h4 y4 ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _3"> </span><span class="ff5">官方課程的開頭是這樣寫的:<span class="_ _7"></span>“<span class="ff7">Python<span class="_ _3"> </span></span>是一門既容易上手又強大的程式設計語言。<span class="_ _7"></span>”隨</span></div><div class="t m0 x5 h4 y5 ff5 fs3 fc0 sc0 ls0 ws0">著大資料、<span class="_ _8"></span>機器學習與人工智慧的興起,<span class="_ _8"></span><span class="ff7">Python<span class="_ _9"> </span><span class="ff5">語言正在受到越來越廣泛的關注和應用。<span class="_ _8"></span>對愛</span></span></div><div class="t m0 x5 h4 y6 ff5 fs3 fc0 sc0 ls0 ws0">好<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>的人來說,人生苦短,<span class="_ _8"></span><span class="ff7">Python<span class="_ _3"> </span><span class="ff5">當歌!簡潔輕鬆的語法,開箱即用的模組,<span class="_ _4"></span>強大快樂</span></span></div><div class="t m0 x5 h4 y7 ff5 fs3 fc0 sc0 ls0 ws0">的社區,總可以快速建立出簡單高效的解決方案。對於希望快速入門<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>語言的讀者們來</div><div class="t m0 x5 h4 y8 ff5 fs3 fc0 sc0 ls0 ws0">說,市面上眾多像字典一般厚厚的<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>書籍常常讓人感到頭皮發麻。為了讓對<span class="_ _a"> </span><span class="ff7">Python<span class="_ _3"> </span></span>語</div><div class="t m0 x5 h4 y9 ff5 fs3 fc0 sc0 ls0 ws0">言感興趣的讀者能快速入門這門語言,而非過分糾結於<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>細枝末節之處,我們選擇並翻</div><div class="t m0 x5 h4 ya ff5 fs3 fc0 sc0 ls0 ws0">譯了這本<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>課程:<span class="ff8">A<span class="_ _3"> </span>Whirlwind<span class="_ _3"> </span>T<span class="_ _2"></span>our<span class="_ _3"> </span>of<span class="_ _3"> </span>Python<span class="ff5">。</span></span></div><div class="t m0 x4 h4 yb ff5 fs3 fc0 sc0 ls0 ws0">本書作者<span class="_ _3"> </span><span class="ff7">Jak<span class="_ _4"></span>e<span class="_ _9"> </span>V<span class="_ _8"></span>anderPlas<span class="_ _9"> </span><span class="ff5">目前是華盛頓大學<span class="_ _3"> </span></span>eScience<span class="_ _9"> </span><span class="ff5">學院物理科學研究院院長。他既是</span></span></div><div class="t m0 x5 h4 yc ff5 fs3 fc0 sc0 ls0 ws0">一位天文學家,也是一位會議演講達人,活躍於歷年的<span class="_ _3"> </span><span class="ff7">PyData<span class="_ _3"> </span></span>會議,尤其擅長<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>科學</div><div class="t m0 x5 h4 yd ff5 fs3 fc0 sc0 ls0 ws0">計算與資料視覺化。<span class="_ _2"></span><span class="ff7">Jake<span class="_ _9"> </span><span class="ff5">在資料視覺化方面頗有建樹,<span class="_ _2"></span>建立了<span class="_ _9"> </span><span class="ff7">altair</span>、<span class="_ _8"></span><span class="ff7">mpld3<span class="ff5">、<span class="_ _2"></span><span class="ff7">JSAnimation<span class="_ _9"> </span><span class="ff5">可</span></span></span></span></span></span></div><div class="t m0 x5 h4 ye ff5 fs3 fc0 sc0 ls0 ws0">視化程式函式庫,<span class="_ _8"></span>同時為<span class="_ _9"> </span><span class="ff7">NumPy</span>、<span class="_ _2"></span><span class="ff7">Scikit-Learn<span class="ff5">、<span class="_ _8"></span><span class="ff7">Scipy<span class="_ _4"></span><span class="ff5">、<span class="_ _8"></span><span class="ff7">Matplotlib<span class="ff5">、<span class="_ _8"></span><span class="ff7">IPython<span class="_ _9"> </span><span class="ff5">等著名<span class="_ _9"> </span></span>Python<span class="_ _9"> </span><span class="ff5">程式</span></span></span></span></span></span></span></span></div><div class="t m0 x5 h4 yf ff5 fs3 fc0 sc0 ls0 ws0">函式庫做了大量貢獻。</div><div class="t m0 x4 h4 y10 ff5 fs3 fc0 sc0 ls0 ws0">這本僅<span class="_ _3"> </span><span class="ff7">100<span class="_ _3"> </span></span>餘頁的小冊子是一本精簡的<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>程式設計入門課程,介紹了<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>語言的核</div><div class="t m0 x5 h4 y11 ff5 fs3 fc0 sc0 ls0 ws0">心特性以及資料科學領域內一些<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>第三方擴充套件的實際應用,目的是讓熟悉其他程式語</div><div class="t m0 x5 h4 y12 ff5 fs3 fc0 sc0 ls0 ws0">言的資料科學家快速學習<span class="_ _9"> </span><span class="ff7">Python</span>。本書適合從未接觸過<span class="_ _9"> </span><span class="ff7">Python<span class="_ _9"> </span></span>語言但對其他程式語言有一定</div><div class="t m0 x5 h4 y13 ff5 fs3 fc0 sc0 ls0 ws0">了解的讀者,建議讀者跟隨本書親自動手完成每一個例子。</div><div class="t m0 x4 h4 y14 ff5 fs3 fc0 sc0 ls0 ws0">本書翻譯力求在原版基礎上做到精煉、全面、準確地介紹<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>語言。在書中難於理解</div><div class="t m0 x5 h4 y15 ff5 fs3 fc0 sc0 ls0 ws0">的部分我們添加了譯者注,希望能便於讀者理解。</div><div class="t m0 x4 h4 y16 ff5 fs3 fc0 sc0 ls0 ws0">本書由同濟大學蘋果開發俱樂部翻譯、審校,其中朱彥樵負責全書統稿工作;朱彥樵負</div><div class="t m0 x5 h4 y17 ff5 fs3 fc0 sc0 ls0 ws0">責翻譯第<span class="_ _a"> </span><span class="ff7">5</span>、<span class="ff7">11–13</span>、<span class="ff7">15–17<span class="_ _a"> </span></span>章和附錄,蔣宇凱負責翻譯第<span class="_ _a"> </span><span class="ff7">6–9<span class="_ _a"> </span></span>章,李晗負責翻譯第<span class="_ _a"> </span><span class="ff7">1–4</span>、<span class="ff7">10</span>、</div><div class="t m0 x5 h4 y18 ff7 fs3 fc0 sc0 ls0 ws0">14<span class="_ _9"> </span><span class="ff5">章;朱彥樵、李晗、李陽、樑琛、阿思亙負責審稿、校對與潤稿;朱彥樵、李源完成了全書</span></div><div class="t m0 x5 h4 y19 ff5 fs3 fc0 sc0 ls0 ws0">的編輯排版工作。</div><div class="t m0 x4 h4 y1a ff5 fs3 fc0 sc0 ls0 ws0">本書的出版工作得到了同濟大學軟體學院出版基金的大力支援,在此一併感謝。</div><div class="t m0 x4 h4 y1b ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _3"> </span><span class="ff5">語言以及其他第三方套件發展迅速,限於譯者的水平與功底,本書錯謬之處在所難</span></div><div class="t m0 x5 h4 y1c ff5 fs3 fc0 sc0 ls0 ws0">免。希望讀者能夠不吝賜教,幫助本書更加完善。</div><div class="t m0 x6 h5 y1d ff2 fs3 fc0 sc0 ls0 ws0">本書譯者</div><div class="t m0 x6 h6 y1e ff6 fs3 fc0 sc0 ls0 ws0">上海嘉定</div><div class="t m0 x7 h6 y1f ff7 fs3 fc0 sc0 ls0 ws0">2018<span class="_ _9"> </span><span class="ff6">年<span class="_ _3"> </span></span>4<span class="_ _9"> </span><span class="ff6">月</span></div><div class="t m0 x8 h7 y20 ff7 fs3 fc0 sc0 ls0 ws0">i</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf3" class="pf w0 h0" data-page-no="3"><div class="pc pc3 w0 h0"></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf4" class="pf w0 h0" data-page-no="4"><div class="pc pc4 w0 h0"><div class="t m0 x9 h1 y21 ff2 fs0 fc0 sc0 ls0 ws0">目錄</div><div class="t m0 x5 h5 y22 ff9 fs3 fc1 sc0 ls0 ws0">1<span class="_ _c"> </span>Python<span class="_ _a"> </span><span class="ff2">介紹<span class="_ _d"> </span></span><span class="fc0">1</span></div><div class="t m0 xa h4 y23 ff7 fs3 fc1 sc0 ls0 ws0">1.1<span class="_ _e"> </span><span class="ff5">使用範例程式碼<span class="_ _f"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _11"> </span>1</span></div><div class="t m0 xa h4 y24 ff7 fs3 fc1 sc0 ls0 ws0">1.2<span class="_ _e"> </span><span class="ff5">安裝和練習的注意事項<span class="_ _1"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _11"> </span>2</span></div><div class="t m0 xb h4 y25 ff7 fs3 fc1 sc0 ls0 ws0">1.2.1<span class="_ _12"> </span>Python<span class="_ _9"> </span>2<span class="_ _3"> </span><span class="ff5">還是<span class="_ _9"> </span></span>Python<span class="_ _9"> </span>3<span class="_ _13"> </span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _11"> </span>2</span></div><div class="t m0 xb h4 y26 ff7 fs3 fc1 sc0 ls0 ws0">1.2.2<span class="_ _12"> </span><span class="ff5">使用<span class="_ _9"> </span></span>Conda<span class="_ _3"> </span><span class="ff5">安裝<span class="_ _10"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _11"> </span>2</span></div><div class="t m0 xa h4 y27 ff7 fs3 fc1 sc0 ls0 ws0">1.3<span class="_ _e"> </span>Python<span class="_ _9"> </span><span class="ff5">之禪<span class="_ _e"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _11"> </span>3</span></div><div class="t m0 x5 h5 y28 ff9 fs3 fc1 sc0 ls0 ws0">2<span class="_ _c"> </span><span class="ff2">如何執行<span class="_ _a"> </span></span>Python<span class="_ _a"> </span><span class="ff2">程式碼<span class="_ _14"> </span></span><span class="fc0">5</span></div><div class="t m0 xa h4 y29 ff7 fs3 fc1 sc0 ls0 ws0">2.1<span class="_ _e"> </span>Python<span class="_ _9"> </span><span class="ff5">直譯器<span class="_ _15"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _11"> </span>5</span></div><div class="t m0 xa h4 y2a ff7 fs3 fc1 sc0 ls0 ws0">2.2<span class="_ _e"> </span>IPython<span class="_ _9"> </span><span class="ff5">直譯器<span class="_ _1"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _11"> </span>5</span></div><div class="t m0 xa h4 y2b ff7 fs3 fc1 sc0 ls0 ws0">2.3<span class="_ _e"> </span><span class="ff5">自我包含<span class="_ _9"> </span></span>Python<span class="_ _3"> </span><span class="ff5">指令碼<span class="_ _16"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _11"> </span>6</span></div><div class="t m0 xa h7 y2c ff7 fs3 fc1 sc0 ls0 ws0">2.4<span class="_ _e"> </span>Jup<span class="_ _4"></span>yter<span class="_ _9"> </span>Notebook<span class="_ _17"> </span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _11"> </span>7</span></div><div class="t m0 x5 h5 y2d ff9 fs3 fc1 sc0 ls0 ws0">3<span class="_ _c"> </span>Python<span class="_ _a"> </span><span class="ff2">語法速成<span class="_ _18"> </span></span><span class="fc0">9</span></div><div class="t m0 xa h4 y2e ff7 fs3 fc1 sc0 ls0 ws0">3.1<span class="_ _e"> </span><span class="ff5">用<span class="_ _9"> </span><span class="ffa">#<span class="_ _3"> </span></span>標記註釋<span class="_ _3"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _11"> </span>9</span></div><div class="t m0 xa h4 y2f ff7 fs3 fc1 sc0 ls0 ws0">3.2<span class="_ _e"> </span><span class="ff5">用換行符號結束語句<span class="_ _19"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>10</span></div><div class="t m0 xa h4 y30 ff7 fs3 fc1 sc0 ls0 ws0">3.3<span class="_ _e"> </span><span class="ff5">用分號結束語句<span class="_ _9"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>10</span></div><div class="t m0 xa h4 y31 ff7 fs3 fc1 sc0 ls0 ws0">3.4<span class="_ _e"> </span><span class="ff5">縮排:空白字元有意義<span class="_ _1"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _c"> </span>11</span></div><div class="t m0 xa h4 y32 ff7 fs3 fc1 sc0 ls0 ws0">3.5<span class="_ _e"> </span><span class="ff5">行內空白字元無意義<span class="_ _1a"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>12</span></div><div class="t m0 xa h4 y33 ff7 fs3 fc1 sc0 ls0 ws0">3.6<span class="_ _e"> </span><span class="ff5">小括號用於表示式分組或函式呼叫<span class="_ _19"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>12</span></div><div class="t m0 xa h4 y34 ff7 fs3 fc1 sc0 ls0 ws0">3.7<span class="_ _e"> </span><span class="ff5">完成並更進一步<span class="_ _9"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>13</span></div><div class="t m0 x5 h5 y35 ff9 fs3 fc1 sc0 ls0 ws0">4<span class="_ _c"> </span><span class="ff2">基礎<span class="_ _a"> </span></span>Python<span class="_ _a"> </span><span class="ff2">語意:變數和物件<span class="_ _1b"> </span></span><span class="fc0">15</span></div><div class="t m0 xa h4 y36 ff7 fs3 fc1 sc0 ls0 ws0">4.1<span class="_ _e"> </span><span class="ff5">變數是指標<span class="_ _16"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _c"> </span>15</span></div><div class="t m0 xa h4 y37 ff7 fs3 fc1 sc0 ls0 ws0">4.2<span class="_ _e"> </span><span class="ff5">一切皆物件<span class="_ _16"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _c"> </span>17</span></div><div class="t m0 x5 h5 y38 ff9 fs3 fc1 sc0 ls0 ws0">5<span class="_ _c"> </span><span class="ff2">基礎<span class="_ _a"> </span></span>Python<span class="_ _a"> </span><span class="ff2">語法:運算子<span class="_ _1c"> </span></span><span class="fc0">19</span></div><div class="t m0 xa h4 y39 ff7 fs3 fc1 sc0 ls0 ws0">5.1<span class="_ _e"> </span><span class="ff5">算術運算子<span class="_ _16"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _c"> </span>19</span></div><div class="t m0 xa h4 y3a ff7 fs3 fc1 sc0 ls0 ws0">5.2<span class="_ _e"> </span><span class="ff5">位元運算子 </span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>20</span></div><div class="t m0 xa h4 y3b ff7 fs3 fc1 sc0 ls0 ws0">5.3<span class="_ _e"> </span><span class="ff5">賦值運算子<span class="_ _16"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _c"> </span>21</span></div><div class="t m0 xa h4 y3c ff7 fs3 fc1 sc0 ls0 ws0">5.4<span class="_ _e"> </span><span class="ff5">比較運算子<span class="_ _16"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _c"> </span>22</span></div><div class="t m0 xa h4 y3d ff7 fs3 fc1 sc0 ls0 ws0">5.5<span class="_ _e"> </span><span class="ff5">布林運算子<span class="_ _16"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _c"> </span>23</span></div><div class="t m0 xa h4 y3e ff7 fs3 fc1 sc0 ls0 ws0">5.6<span class="_ _e"> </span><span class="ff5">身份和成員運算子<span class="_ _19"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>23</span></div><div class="t m0 xb h4 y3f ff7 fs3 fc1 sc0 ls0 ws0">5.6.1<span class="_ _12"> </span><span class="ff5">身份運算子<span class="_ _1a"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>24</span></div><div class="t m0 xb h4 y40 ff7 fs3 fc1 sc0 ls0 ws0">5.6.2<span class="_ _12"> </span><span class="ff5">成員運算子<span class="_ _1a"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>24</span></div><div class="t m0 x8 h7 y20 ff7 fs3 fc0 sc0 ls0 ws0">i</div><a class="l" href="#pf8" data-dest-detail='[8,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:606.768000px;width:82.651000px;height:12.273000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf8" data-dest-detail='[8,"XYZ",72,170.61,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:589.692000px;width:90.545000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf9" data-dest-detail='[9,"XYZ",72,636,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:573.254000px;width:134.181000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf9" data-dest-detail='[9,"XYZ",72,548.91,null]'><div class="d m1" style="border-style:none;position:absolute;left:113.455000px;bottom:556.816000px;width:152.182000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf9" data-dest-detail='[9,"XYZ",72,388.35,null]'><div class="d m1" style="border-style:none;position:absolute;left:113.455000px;bottom:540.379000px;width:116.726000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pfa" data-dest-detail='[10,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:523.941000px;width:85.545000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pfc" data-dest-detail='[12,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:497.115000px;width:130.470000px;height:12.272000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pfc" data-dest-detail='[12,"XYZ",72,536.94,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:480.038000px;width:96.454000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pfc" data-dest-detail='[12,"XYZ",72,186.77,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:463.600000px;width:100.394000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pfd" data-dest-detail='[13,"XYZ",72,334.93,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:447.163000px;width:121.909000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pfe" data-dest-detail='[14,"XYZ",72,741.07,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:431.715000px;width:111.334000px;height:9.636000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf10" data-dest-detail='[16,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:403.899000px;width:104.470000px;height:12.272000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf10" data-dest-detail='[16,"XYZ",72,118.47,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:386.822000px;width:92.636000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf11" data-dest-detail='[17,"XYZ",72,595.21,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:370.384000px;width:112.363000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf11" data-dest-detail='[17,"XYZ",72,274.02,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:353.947000px;width:101.454000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf12" data-dest-detail='[18,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:337.509000px;width:134.181000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf13" data-dest-detail='[19,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:321.072000px;width:123.272000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf13" data-dest-detail='[19,"XYZ",72,438.32,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:304.634000px;width:188.727000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf14" data-dest-detail='[20,"XYZ",72,275.96,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:288.196000px;width:101.454000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf16" data-dest-detail='[22,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:261.370000px;width:174.106000px;height:12.273000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf16" data-dest-detail='[22,"XYZ",72,538.11,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:244.293000px;width:79.636000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf18" data-dest-detail='[24,"XYZ",72,674.07,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:227.856000px;width:79.636000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf1a" data-dest-detail='[26,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:201.029000px;width:152.288000px;height:12.273000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf1a" data-dest-detail='[26,"XYZ",72,569.24,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:183.952000px;width:79.636000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf1b" data-dest-detail='[27,"XYZ",72,591.95,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:167.515000px;width:68.727000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf1c" data-dest-detail='[28,"XYZ",72,625.31,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:151.077000px;width:79.636000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf1d" data-dest-detail='[29,"XYZ",72,724.81,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:134.640000px;width:79.636000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf1e" data-dest-detail='[30,"XYZ",72,692.72,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:118.202000px;width:79.636000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf1e" data-dest-detail='[30,"XYZ",72,147.69,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:101.764000px;width:112.363000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf1f" data-dest-detail='[31,"XYZ",72,660.33,null]'><div class="d m1" style="border-style:none;position:absolute;left:113.455000px;bottom:85.327000px;width:89.454000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf1f" data-dest-detail='[31,"XYZ",72,127.42,null]'><div class="d m1" style="border-style:none;position:absolute;left:113.455000px;bottom:68.889000px;width:89.454000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf5" class="pf w0 h0" data-page-no="5"><div class="pc pc5 w0 h0"><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">ii<span class="_ _1d"> </span><span class="ff6">目錄</span></div><div class="t m0 x5 h5 y42 ff9 fs3 fc1 sc0 ls0 ws0">6<span class="_ _c"> </span><span class="ff2">內建型別:簡單變數型別<span class="_ _1e"> </span></span><span class="fc0">27</span></div><div class="t m0 xa h4 y43 ff7 fs3 fc1 sc0 ls0 ws0">6.1<span class="_ _e"> </span><span class="ff5">整數<span class="_ _1a"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>27</span></div><div class="t m0 xa h4 y44 ff7 fs3 fc1 sc0 ls0 ws0">6.2<span class="_ _e"> </span><span class="ff5">浮點數<span class="_ _1"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _c"> </span>28</span></div><div class="t m0 xb h4 y45 ff7 fs3 fc1 sc0 ls0 ws0">6.2.1<span class="_ _12"> </span><span class="ff5">浮點數精度<span class="_ _1a"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>29</span></div><div class="t m0 xa h4 y46 ff7 fs3 fc1 sc0 ls0 ws0">6.3<span class="_ _e"> </span><span class="ff5">複數<span class="_ _1a"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>30</span></div><div class="t m0 xa h4 y47 ff7 fs3 fc1 sc0 ls0 ws0">6.4<span class="_ _e"> </span><span class="ff5">字串型別<span class="_ _16"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _c"> </span>30</span></div><div class="t m0 xa h4 y48 ff7 fs3 fc1 sc0 ls0 ws0">6.5<span class="_ _e"> </span><span class="ffa">None<span class="_ _9"> </span><span class="ff5">型別<span class="_ _1f"> </span></span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>31</span></div><div class="t m0 xa h4 y49 ff7 fs3 fc1 sc0 ls0 ws0">6.6<span class="_ _e"> </span><span class="ff5">布林型別 </span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>32</span></div><div class="t m0 x5 h5 y4a ff9 fs3 fc1 sc0 ls0 ws0">7<span class="_ _c"> </span><span class="ff2">內建資料結構<span class="_ _20"> </span></span><span class="fc0">35</span></div><div class="t m0 xa h4 y4b ff7 fs3 fc1 sc0 ls0 ws0">7.1<span class="_ _e"> </span><span class="ff5">列表<span class="_ _1a"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>35</span></div><div class="t m0 xb h4 y4c ff7 fs3 fc1 sc0 ls0 ws0">7.1.1<span class="_ _12"> </span><span class="ff5">列表索引與切片<span class="_ _e"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>36</span></div><div class="t m0 xa h4 y4d ff7 fs3 fc1 sc0 ls0 ws0">7.2<span class="_ _e"> </span><span class="ff5">Tuple<span class="_ _1a"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>38</span></div><div class="t m0 xa h4 y4e ff7 fs3 fc1 sc0 ls0 ws0">7.3<span class="_ _e"> </span><span class="ff5">字典<span class="_ _1a"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>40</span></div><div class="t m0 xa h4 y4f ff7 fs3 fc1 sc0 ls0 ws0">7.4<span class="_ _e"> </span><span class="ff5">集合<span class="_ _1a"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>41</span></div><div class="t m0 xa h4 y50 ff7 fs3 fc1 sc0 ls0 ws0">7.5<span class="_ _e"> </span><span class="ff5">其他專用資料結構<span class="_ _19"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>41</span></div><div class="t m0 x5 h5 y51 ff9 fs3 fc1 sc0 ls0 ws0">8<span class="_ _c"> </span><span class="ff2">控制流程<span class="_ _21"> </span></span><span class="fc0">43</span></div><div class="t m0 xa h4 y52 ff7 fs3 fc1 sc0 ls0 ws0">8.1<span class="_ _e"> </span><span class="ff5">條件語句:<span class="ffa">if</span></span>-<span class="ffa">elif</span>-<span class="ffa">else<span class="_ _9"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _c"> </span>43</span></div><div class="t m0 xa h4 y53 ff7 fs3 fc1 sc0 ls0 ws0">8.2<span class="_ _e"> </span><span class="ffa">for<span class="_ _9"> </span><span class="ff5">迴圈<span class="_ _9"> </span></span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>43</span></div><div class="t m0 xa h4 y54 ff7 fs3 fc1 sc0 ls0 ws0">8.3<span class="_ _e"> </span><span class="ffa">while<span class="_ _9"> </span><span class="ff5">迴圈<span class="_ _22"> </span></span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>44</span></div><div class="t m0 xa h4 y55 ff7 fs3 fc1 sc0 ls0 ws0">8.4<span class="_ _e"> </span><span class="ff5">調整迴圈執行:<span class="ffa">break<span class="_ _9"> </span></span>和<span class="_ _3"> </span><span class="ffa">continue<span class="_ _a"> </span></span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>45</span></div><div class="t m0 xa h4 y56 ff7 fs3 fc1 sc0 ls0 ws0">8.5<span class="_ _e"> </span><span class="ff5">帶有<span class="_ _9"> </span><span class="ffa">else<span class="_ _3"> </span></span>塊的迴圈<span class="_ _19"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>45</span></div><div class="t m0 x5 h5 y57 ff9 fs3 fc1 sc0 ls0 ws0">9<span class="_ _c"> </span><span class="ff2">定義和使用函式<span class="_ _23"> </span></span><span class="fc0">47</span></div><div class="t m0 xa h4 y58 ff7 fs3 fc1 sc0 ls0 ws0">9.1<span class="_ _e"> </span><span class="ff5">使用函式 </span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>47</span></div><div class="t m0 xa h4 y59 ff7 fs3 fc1 sc0 ls0 ws0">9.2<span class="_ _e"> </span><span class="ff5">定義函式 </span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>47</span></div><div class="t m0 xa h4 y5a ff7 fs3 fc1 sc0 ls0 ws0">9.3<span class="_ _e"> </span><span class="ff5">預設參數 </span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>48</span></div><div class="t m0 xa h4 y5b ff7 fs3 fc1 sc0 ls0 ws0">9.4<span class="_ _e"> </span><span class="ff5">可變參數:<span class="ffa">*args<span class="_ _9"> </span></span>和<span class="_ _3"> </span><span class="ffa">**kwargs<span class="_ _22"> </span></span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>49</span></div><div class="t m0 xa h4 y5c ff7 fs3 fc1 sc0 ls0 ws0">9.5<span class="_ _e"> </span><span class="ff5">匿名(</span>Lam<span class="_ _4"></span>bda<span class="ff5">)函式<span class="_ _19"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _c"> </span>50</span></div><div class="t m0 x5 h5 y5d ff9 fs3 fc1 sc0 ls0 ws0">10<span class="_ _3"> </span><span class="ff2">錯誤和異常<span class="_ _25"> </span></span><span class="fc0">53</span></div><div class="t m0 xa h4 y5e ff7 fs3 fc1 sc0 ls0 ws0">10.1<span class="_ _10"> </span><span class="ff5">執行時錯誤<span class="_ _16"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>53</span></div><div class="t m0 xa h4 y5f ff7 fs3 fc1 sc0 ls0 ws0">10.2<span class="_ _10"> </span><span class="ff5">捕獲異常:<span class="ffa">try<span class="_ _3"> </span></span>和<span class="_ _9"> </span><span class="ffa">except<span class="_ _26"> </span></span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>55</span></div><div class="t m0 xa h4 y60 ff7 fs3 fc1 sc0 ls0 ws0">10.3<span class="_ _10"> </span><span class="ff5">丟擲異常:<span class="ffa">raise<span class="_ _10"> </span></span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>57</span></div><div class="t m0 xa h4 y61 ff7 fs3 fc1 sc0 ls0 ws0">10.4<span class="_ _10"> </span><span class="ff5">深入探究異常<span class="_ _f"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>58</span></div><div class="t m0 xb h4 y62 ff7 fs3 fc1 sc0 ls0 ws0">10.4.1<span class="_ _26"> </span><span class="ff5">訪問錯誤資訊<span class="_ _10"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _c"> </span>58</span></div><div class="t m0 xb h4 y63 ff7 fs3 fc1 sc0 ls0 ws0">10.4.2<span class="_ _26"> </span><span class="ff5">定義自定義異常<span class="_ _27"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>59</span></div><div class="t m0 xa h7 y64 ff7 fs3 fc1 sc0 ls0 ws0">10.5<span class="_ _10"> </span><span class="ffa">try</span>...<span class="ffa">except</span>...<span class="ffa">else</span>...<span class="ffa">finally<span class="_ _f"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>60</span></div><div class="t m0 x5 h5 y65 ff9 fs3 fc1 sc0 ls0 ws0">11<span class="_ _3"> </span><span class="ff2">迭代器<span class="_ _21"> </span></span><span class="fc0">61</span></div><div class="t m0 xa h4 y66 ff7 fs3 fc1 sc0 ls0 ws0">11.1<span class="_ _10"> </span><span class="ff5">遍歷列表<span class="_ _e"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>61</span></div><div class="t m0 xa h4 y67 ff7 fs3 fc1 sc0 ls0 ws0">11.2<span class="_ _10"> </span><span class="ff5">不總是列表的列表:<span class="ffa">range()<span class="_ _19"> </span></span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>62</span></div><div class="t m0 xa h4 y40 ff7 fs3 fc1 sc0 ls0 ws0">11.3<span class="_ _10"> </span><span class="ff5">實用迭代器介紹<span class="_ _3"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>63</span></div><a class="l" href="#pf22" data-dest-detail='[34,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:756.459000px;width:136.364000px;height:12.273000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf22" data-dest-detail='[34,"XYZ",72,383.64,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:739.209000px;width:46.909000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf23" data-dest-detail='[35,"XYZ",72,450.69,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:722.598000px;width:57.818000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf24" data-dest-detail='[36,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:113.455000px;bottom:705.987000px;width:89.454000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf25" data-dest-detail='[37,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:689.377000px;width:46.909000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf25" data-dest-detail='[37,"XYZ",72,202.48,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:672.766000px;width:79.636000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf26" data-dest-detail='[38,"XYZ",72,240.42,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:656.155000px;width:73.454000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf27" data-dest-detail='[39,"XYZ",72,584.17,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:639.544000px;width:68.727000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf2a" data-dest-detail='[42,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:612.429000px;width:81.818000px;height:12.272000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf2a" data-dest-detail='[42,"XYZ",72,435.51,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:595.179000px;width:46.909000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf2b" data-dest-detail='[43,"XYZ",72,399.91,null]'><div class="d m1" style="border-style:none;position:absolute;left:113.455000px;bottom:578.568000px;width:111.272000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf2d" data-dest-detail='[45,"XYZ",72,285.64,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:561.957000px;width:46.909000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf2f" data-dest-detail='[47,"XYZ",72,530.2,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:545.346000px;width:46.909000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf30" data-dest-detail='[48,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:528.735000px;width:46.909000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf30" data-dest-detail='[48,"XYZ",72,150.69,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:512.124000px;width:112.363000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf32" data-dest-detail='[50,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:485.009000px;width:49.091000px;height:12.273000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf32" data-dest-detail='[50,"XYZ",72,528.9,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:467.759000px;width:144.181000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf32" data-dest-detail='[50,"XYZ",72,138.4,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:451.148000px;width:67.727000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf33" data-dest-detail='[51,"XYZ",72,233.5,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:434.537000px;width:79.181000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf34" data-dest-detail='[52,"XYZ",72,741.54,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:417.926000px;width:194.090000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf34" data-dest-detail='[52,"XYZ",72,151.7,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:401.316000px;width:120.727000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf36" data-dest-detail='[54,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:374.200000px;width:92.727000px;height:12.273000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf36" data-dest-detail='[54,"XYZ",72,553.69,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:356.950000px;width:68.727000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf36" data-dest-detail='[54,"XYZ",72,179.68,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:340.339000px;width:68.727000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf37" data-dest-detail='[55,"XYZ",72,358.1,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:323.729000px;width:68.727000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf38" data-dest-detail='[56,"XYZ",72,586.65,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:307.118000px;width:172.272000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf39" data-dest-detail='[57,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:290.507000px;width:129.484000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf3c" data-dest-detail='[60,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:263.391000px;width:70.909000px;height:12.273000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf3c" data-dest-detail='[60,"XYZ",72,434.55,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:246.141000px;width:79.636000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf3e" data-dest-detail='[62,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:229.531000px;width:149.363000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf40" data-dest-detail='[64,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:212.920000px;width:108.272000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf41" data-dest-detail='[65,"XYZ",72,210.31,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:196.309000px;width:90.545000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf41" data-dest-detail='[65,"XYZ",72,127.42,null]'><div class="d m1" style="border-style:none;position:absolute;left:113.455000px;bottom:179.698000px;width:100.363000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf42" data-dest-detail='[66,"XYZ",72,580.1,null]'><div class="d m1" style="border-style:none;position:absolute;left:113.455000px;bottom:163.087000px;width:111.272000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf43" data-dest-detail='[67,"XYZ",72,741.07,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:147.163000px;width:166.908000px;height:9.294000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf44" data-dest-detail='[68,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:119.361000px;width:49.091000px;height:12.273000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf44" data-dest-detail='[68,"XYZ",72,423.75,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:102.111000px;width:68.727000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf45" data-dest-detail='[69,"XYZ",72,480.69,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:85.500000px;width:163.363000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf46" data-dest-detail='[70,"XYZ",72,371.13,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:68.889000px;width:101.454000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf6" class="pf w0 h0" data-page-no="6"><div class="pc pc6 w0 h0"><div class="t m0 x5 h6 y41 ff6 fs3 fc0 sc0 ls0 ws0">目錄<span class="_ _28"> </span><span class="ff7">iii</span></div><div class="t m0 xb h7 y42 ff7 fs3 fc1 sc0 ls0 ws0">11.3.1<span class="_ _26"> </span><span class="ffa">enumerate<span class="_ _17"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>63</span></div><div class="t m0 xb h7 y43 ff7 fs3 fc1 sc0 ls0 ws0">11.3.2<span class="_ _26"> </span><span class="ffa">zip<span class="_ _e"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>64</span></div><div class="t m0 xb h4 y44 ff7 fs3 fc1 sc0 ls0 ws0">11.3.3<span class="_ _26"> </span><span class="ffa">map<span class="_ _3"> </span><span class="ff5">和<span class="_ _9"> </span></span>filter<span class="_ _29"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>64</span></div><div class="t m0 xb h4 y45 ff7 fs3 fc1 sc0 ls0 ws0">11.3.4<span class="_ _26"> </span><span class="ff5">迭代器作為函式參數<span class="_ _1f"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>65</span></div><div class="t m0 xa h4 y46 ff7 fs3 fc1 sc0 ls0 ws0">11.4<span class="_ _10"> </span><span class="ff5">專用迭代器:<span class="ffa">itertools<span class="_ _2a"> </span></span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>66</span></div><div class="t m0 x5 h5 y68 ff9 fs3 fc1 sc0 ls0 ws0">12<span class="_ _3"> </span><span class="ff2">列表推導<span class="_ _2b"> </span></span><span class="fc0">67</span></div><div class="t m0 xa h4 y69 ff7 fs3 fc1 sc0 ls0 ws0">12.1<span class="_ _10"> </span><span class="ff5">基礎列表推導<span class="_ _f"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>67</span></div><div class="t m0 xa h4 y6a ff7 fs3 fc1 sc0 ls0 ws0">12.2<span class="_ _10"> </span><span class="ff5">多重迭代<span class="_ _e"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>68</span></div><div class="t m0 xa h4 y4a ff7 fs3 fc1 sc0 ls0 ws0">12.3<span class="_ _10"> </span><span class="ff5">條件控制迭代<span class="_ _f"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>68</span></div><div class="t m0 xa h4 y4b ff7 fs3 fc1 sc0 ls0 ws0">12.4<span class="_ _10"> </span><span class="ff5">條件控制值<span class="_ _16"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>68</span></div><div class="t m0 x5 h5 y6b ff9 fs3 fc1 sc0 ls0 ws0">13<span class="_ _3"> </span><span class="ff2">產生器<span class="_ _21"> </span></span><span class="fc0">71</span></div><div class="t m0 xa h4 y6c ff7 fs3 fc1 sc0 ls0 ws0">13.1<span class="_ _10"> </span><span class="ff5">產生器表示式<span class="_ _f"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>71</span></div><div class="t m0 xb h4 y6d ff7 fs3 fc1 sc0 ls0 ws0">13.1.1<span class="_ _26"> </span><span class="ff5">列表推導使用方括號,而產生器表示式使用圓括號<span class="_ _15"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>71</span></div><div class="t m0 xb h4 y6e ff7 fs3 fc1 sc0 ls0 ws0">13.1.2<span class="_ _26"> </span><span class="ff5">列表是值的集合,而產生器是產生值的方法<span class="_ _0"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>71</span></div><div class="t m0 xb h4 y6f ff7 fs3 fc1 sc0 ls0 ws0">13.1.3<span class="_ _26"> </span><span class="ff5">列表可以被迭代多次,而產生器表示式是一次性的<span class="_ _15"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>73</span></div><div class="t m0 xa h4 y51 ff7 fs3 fc1 sc0 ls0 ws0">13.2<span class="_ _10"> </span><span class="ff5">產生器函式:使用關鍵字<span class="_ _3"> </span><span class="ffa">yield<span class="_ _3"> </span></span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>74</span></div><div class="t m0 xa h4 y52 ff7 fs3 fc1 sc0 ls0 ws0">13.3<span class="_ _10"> </span><span class="ff5">案例學習:質數產生器<span class="_ _1"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>74</span></div><div class="t m0 x5 h5 y70 ff9 fs3 fc1 sc0 ls0 ws0">14<span class="_ _3"> </span><span class="ff2">模組和套件<span class="_ _2b"> </span></span><span class="fc0">77</span></div><div class="t m0 xa h4 y71 ff7 fs3 fc1 sc0 ls0 ws0">14.1<span class="_ _10"> </span><span class="ff5">匯入模組:<span class="ffa">import<span class="_ _3"> </span></span>語句<span class="_ _2c"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>77</span></div><div class="t m0 xb h4 y72 ff7 fs3 fc1 sc0 ls0 ws0">14.1.1<span class="_ _26"> </span><span class="ff5">明確模組匯入<span class="_ _10"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _c"> </span>77</span></div><div class="t m0 xb h4 y73 ff7 fs3 fc1 sc0 ls0 ws0">14.1.2<span class="_ _26"> </span><span class="ff5">明確模組匯入並取別名<span class="_ _3"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>77</span></div><div class="t m0 xb h4 y57 ff7 fs3 fc1 sc0 ls0 ws0">14.1.3<span class="_ _26"> </span><span class="ff5">明確匯入模組內容<span class="_ _15"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>78</span></div><div class="t m0 xb h4 y58 ff7 fs3 fc1 sc0 ls0 ws0">14.1.4<span class="_ _26"> </span><span class="ff5">隱式匯入模組內容<span class="_ _15"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>78</span></div><div class="t m0 xa h4 y59 ff7 fs3 fc1 sc0 ls0 ws0">14.2<span class="_ _10"> </span><span class="ff5">匯入<span class="_ _3"> </span></span>Python<span class="_ _9"> </span><span class="ff5">標準函式庫<span class="_ _16"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>79</span></div><div class="t m0 xa h4 y5a ff7 fs3 fc1 sc0 ls0 ws0">14.3<span class="_ _10"> </span><span class="ff5">匯入第三方模組<span class="_ _3"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>79</span></div><div class="t m0 x5 h5 y74 ff9 fs3 fc1 sc0 ls0 ws0">15<span class="_ _3"> </span><span class="ff2">字串處理和正則表示式<span class="_ _1e"> </span></span><span class="fc0">81</span></div><div class="t m0 xa h4 y75 ff7 fs3 fc1 sc0 ls0 ws0">15.1<span class="_ _10"> </span>Python<span class="_ _3"> </span><span class="ff5">簡易字串操作<span class="_ _1a"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>81</span></div><div class="t m0 xb h4 y5d ff7 fs3 fc1 sc0 ls0 ws0">15.1.1<span class="_ _26"> </span><span class="ff5">字串格式化:大小寫轉換<span class="_ _0"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>81</span></div><div class="t m0 xb h4 y5e ff7 fs3 fc1 sc0 ls0 ws0">15.1.2<span class="_ _26"> </span><span class="ff5">格式化字串:新增和刪除空格<span class="_ _27"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>82</span></div><div class="t m0 xb h4 y5f ff7 fs3 fc1 sc0 ls0 ws0">15.1.3<span class="_ _26"> </span><span class="ff5">查詢和替換子字串<span class="_ _1f"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>84</span></div><div class="t m0 xb h4 y60 ff7 fs3 fc1 sc0 ls0 ws0">15.1.4<span class="_ _26"> </span><span class="ff5">拆分和分割字串<span class="_ _15"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>85</span></div><div class="t m0 xa h4 y61 ff7 fs3 fc1 sc0 ls0 ws0">15.2<span class="_ _10"> </span><span class="ff5">格式化字串<span class="_ _f"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>86</span></div><div class="t m0 xa h4 y62 ff7 fs3 fc1 sc0 ls0 ws0">15.3<span class="_ _10"> </span><span class="ff5">使用正則表示式靈活地匹配字串模式<span class="_ _1"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>88</span></div><div class="t m0 xb h4 y63 ff7 fs3 fc1 sc0 ls0 ws0">15.3.1<span class="_ _26"> </span><span class="ff5">一個更複雜的例子<span class="_ _15"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>89</span></div><div class="t m0 xb h4 y64 ff7 fs3 fc1 sc0 ls0 ws0">15.3.2<span class="_ _26"> </span><span class="ff5">正則表示式語法基礎<span class="_ _1f"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>90</span></div><div class="t m0 xb h4 y76 ff7 fs3 fc1 sc0 ls0 ws0">15.3.3<span class="_ _26"> </span><span class="ff5">關於正則表示式的更多資源<span class="_ _0"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>94</span></div><div class="t m0 x5 h5 y66 ff9 fs3 fc1 sc0 ls0 ws0">16<span class="_ _3"> </span><span class="ff2">資料科學工具概覽<span class="_ _2d"> </span></span><span class="fc0">95</span></div><div class="t m0 xa h4 y67 ff7 fs3 fc1 sc0 ls0 ws0">16.1<span class="_ _10"> </span>NumPy<span class="ff5">:</span>Python<span class="_ _3"> </span><span class="ff5">數值計算<span class="_ _19"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>95</span></div><div class="t m0 xa h4 y40 ff7 fs3 fc1 sc0 ls0 ws0">16.2<span class="_ _10"> </span>Pandas<span class="ff5">:標籤化的面向行資料儲存(Labeled Column-oriented Data)<span class="_ _0"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>97</span></div><a class="l" href="#pf46" data-dest-detail='[70,"XYZ",72,290.39,null]'><div class="d m1" style="border-style:none;position:absolute;left:113.455000px;bottom:758.931000px;width:86.454000px;height:6.870000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf47" data-dest-detail='[71,"XYZ",72,548.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:113.455000px;bottom:739.896000px;width:52.090000px;height:9.294000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf47" data-dest-detail='[71,"XYZ",72,236.57,null]'><div class="d m1" style="border-style:none;position:absolute;left:113.455000px;bottom:722.598000px;width:104.636000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf48" data-dest-detail='[72,"XYZ",72,511.6,null]'><div class="d m1" style="border-style:none;position:absolute;left:113.455000px;bottom:705.987000px;width:133.090000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf49" data-dest-detail='[73,"XYZ",72,622.16,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:689.377000px;width:142.091000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf4a" data-dest-detail='[74,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:662.261000px;width:60.000000px;height:12.273000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf4a" data-dest-detail='[74,"XYZ",72,457.08,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:645.011000px;width:90.545000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf4b" data-dest-detail='[75,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:628.400000px;width:68.727000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf4b" data-dest-detail='[75,"XYZ",72,558.94,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:611.790000px;width:90.545000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf4b" data-dest-detail='[75,"XYZ",72,175.88,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:595.179000px;width:79.636000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf4e" data-dest-detail='[78,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:568.063000px;width:49.091000px;height:12.273000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf4e" data-dest-detail='[78,"XYZ",72,583.79,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:550.813000px;width:90.545000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf4e" data-dest-detail='[78,"XYZ",72,502.68,null]'><div class="d m1" style="border-style:none;position:absolute;left:113.455000px;bottom:534.202000px;width:274.909000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf4e" data-dest-detail='[78,"XYZ",72,145.28,null]'><div class="d m1" style="border-style:none;position:absolute;left:113.455000px;bottom:517.592000px;width:242.181000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf50" data-dest-detail='[80,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:113.455000px;bottom:500.981000px;width:274.909000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf51" data-dest-detail='[81,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:484.370000px;width:177.363000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf51" data-dest-detail='[81,"XYZ",72,153.5,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:467.759000px;width:134.181000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf54" data-dest-detail='[84,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:440.644000px;width:60.000000px;height:12.272000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf54" data-dest-detail='[84,"XYZ",72,549.63,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:423.394000px;width:139.454000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf54" data-dest-detail='[84,"XYZ",72,464.72,null]'><div class="d m1" style="border-style:none;position:absolute;left:113.455000px;bottom:406.783000px;width:100.363000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf54" data-dest-detail='[84,"XYZ",72,285.19,null]'><div class="d m1" style="border-style:none;position:absolute;left:113.455000px;bottom:390.172000px;width:143.999000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf55" data-dest-detail='[85,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:113.455000px;bottom:373.561000px;width:122.181000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf55" data-dest-detail='[85,"XYZ",72,607.99,null]'><div class="d m1" style="border-style:none;position:absolute;left:113.455000px;bottom:356.950000px;width:122.181000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf56" data-dest-detail='[86,"XYZ",72,581.32,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:340.339000px;width:121.909000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf56" data-dest-detail='[86,"XYZ",72,305.69,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:323.729000px;width:101.454000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf58" data-dest-detail='[88,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:296.613000px;width:136.364000px;height:12.273000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf58" data-dest-detail='[88,"XYZ",72,273.01,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:279.363000px;width:140.091000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf58" data-dest-detail='[88,"XYZ",72,161.69,null]'><div class="d m1" style="border-style:none;position:absolute;left:113.455000px;bottom:262.752000px;width:165.818000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf59" data-dest-detail='[89,"XYZ",72,334.76,null]'><div class="d m1" style="border-style:none;position:absolute;left:113.455000px;bottom:246.141000px;width:187.636000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf5b" data-dest-detail='[91,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:113.455000px;bottom:229.531000px;width:133.090000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf5c" data-dest-detail='[92,"XYZ",72,366,null]'><div class="d m1" style="border-style:none;position:absolute;left:113.455000px;bottom:212.920000px;width:122.181000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf5d" data-dest-detail='[93,"XYZ",72,282.06,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:196.309000px;width:90.545000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf5f" data-dest-detail='[95,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:179.698000px;width:210.545000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf60" data-dest-detail='[96,"XYZ",72,176.53,null]'><div class="d m1" style="border-style:none;position:absolute;left:113.455000px;bottom:163.087000px;width:122.181000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf61" data-dest-detail='[97,"XYZ",72,272.91,null]'><div class="d m1" style="border-style:none;position:absolute;left:113.455000px;bottom:146.476000px;width:133.090000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf65" data-dest-detail='[101,"XYZ",72,546.13,null]'><div class="d m1" style="border-style:none;position:absolute;left:113.455000px;bottom:129.865000px;width:165.818000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf66" data-dest-detail='[102,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:102.750000px;width:103.636000px;height:12.273000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf66" data-dest-detail='[102,"XYZ",72,492.28,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:85.500000px;width:154.787000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf68" data-dest-detail='[104,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:68.889000px;width:190.454000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf7" class="pf w0 h0" data-page-no="7"><div class="pc pc7 w0 h0"><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">iv<span class="_ _2e"> </span><span class="ff6">目錄</span></div><div class="t m0 xa h4 y42 ff7 fs3 fc1 sc0 ls0 ws0">16.3<span class="_ _10"> </span>Matplotlib<span class="ff5">:</span>MatLab<span class="_ _3"> </span><span class="ff5">風格的科學視覺化<span class="_ _16"> </span></span><span class="fc0">.<span class="_ _1"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _17"> </span>98</span></div><div class="t m0 xa h4 y77 ff7 fs3 fc1 sc0 ls0 ws0">16.4<span class="_ _10"> </span>SciPy<span class="ff5">:</span>Python<span class="_ _3"> </span><span class="ff5">科學計算<span class="_ _17"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _24"> </span>99</span></div><div class="t m0 xa h4 y78 ff7 fs3 fc1 sc0 ls0 ws0">16.5<span class="_ _10"> </span><span class="ff5">其他資料科學函式庫<span class="_ _3"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _1"> </span>100</span></div><div class="t m0 x5 h5 y79 ff9 fs3 fc1 sc0 ls0 ws0">17<span class="_ _3"> </span><span class="ff2">更多學習資源<span class="_ _2f"> </span></span><span class="fc0">101</span></div><div class="t m0 x5 h5 y7a ff9 fs3 fc1 sc0 ls0 ws0">A<span class="_ _26"> </span><span class="ff2">附錄:插圖程式碼<span class="_ _30"> </span></span><span class="fc0">103</span></div><div class="t m0 xa h4 y7b ff7 fs3 fc1 sc0 ls0 ws0">A.1<span class="_ _16"> </span><span class="ff5">第<span class="_ _9"> </span></span>7<span class="_ _9"> </span><span class="ff5">章:列表索引<span class="_ _2c"> </span></span><span class="fc0">.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _10"> </span>.<span class="_ _1"> </span>103</span></div><div class="t m0 x5 h5 y7c ff2 fs3 fc1 sc0 ls0 ws0">中英文術語對照表<span class="_ _31"> </span><span class="ff9 fc0">105</span></div><a class="l" href="#pf69" data-dest-detail='[105,"XYZ",72,413.5,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:755.820000px;width:215.546000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf6a" data-dest-detail='[106,"XYZ",72,490.64,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:739.561000px;width:145.394000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf6b" data-dest-detail='[107,"XYZ",72,428.69,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:723.302000px;width:101.454000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf6c" data-dest-detail='[108,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:696.773000px;width:81.818000px;height:12.273000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf6e" data-dest-detail='[110,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:669.605000px;width:92.727000px;height:12.273000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf6e" data-dest-detail='[110,"XYZ",72,493.2,null]'><div class="d m1" style="border-style:none;position:absolute;left:88.364000px;bottom:652.707000px;width:114.181000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf70" data-dest-detail='[112,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:626.178000px;width:87.273000px;height:12.273000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf8" class="pf w0 h0" data-page-no="8"><div class="pc pc8 w0 h0"><div class="t m0 xc h1 y7d ff2 fs0 fc0 sc0 ls0 ws0">第<span class="_ _0"> </span><span class="ff1">1<span class="_ _0"> </span></span>章<span class="_ _32"> </span><span class="ff1">Python<span class="_ _0"> </span></span>介紹</div><div class="t m0 x4 h4 y7e ff5 fs3 fc0 sc0 ls0 ws0">在<span class="_ _3"> </span><span class="ff7">20<span class="_ _3"> </span></span>世紀<span class="_ _3"> </span><span class="ff7">80<span class="_ _3"> </span></span>年代後期,<span class="ff7">Python<span class="_ _3"> </span></span>作為一種適合教學使用的腳本語言,已經成為學術界和</div><div class="t m0 x5 h4 y7f ff5 fs3 fc0 sc0 ls0 ws0">工業界中許多程式員、工程師、研究人員和資料科學家們必不可少的工具。作為一名專注於構</div><div class="t m0 x5 h4 y80 ff5 fs3 fc0 sc0 ls0 ws0">建和推廣適合資料密集型科學研究開放工具的天文學家,我發現無論是從海量天文資料中抽取</div><div class="t m0 x5 h4 y81 ff5 fs3 fc0 sc0 ls0 ws0">出有意義的資訊、<span class="_ _8"></span>在網路上爬取資料並進行清洗,<span class="_ _8"></span>還是自動化地處理日常的工作,<span class="_ _8"></span><span class="ff7">Python<span class="_ _9"> </span><span class="ff5">幾乎</span></span></div><div class="t m0 x5 h4 y82 ff5 fs3 fc0 sc0 ls0 ws0">都可以完美地解決我每天遇到的各種各樣的問題。</div><div class="t m0 x4 h4 y83 ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _9"> </span><span class="ff5">的魅力在於它的語法簡潔但不失優雅,以及<span class="_ _9"> </span></span>Python<span class="_ _9"> </span><span class="ff5">龐大的生態環境可在不同領域</span></div><div class="t m0 x5 h4 y84 ff5 fs3 fc0 sc0 ls0 ws0">都發揮作用所帶來的便利。比如,大部分在科學計算和資料科學中用到的<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>程式碼都使用</div><div class="t m0 x5 h4 y85 ff5 fs3 fc0 sc0 ls0 ws0">到了幾種功能強大且成熟的第三方<span class="ff6">套件</span>(<span class="ff7">pac<span class="_ _4"></span>kage<span class="ff5">)<span class="_ _33"></span>:</span></span></div><div class="t m0 x4 h4 y86 ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff7 fc2">NumPy</span><span class="ff5">:提供對多維資料矩陣進行高效計算和儲存的方法。</span></div><div class="t m0 x4 h4 y87 ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff7 fc2">SciPy</span><span class="ff5">:包含一系列數學工具,比如數值積分和數值插值計算。</span></div><div class="t m0 x4 h4 y88 ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff7 fc2">P<span class="_ _4"></span>andas<span class="ff5 fc0">:提供資料幀物件以及一組強大的方法來處理、過濾、分組和轉換資料。</span></span></div><div class="t m0 x4 h4 y89 ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff7 fc2">Matplotlib</span><span class="ff5">:提供建立高品質資料圖表的介面。</span></div><div class="t m0 x4 h4 y8a ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff7 fc2">Scikit-Learn</span><span class="ff5">:提供一個用於常用機器學習演算法的通用工具套件。</span></div><div class="t m0 x4 h4 y8b ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff7 fc2">IPython<span class="_ _9"> </span>/<span class="_ _3"> </span>Jup<span class="_ _8"></span>yter<span class="ff5 fc0">:提供一種互動式筆記本環境,對於科研分析以及建立互動式可執行</span></span></div><div class="t m0 xd h4 y8c ff5 fs3 fc0 sc0 ls0 ws0">文件非常有用。例如,這本書的手稿完全由<span class="_ _9"> </span><span class="ff7">Jupyter<span class="_ _9"> </span>Notebook<span class="_ _3"> </span></span>寫成。</div><div class="t m0 x4 h4 y8d ff5 fs3 fc0 sc0 ls0 ws0">有了這些強大的套件作為支援,其他工具套件好像沒有什麼必要了。如果你需要完成一項科學</div><div class="t m0 x5 h4 y8e ff5 fs3 fc0 sc0 ls0 ws0">分析或者資料分析的任務,一般來說都有人已經寫了這樣一個工具。</div><div class="t m0 x4 h4 y8f ff5 fs3 fc0 sc0 ls0 ws0">為了能更好的感受到<span class="_ _9"> </span><span class="ff7">Python<span class="_ _34"> </span></span>在資料科學領域的力量,<span class="_ _35"></span>首先要熟悉<span class="_ _34"> </span><span class="ff7">Python<span class="_ _9"> </span></span>的語法。<span class="_ _35"></span>我常常</div><div class="t m0 x5 h4 y90 ff5 fs3 fc0 sc0 ls0 ws0">遇到一些有著<span class="_ _35"></span>(有時是廣泛的)<span class="_ _2"></span>其他程式語言背景<span class="_ _35"></span>(比如<span class="_ _36"> </span><span class="ff7">MA<span class="_ _2"></span>TLAB<span class="ff5">、<span class="_ _37"></span><span class="ff7">IDL<span class="ff5">、<span class="_ _37"></span><span class="ff7">R<span class="ff5">、<span class="_ _38"></span><span class="ff7">Ja<span class="_ _4"></span>v<span class="_ _8"></span>a<span class="_ _36"> </span><span class="ff5">和<span class="_ _36"> </span></span>C++<span class="_ _36"> </span><span class="ff5">等)</span></span></span></span></span></span></span></span></div><div class="t m0 x5 h4 y91 ff5 fs3 fc0 sc0 ls0 ws0">的學生和同事一直在尋找一份簡潔、全面,並且符合他們的水平而不是從零開始的<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>教</div><div class="t m0 x5 h4 y92 ff5 fs3 fc0 sc0 ls0 ws0">程,這份課程就試圖填補這個空白。</div><div class="t m0 x4 h4 y93 ff5 fs3 fc0 sc0 ls0 ws0">因此,本書不是為了全面地介紹程式設計,也不是一份完整的<span class="_ _9"> </span><span class="ff7">Python<span class="_ _9"> </span></span>語言說明。<span class="_ _33"></span>(如果你對此</div><div class="t m0 x5 h4 y94 ff5 fs3 fc0 sc0 ls0 ws0">有需求,可以看看第<span class="_ _3"> </span><span class="ff7 fc1">17<span class="_ _3"> </span></span>章<span class="fc1">更多學習資源</span>。<span class="_ _7"></span>)相對地,這本書為你提供了一場<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>的旋風之</div><div class="t m0 x5 h4 y95 ff5 fs3 fc0 sc0 ls0 ws0">旅,它涉及到<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>核心的語法、語意、內建的資料型別和資料結構、函數定義、控制流程語</div><div class="t m0 x5 h4 y96 ff5 fs3 fc0 sc0 ls0 ws0">句和這門語言的其他方面。我希望讀者能順著這本書走下去,為探索資料科學打下堅實的基礎。</div><div class="t m0 x5 h8 y97 ff1 fs1 fc0 sc0 ls0 ws0">1.1<span class="_ _39"> </span><span class="ff2">使用範例程式碼</span></div><div class="t m0 x4 h4 y98 ff5 fs3 fc0 sc0 ls0 ws0">本書中使用到的一些補充資料<span class="_ _2"></span>(範例程式碼或者練習等)<span class="_ _35"></span>可以從這裡下載:<span class="_ _3a"></span><span class="ffa fc2">https://github.</span></div><div class="t m0 x5 h4 y99 ffa fs3 fc2 sc0 ls0 ws0">com/jakevdp/WhirlwindTourOfPython/<span class="ff5 fc0">。本書是要幫你完成工作的。一般來說,如果本書提</span></div><div class="t m0 x5 h4 y9a ff5 fs3 fc0 sc0 ls0 ws0">供了範例程式碼,你可以把它用在你的程序或文件中。除非你使用了很大一部分程式碼,否則無</div><div class="t m0 x5 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">須聯系我們獲得許可。比如,用本書的幾個程式碼片段寫一個程式就無須獲得許可,銷售或分</div><div class="t m0 xe h7 y20 ff7 fs3 fc0 sc0 ls0 ws0">1</div><a class="l" href="http://numpy.org"><div class="d m1" style="border-style:none;position:absolute;left:107.758000px;bottom:468.967000px;width:36.515000px;height:9.636000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="http://scipy.org"><div class="d m1" style="border-style:none;position:absolute;left:107.758000px;bottom:452.708000px;width:27.121000px;height:9.636000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="http://pandas.pydata.org"><div class="d m1" style="border-style:none;position:absolute;left:107.758000px;bottom:438.570000px;width:34.454000px;height:7.515000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="http://matplotlib.org"><div class="d m1" style="border-style:none;position:absolute;left:107.758000px;bottom:420.190000px;width:50.606000px;height:9.636000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="http://scikit-learn.org"><div class="d m1" style="border-style:none;position:absolute;left:107.758000px;bottom:406.052000px;width:58.061000px;height:7.515000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="http://jupyter.org"><div class="d m1" style="border-style:none;position:absolute;left:107.758000px;bottom:387.066000px;width:88.215000px;height:10.909000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf6c" data-dest-detail='[108,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:174.607000px;bottom:225.223000px;width:10.909000px;height:6.870000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf6c" data-dest-detail='[108,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:200.193000px;bottom:222.112000px;width:65.924000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="https://github.com/jakevdp/WhirlwindTourOfPython/"><div class="d m1" style="border-style:none;position:absolute;left:437.367000px;bottom:118.353000px;width:85.909000px;height:10.000000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="https://github.com/jakevdp/WhirlwindTourOfPython/"><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:102.094000px;width:194.727000px;height:10.000000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf9" class="pf w0 h0" data-page-no="9"><div class="pc pc9 w0 h0"><img class="bi x5 y9b w1 h9" alt="" src="images/bg9.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">2<span class="_ _3b"> </span><span class="ffc">1.<span class="_ _16"> </span>PYTHON<span class="_ _9"> </span><span class="ff6">介紹</span></span></div><div class="t m0 x5 h4 y42 ff5 fs3 fc0 sc0 ls0 ws0">發<span class="_ _3"> </span><span class="ff7">O’Reilly<span class="_ _3"> </span></span>圖書的範例光碟則需要獲得許可;引用本書中的範例程式碼回答問題無須獲得許可,</div><div class="t m0 x5 h4 y77 ff5 fs3 fc0 sc0 ls0 ws0">將書中大量的程式碼放到你的產品文件中則需要獲得許可。</div><div class="t m0 x4 h4 y9c ff5 fs3 fc0 sc0 ls0 ws0">我們很希望但並不強制要求你在引用本書內容時加上引用說明。引用說明一般包括書名、</div><div class="t m0 x5 h4 y9d ff5 fs3 fc0 sc0 ls0 ws0">作者、出版社和<span class="_ _a"> </span><span class="ff7">ISBN</span>,比如:<span class="ff8">A<span class="_ _13"> </span>Whirlwind<span class="_ _13"> </span>T<span class="_ _8"></span>our<span class="_ _13"> </span>of<span class="_ _13"> </span>Python<span class="_ _1"> </span><span class="ff7">by<span class="_ _3"> </span>Jake<span class="_ _a"> </span>V<span class="_ _8"></span>anderPlas<span class="_ _a"> </span>(O’Reilly).</span></span></div><div class="t m0 x5 h4 y9e ff7 fs3 fc0 sc0 ls0 ws0">Cop<span class="_ _4"></span>yrigh<span class="_ _4"></span>t<span class="_ _9"> </span>2016<span class="_ _3"> </span>O’Reilly<span class="_ _9"> </span>Media,<span class="_ _9"> </span>Inc.,<span class="_ _3"> </span>978-1-491-96465-1<span class="ff5">。</span></div><div class="t m0 x4 h4 y9f ff5 fs3 fc0 sc0 ls0 ws0">如<span class="_ _34"> </span>果<span class="_ _36"> </span>你<span class="_ _34"> </span>覺<span class="_ _34"> </span>得<span class="_ _34"> </span>你<span class="_ _34"> </span>使<span class="_ _34"> </span>用<span class="_ _34"> </span>代<span class="_ _36"> </span>碼<span class="_ _34"> </span>示<span class="_ _34"> </span>例<span class="_ _34"> </span>的<span class="_ _34"> </span>場<span class="_ _34"> </span>景<span class="_ _36"> </span>不<span class="_ _34"> </span>符<span class="_ _34"> </span>合<span class="_ _34"> </span>上<span class="_ _34"> </span>述<span class="_ _34"> </span>規<span class="_ _34"> </span>定,<span class="_ _36"> </span>請<span class="_ _34"> </span>隨<span class="_ _34"> </span>時<span class="_ _34"> </span>通<span class="_ _34"> </span>過<span class="_ _0"> </span><span class="ff7 fc2">permis-</span></div><div class="t m0 x5 h4 ya0 ff7 fs3 fc2 sc0 ls0 ws0">sions@oreilly<span class="_ _2"></span>.com<span class="_ _3"> </span><span class="ff5 fc0">與我們聯絡。</span></div><div class="t m0 x5 h8 ya1 ff1 fs1 fc0 sc0 ls0 ws0">1.2<span class="_ _39"> </span><span class="ff2">安裝和練習的注意事項</span></div><div class="t m0 x4 h4 ya2 ff5 fs3 fc0 sc0 ls0 ws0">無論是使用<span class="_ _9"> </span><span class="ff7">Windows<span class="_ _4"></span><span class="ff5">、<span class="_ _8"></span><span class="ff7">Linux<span class="_ _34"> </span><span class="ff5">還是<span class="_ _3"> </span></span>macOS<span class="ff5">,<span class="_ _8"></span>安裝<span class="_ _9"> </span><span class="ff7">Python<span class="_ _9"> </span></span>和使用科學計算的函式庫套件都非常</span></span></span></span></div><div class="t m0 x5 h4 ya3 ff5 fs3 fc0 sc0 ls0 ws0">簡單。本節將簡要介紹配置你的環境時的一些注意事項。</div><div class="t m0 x5 ha ya4 ff1 fs2 fc0 sc0 ls0 ws0">1.2.1<span class="_ _3c"> </span>Python<span class="_ _13"> </span>2<span class="_ _13"> </span><span class="ff2">還是<span class="_ _13"> </span></span>Python<span class="_ _13"> </span>3</div><div class="t m0 x4 h4 ya5 ff5 fs3 fc0 sc0 ls0 ws0">這本書使用了<span class="_ _3"> </span><span class="ff7">Python<span class="_ _9"> </span>3<span class="_ _3"> </span></span>的語法,其中包含與<span class="_ _3"> </span><span class="ff7">Python<span class="_ _9"> </span>2.<span class="ffd">x<span class="_ _3"> </span></span></span>系列不相容的語言增強功能。雖</div><div class="t m0 x5 h4 ya6 ff5 fs3 fc0 sc0 ls0 ws0">然<span class="_ _a"> </span><span class="ff7">Python<span class="_ _13"> </span>3.0<span class="_ _13"> </span></span>在<span class="_ _13"> </span><span class="ff7">2008<span class="_ _a"> </span></span>年首次發布,但採用的速度相對較慢,特別是在科學研究和<span class="_ _13"> </span><span class="ff7">W<span class="_ _2"></span>eb<span class="_ _13"> </span><span class="ff5">開</span></span></div><div class="t m0 x5 h4 ya7 ff5 fs3 fc0 sc0 ls0 ws0">發領域。這主要是因為許多基本套件和工具套件需要一些時間才能與新的語言兼容。然而,</div><div class="t m0 x5 h4 ya8 ff5 fs3 fc0 sc0 ls0 ws0">自<span class="_ _9"> </span><span class="ff7">2014<span class="_ _9"> </span></span>年初以來,<span class="_ _8"></span>資料科學生態中一些最重要的工具的穩定版本已經與<span class="_ _34"> </span><span class="ff7">Python<span class="_ _9"> </span>2<span class="_ _9"> </span></span>和<span class="_ _9"> </span><span class="ff7">3<span class="_ _9"> </span></span>完全兼</div><div class="t m0 x5 h4 ya9 ff5 fs3 fc0 sc0 ls0 ws0">容,因此本書將使用較新的<span class="_ _a"> </span><span class="ff7">Python<span class="_ _3"> </span>3<span class="_ _3"> </span></span>語法。即使是在這種情況下,本書中絕大多數的程式碼片</div><div class="t m0 x5 h4 yaa ff5 fs3 fc0 sc0 ls0 ws0">段也可以在<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span>2<span class="_ _3"> </span></span>中不加修改地執行。在使用<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span>2<span class="_ _3"> </span></span>不相容的語法的情況下</div><div class="t m0 xf hb yab ffe fs4 fc1 sc0 ls0 ws0">1</div><div class="t m0 x10 h4 yaa ff5 fs3 fc0 sc0 ls0 ws0">,我將盡</div><div class="t m0 x5 h4 yac ff5 fs3 fc0 sc0 ls0 ws0">力明確地註明。</div><div class="t m0 x5 ha yad ff1 fs2 fc0 sc0 ls0 ws0">1.2.2<span class="_ _3c"> </span><span class="ff2">使用<span class="_ _13"> </span></span>Conda<span class="_ _13"> </span><span class="ff2">安裝</span></div><div class="t m0 x4 h4 yae ff5 fs3 fc0 sc0 ls0 ws0">雖然有很多種方法安裝<span class="_ _3"> </span><span class="ff7">Python</span>,但是我較為推薦跨平台的<span class="_ _3"> </span><span class="ff7">Anaconda</span>,特別是如果你希望</div><div class="t m0 x5 h4 yaf ff5 fs3 fc0 sc0 ls0 ws0">最終使用前面提到的資料科學工具的話。<span class="ff7">Anaconda<span class="_ _9"> </span></span>的發行版有下面兩種版本:</div><div class="t m0 x4 h4 yb0 ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff7 fc2">Miniconda<span class="_ _a"> </span></span><span class="ff5">提供了一個<span class="_ _3"> </span><span class="ff7">Python<span class="_ _13"> </span></span>直譯器和一個叫做<span class="_ _a"> </span><span class="ffa">conda<span class="_ _a"> </span></span>的命令行工具,它提供了面</span></div><div class="t m0 xd h4 yb1 ff5 fs3 fc0 sc0 ls0 ws0">向<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>套件的跨平台套件管理功能,就像<span class="_ _9"> </span><span class="ff7">Linux<span class="_ _34"> </span></span>使用者熟悉的<span class="_ _3"> </span><span class="ffa">apt<span class="_ _9"> </span></span>或者<span class="_ _3"> </span><span class="ffa">yum</span>。</div><div class="t m0 x4 h4 yb2 ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff7 fc2">Anaconda<span class="_ _9"> </span></span><span class="ff5">包括<span class="_ _3"> </span><span class="ff7">Python<span class="_ _9"> </span></span>和<span class="_ _9"> </span><span class="ffa">conda</span>,以及可新增的科學計算套件。</span></div><div class="t m0 x4 h4 yb3 ff7 fs3 fc0 sc0 ls0 ws0">Anaconda<span class="_ _9"> </span><span class="ff5">所包含的套件都可以手動地安裝在<span class="_ _34"> </span></span>Miniconda<span class="_ _9"> </span><span class="ff5">中。<span class="_ _2"></span>出於這個原因,<span class="_ _2"></span>我推薦從<span class="_ _9"> </span><span class="ff7">Mini-</span></span></div><div class="t m0 x5 h4 yb4 ff7 fs3 fc0 sc0 ls0 ws0">conda<span class="_ _9"> </span><span class="ff5">開始。要下載並安裝<span class="_ _9"> </span></span>Miniconda<span class="ff5">,確保你已經選擇了<span class="_ _9"> </span></span>Python<span class="_ _3"> </span>3<span class="_ _9"> </span><span class="ff5">版本,然後安裝<span class="_ _9"> </span></span>IPython</div><div class="t m0 x5 h4 yb5 ff7 fs3 fc0 sc0 ls0 ws0">Notebook<span class="_ _9"> </span><span class="ff5">套件:</span></div><div class="t m0 x11 hc yb6 ffa fs3 fc0 sc0 ls0 ws0">[~]$<span class="_ _2a"> </span>conda<span class="_ _2a"> </span>install<span class="_ _2a"> </span>ipython-notebook</div><div class="t m0 x4 h4 yb7 ff5 fs3 fc0 sc0 ls0 ws0">想獲得關於<span class="_ _3"> </span><span class="ffa">conda<span class="_ _9"> </span></span>的資訊,例如如何建立或者使用<span class="_ _3"> </span><span class="ffa">conda<span class="_ _9"> </span></span>環境,請檢視上面給出的<span class="_ _3"> </span><span class="ff7">Mini-</span></div><div class="t m0 x5 h4 yb8 ff7 fs3 fc0 sc0 ls0 ws0">conda<span class="_ _9"> </span><span class="ff5">連結。</span></div><div class="t m0 x12 hd yb9 fff fs5 fc0 sc0 ls0 ws0">1</div><div class="t m0 xa he yba ff5 fs6 fc0 sc0 ls0 ws0">譯者注:<span class="ff10">Python<span class="_ _34"> </span></span>核心團隊計劃在<span class="_ _34"> </span><span class="ff10">2020<span class="_ _9"> </span></span>年停止支援<span class="_ _9"> </span><span class="ff10">Python<span class="_ _34"> </span>2</span>。已經有許多開源專案,如<span class="_ _34"> </span><span class="ff10">Numpy</span>、<span class="ff10">sklearn<span class="_ _34"> </span></span>等</div><div class="t m0 x5 he y40 ff5 fs6 fc0 sc0 ls0 ws0">也已經發表宣告將在<span class="_ _34"> </span><span class="ff10">2020<span class="_ _36"> </span></span>年以前放棄對於<span class="_ _34"> </span><span class="ff10">Python<span class="_ _34"> </span>2<span class="_ _34"> </span></span>的支援。</div><a class="l" href="mailto:[email protected]"><div class="d m1" style="border-style:none;position:absolute;left:487.731000px;bottom:673.490000px;width:35.545000px;height:8.991000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="mailto:[email protected]"><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:657.231000px;width:82.576000px;height:9.637000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf9" data-dest-detail='[9,"XYZ",88.59,98.3,null]'><div class="d m1" style="border-style:none;position:absolute;left:474.401000px;bottom:429.866000px;width:4.234000px;height:5.136000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="http://conda.pydata.org/miniconda.html"><div class="d m1" style="border-style:none;position:absolute;left:107.758000px;bottom:297.028000px;width:50.000000px;height:7.515000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="https://www.continuum.io/downloads"><div class="d m1" style="border-style:none;position:absolute;left:107.758000px;bottom:264.510000px;width:47.575000px;height:7.515000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pfa" class="pf w0 h0" data-page-no="a"><div class="pc pca w0 h0"><img class="bi x5 y40 w1 hf" alt="" src="images/bga.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">1.3.<span class="_ _16"> </span>PYTHON<span class="_ _9"> </span><span class="ff6">之禪<span class="_ _3d"> </span><span class="ff7">3</span></span></div><div class="t m0 x5 h8 ybb ff1 fs1 fc0 sc0 ls0 ws0">1.3<span class="_ _39"> </span>Python<span class="_ _10"> </span><span class="ff2">之禪</span></div><div class="t m0 x4 h4 ybc ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _34"> </span><span class="ff5">愛好者經常很快指出<span class="_ _34"> </span></span>Python<span class="_ _34"> </span><span class="ff5">是多麼<span class="_ _3e"></span>“直觀”<span class="_ _3f"></span>“美麗”<span class="_ _40"></span>或<span class="_ _3e"></span>“有趣”<span class="_ _41"></span>。<span class="_ _40"></span>雖然我傾向於同意</span></div><div class="t m0 x5 h4 ybd ff5 fs3 fc0 sc0 ls0 ws0">這個觀點,<span class="_ _3e"></span>但我也認識到,<span class="_ _3e"></span>美麗、<span class="_ _3e"></span>直觀和樂趣往往與對語言的熟悉並存,<span class="_ _3e"></span>因此對於那些熟悉其他</div><div class="t m0 x5 h4 ybe ff5 fs3 fc0 sc0 ls0 ws0">語言的人來說,<span class="_ _3e"></span>這種絢麗的情感可能會顯得有些沾沾自喜。<span class="_ _3e"></span>不過,<span class="_ _35"></span>我希望如果你給<span class="_ _34"> </span><span class="ff7">Python<span class="_ _9"> </span></span>一個</div><div class="t m0 x5 h4 ybf ff5 fs3 fc0 sc0 ls0 ws0">機會,<span class="_ _3e"></span>你將會看到這樣的印象可能來自哪裡。<span class="_ _3e"></span>並且,<span class="_ _35"></span>如果你真的想深入探究那些推動<span class="_ _34"> </span><span class="ff7">Python<span class="_ _9"> </span></span>進</div><div class="t m0 x5 h4 yc0 ff5 fs3 fc0 sc0 ls0 ws0">階用戶的程式設計理念的話,<span class="ff7">Python<span class="_ _3"> </span></span>直譯器中就有一個很好的復活節小彩蛋——你只需</div><div class="t m0 x5 h4 yc1 ff5 fs3 fc0 sc0 ls0 ws0">閉上眼睛,冥想幾分鐘,然後執行<span class="_ _9"> </span><span class="ffa">import<span class="_ _2a"> </span>this</span>:</div><div class="t m0 x11 hc yc2 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[1]:<span class="_ _2a"> </span><span class="ff11 fc4">import<span class="_ _2a"> </span><span class="fc5">this</span></span></div><div class="t m0 x11 hc yc3 ffa fs3 fc0 sc0 ls0 ws0">The<span class="_ _2a"> </span>Zen<span class="_ _2a"> </span>of<span class="_ _2a"> </span>Python,<span class="_ _2a"> </span>by<span class="_ _2a"> </span>Tim<span class="_ _2a"> </span>Peters</div><div class="t m0 x11 hc yc4 ffa fs3 fc0 sc0 ls0 ws0">Beautiful<span class="_ _2a"> </span>is<span class="_ _2a"> </span>better<span class="_ _2a"> </span>than<span class="_ _2a"> </span>ugly.</div><div class="t m0 x11 hc yc5 ffa fs3 fc0 sc0 ls0 ws0">Explicit<span class="_ _2a"> </span>is<span class="_ _2a"> </span>better<span class="_ _2a"> </span>than<span class="_ _2a"> </span>implicit.</div><div class="t m0 x11 hc yc6 ffa fs3 fc0 sc0 ls0 ws0">Simple<span class="_ _2a"> </span>is<span class="_ _2a"> </span>better<span class="_ _2a"> </span>than<span class="_ _2a"> </span>complex.</div><div class="t m0 x11 hc yc7 ffa fs3 fc0 sc0 ls0 ws0">Complex<span class="_ _2a"> </span>is<span class="_ _2a"> </span>better<span class="_ _2a"> </span>than<span class="_ _2a"> </span>complicated.</div><div class="t m0 x11 hc yc8 ffa fs3 fc0 sc0 ls0 ws0">Flat<span class="_ _2a"> </span>is<span class="_ _2a"> </span>better<span class="_ _2a"> </span>than<span class="_ _2a"> </span>nested.</div><div class="t m0 x11 hc yc9 ffa fs3 fc0 sc0 ls0 ws0">Sparse<span class="_ _2a"> </span>is<span class="_ _2a"> </span>better<span class="_ _2a"> </span>than<span class="_ _2a"> </span>dense.</div><div class="t m0 x11 hc yca ffa fs3 fc0 sc0 ls0 ws0">Readability<span class="_ _2a"> </span>counts.</div><div class="t m0 x11 hc ycb ffa fs3 fc0 sc0 ls0 ws0">Special<span class="_ _2a"> </span>cases<span class="_ _2a"> </span>aren<span class="ff12">'</span>t<span class="_ _2a"> </span>special<span class="_ _2a"> </span>enough<span class="_ _2a"> </span>to<span class="_ _2a"> </span>break<span class="_ _2a"> </span>the<span class="_ _2a"> </span>rules.</div><div class="t m0 x11 hc ycc ffa fs3 fc0 sc0 ls0 ws0">Although<span class="_ _2a"> </span>practicality<span class="_ _2a"> </span>beats<span class="_ _2a"> </span>purity.</div><div class="t m0 x11 hc ycd ffa fs3 fc0 sc0 ls0 ws0">Errors<span class="_ _2a"> </span>should<span class="_ _2a"> </span>never<span class="_ _2a"> </span>pass<span class="_ _2a"> </span>silently.</div><div class="t m0 x11 hc yce ffa fs3 fc0 sc0 ls0 ws0">Unless<span class="_ _2a"> </span>explicitly<span class="_ _2a"> </span>silenced.</div><div class="t m0 x11 hc ycf ffa fs3 fc0 sc0 ls0 ws0">In<span class="_ _2a"> </span>the<span class="_ _2a"> </span>face<span class="_ _2a"> </span>of<span class="_ _2a"> </span>ambiguity,<span class="_ _2a"> </span>refuse<span class="_ _2a"> </span>the<span class="_ _2a"> </span>temptation<span class="_ _2a"> </span>to<span class="_ _2a"> </span>guess.</div><div class="t m0 x11 hc yd0 ffa fs3 fc0 sc0 ls0 ws0">There<span class="_ _2a"> </span>should<span class="_ _2a"> </span>be<span class="_ _2a"> </span>one--<span class="_ _2a"> </span>and<span class="_ _2a"> </span>preferably<span class="_ _2a"> </span>only<span class="_ _2a"> </span>one<span class="_ _2a"> </span>--obvious<span class="_ _2a"> </span>way<span class="_ _2a"> </span>to<span class="_ _2a"> </span>do<span class="_ _2a"> </span>it.</div><div class="t m0 x11 hc yd1 ffa fs3 fc0 sc0 ls0 ws0">Although<span class="_ _2a"> </span>that<span class="_ _2a"> </span>way<span class="_ _2a"> </span>may<span class="_ _2a"> </span>not<span class="_ _2a"> </span>be<span class="_ _2a"> </span>obvious<span class="_ _2a"> </span>at<span class="_ _2a"> </span>first<span class="_ _2a"> </span>unless<span class="_ _2a"> </span>you<span class="ff12">'</span>re<span class="_ _2a"> </span>Dutch.</div><div class="t m0 x11 hc yd2 ffa fs3 fc0 sc0 ls0 ws0">Now<span class="_ _2a"> </span>is<span class="_ _2a"> </span>better<span class="_ _2a"> </span>than<span class="_ _2a"> </span>never.</div><div class="t m0 x11 hc yd3 ffa fs3 fc0 sc0 ls0 ws0">Although<span class="_ _2a"> </span>never<span class="_ _2a"> </span>is<span class="_ _2a"> </span>often<span class="_ _2a"> </span>better<span class="_ _2a"> </span>than<span class="_ _2a"> </span>*right*<span class="_ _2a"> </span>now.</div><div class="t m0 x11 hc yd4 ffa fs3 fc0 sc0 ls0 ws0">If<span class="_ _2a"> </span>the<span class="_ _2a"> </span>implementation<span class="_ _2a"> </span>is<span class="_ _2a"> </span>hard<span class="_ _2a"> </span>to<span class="_ _2a"> </span>explain,<span class="_ _2a"> </span>it<span class="ff12">'</span>s<span class="_ _2a"> </span>a<span class="_ _2a"> </span>bad<span class="_ _2a"> </span>idea.</div><div class="t m0 x11 hc yd5 ffa fs3 fc0 sc0 ls0 ws0">If<span class="_ _2a"> </span>the<span class="_ _2a"> </span>implementation<span class="_ _2a"> </span>is<span class="_ _2a"> </span>easy<span class="_ _2a"> </span>to<span class="_ _2a"> </span>explain,<span class="_ _2a"> </span>it<span class="_ _2a"> </span>may<span class="_ _2a"> </span>be<span class="_ _2a"> </span>a<span class="_ _2a"> </span>good<span class="_ _2a"> </span>idea.</div><div class="t m0 x11 hc yd6 ffa fs3 fc0 sc0 ls0 ws0">Namespaces<span class="_ _2a"> </span>are<span class="_ _2a"> </span>one<span class="_ _2a"> </span>honking<span class="_ _2a"> </span>great<span class="_ _2a"> </span>idea<span class="_ _2a"> </span>--<span class="_ _2a"> </span>let<span class="ff12">'</span>s<span class="_ _2a"> </span>do<span class="_ _2a"> </span>more<span class="_ _2a"> </span>of<span class="_ _2a"> </span>those!</div><div class="t m0 x4 h4 yd7 ff5 fs3 fc0 sc0 ls0 ws0">伴隨著《<span class="ff7">Python<span class="_ _9"> </span></span>之禪》</div><div class="t m0 x1 hb yd8 ffe fs4 fc1 sc0 ls0 ws0">2</div><div class="t m0 x13 h4 yd7 ff5 fs3 fc0 sc0 ls0 ws0">,讓我們開始我們的<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>之旅!</div><div class="t m0 x12 hd yd9 fff fs5 fc0 sc0 ls0 ws0">2</div><div class="t m0 xa he yda ff5 fs6 fc0 sc0 ls0 ws0">譯者注:<span class="_ _42"></span>《<span class="ff10">Python<span class="_ _3"> </span></span>之禪》是<span class="_ _9"> </span><span class="ff10">Python<span class="_ _3"> </span>2.2<span class="_ _3"> </span></span>及以後版本中的一個小彩蛋,是<span class="_ _3"> </span><span class="ff10">Tim<span class="_ _9"> </span>Peters<span class="_ _9"> </span></span>在<span class="_ _3"> </span><span class="ff10">1999<span class="_ _3"> </span></span>年<span class="_ _9"> </span><span class="ff10">6<span class="_ _3"> </span></span>月左右</div><div class="t m0 x5 he ydb ff5 fs6 fc0 sc0 ls0 ws0">所寫的<span class="_ _3"> </span><span class="ff10">20<span class="_ _9"> </span></span>個影響<span class="_ _9"> </span><span class="ff10">Python<span class="_ _9"> </span></span>程式語言設計的原理組成的集合,其中<span class="_ _3"> </span><span class="ff10">19<span class="_ _9"> </span></span>個被包括在這裡。<span class="_ _43"></span>《<span class="ff10">Python<span class="_ _9"> </span></span>之禪》實際上</div><div class="t m0 x5 he yba ff5 fs6 fc0 sc0 ls0 ws0">是<span class="_ _36"> </span><span class="ff10">Python<span class="_ _34"> </span></span>增強提案<span class="_ _4"></span>(<span class="ff10">Python<span class="_ _36"> </span>Enhancement<span class="_ _36"> </span>Proposals,<span class="_ _34"> </span>PEP</span>)中的第<span class="_ _36"> </span><span class="ff10">20<span class="_ _36"> </span></span>號提案<span class="_ _36"> </span><span class="ff10">PEP<span class="_ _34"> </span>20</span>。<span class="_ _8"></span>有意思的是,<span class="_ _8"></span>如果你查</div><div class="t m0 x5 he y40 ff5 fs6 fc0 sc0 ls0 ws0">看<span class="_ _34"> </span><span class="ff13">this<span class="_ _36"> </span></span>模組的原始碼,你會發現它完全違背了這些原則<span class="_ _26"> </span>。</div><a class="l" href="#pfa" data-dest-detail='[10,"XYZ",88.59,124.6,null]'><div class="d m1" style="border-style:none;position:absolute;left:208.818000px;bottom:227.092000px;width:4.234000px;height:5.137000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pfb" class="pf w0 h0" data-page-no="b"><div class="pc pcb w0 h0"></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pfc" class="pf w0 h0" data-page-no="c"><div class="pc pcc w0 h0"><img class="bi x5 ydc w1 h10" alt="" src="images/bgc.png"/><div class="t m0 x14 h1 y3 ff2 fs0 fc0 sc0 ls0 ws0">第<span class="_ _0"> </span><span class="ff1">2<span class="_ _0"> </span></span>章<span class="_ _32"> </span>如何執行<span class="_ _0"> </span><span class="ff1">Python<span class="_ _0"> </span></span>程式碼</div><div class="t m0 x4 h4 ydd ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _36"> </span><span class="ff5">是一種靈活的語言,<span class="_ _3a"></span>你可以根據你手中的任務選擇不同的方法來使用它。<span class="_ _37"></span><span class="ff7">Python<span class="_ _34"> </span><span class="ff5">和</span></span></span></div><div class="t m0 x5 h4 yde ff5 fs3 fc0 sc0 ls0 ws0">其他程式語言的一個區別在於<span class="_ _36"> </span><span class="ff7">Python<span class="_ _36"> </span></span>是<span class="ff6">直譯型<span class="_ _2"></span><span class="ff5">(<span class="ff7">in<span class="_ _8"></span>terpreted<span class="ff5">)<span class="_ _2"></span>而不是<span class="ff6">編譯型<span class="_ _35"></span><span class="ff5">(<span class="ff7">compiled</span>)<span class="_ _2"></span>語言。</span></span></span></span></span></span></div><div class="t m0 x5 h4 ydf ff5 fs3 fc0 sc0 ls0 ws0">這意味著<span class="_ _36"> </span><span class="ff7">Python<span class="_ _36"> </span></span>是一行一行執行的,<span class="_ _44"></span>因此它具有<span class="_ _36"> </span><span class="ff7">F<span class="_ _2"></span>ortran<span class="ff5">、<span class="_ _45"></span><span class="ff7">C<span class="_ _36"> </span><span class="ff5">或者<span class="_ _36"> </span></span>Ja<span class="_ _8"></span>v<span class="_ _8"></span>a</span></span></span></div><div class="t m0 x15 hb ye0 ffe fs4 fc1 sc0 ls0 ws0">1</div><div class="t m0 x16 h4 ydf ff5 fs3 fc0 sc0 ls0 ws0">這些編譯型語言無法做</div><div class="t m0 x5 h4 ye1 ff5 fs3 fc0 sc0 ls0 ws0">到的<span class="_ _3a"></span>“互動”<span class="_ _37"></span>特性。<span class="_ _37"></span>這一節會介紹</div><div class="t m0 x17 h7 ye2 ff7 fs3 fc0 sc0 ls0 ws0">4</div><div class="t m0 x18 h4 ye1 ff5 fs3 fc0 sc0 ls0 ws0">種執行</div><div class="t m0 x19 h7 ye2 ff7 fs3 fc0 sc0 ls0 ws0">Python</div><div class="t m0 x1a h4 ye1 ff5 fs3 fc0 sc0 ls0 ws0">程式碼的主要方法:<span class="_ _38"></span>使用</div><div class="t m0 x1b h7 ye2 ff7 fs3 fc0 sc0 ls0 ws0">Python</div><div class="t m0 x1c h4 ye1 ff5 fs3 fc0 sc0 ls0 ws0">或</div><div class="t m0 x6 h7 ye2 ff7 fs3 fc0 sc0 ls0 ws0">IPython</div><div class="t m0 x1d h6 ye1 ff6 fs3 fc0 sc0 ls0 ws0">解</div><div class="t m0 x5 h4 ye3 ff6 fs3 fc0 sc0 ls0 ws0">釋器<span class="ff5">(<span class="ff7">in<span class="_ _4"></span>terpreter<span class="ff5">)<span class="_ _33"></span>、執行<span class="ff6">自我包含指令碼</span>(<span class="ff7">self-con<span class="_ _4"></span>tained<span class="_ _9"> </span>script<span class="ff5">)或者使用<span class="_ _3"> </span></span>Jup<span class="_ _8"></span>yter<span class="_ _3"> </span>Notebook<span class="ff5">。</span></span></span></span></span></div><div class="t m0 x5 h8 ye4 ff1 fs1 fc0 sc0 ls0 ws0">2.1<span class="_ _39"> </span>Python<span class="_ _10"> </span><span class="ff2">直譯器</span></div><div class="t m0 x4 h4 ye5 ff5 fs3 fc0 sc0 ls0 ws0">逐行運行<span class="_ _3"> </span><span class="ff7">Python<span class="_ _a"> </span></span>程式碼最基礎的方法是在<span class="_ _3"> </span><span class="ff7">Python<span class="_ _a"> </span></span>直譯器中運行。安裝<span class="_ _3"> </span><span class="ff7">Python<span class="_ _a"> </span></span>語言運行</div><div class="t m0 x5 h4 ye6 ff5 fs3 fc0 sc0 ls0 ws0">時(在前面的一節提到過)然後在命令行中輸入<span class="_ _3"> </span><span class="ffa">python</span>(在<span class="_ _3"> </span><span class="ff7">macOS<span class="_ _3"> </span></span>和<span class="_ _3"> </span><span class="ff7">Unix/Lin<span class="_ _8"></span>ux<span class="_ _3"> </span><span class="ff5">中使用<span class="ff6">終</span></span></span></div><div class="t m0 x5 h4 ye7 ff6 fs3 fc0 sc0 ls0 ws0">端<span class="ff5">(<span class="ff7">terminal</span>)<span class="_ _33"></span>,或者在<span class="_ _9"> </span><span class="ff7">Windows<span class="_ _9"> </span></span>下使用<span class="_ _9"> </span><span class="ff7">CMD</span>)<span class="_ _33"></span>:</span></div><div class="t m0 x11 hc ye8 ffa fs3 fc0 sc0 ls0 ws0">$<span class="_ _2a"> </span>python</div><div class="t m0 x11 hc ye9 ffa fs3 fc0 sc0 ls0 ws0">Python<span class="_ _2a"> </span>3.5.1<span class="_ _2a"> </span>|Continuum<span class="_ _2a"> </span>Analytics,<span class="_ _2a"> </span>Inc.|<span class="_ _2a"> </span>(default,<span class="_ _2a"> </span>Dec<span class="_ _27"> </span>7<span class="_ _2a"> </span>2015,<span class="_ _2a"> </span>11:24:55)</div><div class="t m0 x11 hc yea ffa fs3 fc0 sc0 ls0 ws0">Type<span class="_ _2a"> </span>"help",<span class="_ _2a"> </span>"copyright",<span class="_ _2a"> </span>"credits"<span class="_ _2a"> </span>or<span class="_ _2a"> </span>"license"<span class="_ _2a"> </span>for<span class="_ _2a"> </span>more<span class="_ _2a"> </span>information.</div><div class="t m0 x11 hc yeb ffa fs3 fc0 sc0 ls0 ws0">>>></div><div class="t m0 x4 h4 yec ff5 fs3 fc0 sc0 ls0 ws0">當直譯器啟動之後,你就可以開始輸入並執行程式碼片段了。這裡我們可以把它當作一個簡</div><div class="t m0 x5 h4 yed ff5 fs3 fc0 sc0 ls0 ws0">單的計算器來用,執行一個計算過程並把結果賦給某個<span class="ff6">變數</span>(<span class="ff7">v<span class="_ _8"></span>ariable<span class="ff5">)<span class="_ _33"></span>:</span></span></div><div class="t m0 x11 hc yee ffa fs3 fc6 sc0 ls0 ws0">>>><span class="_ _2a"> </span><span class="fc7">1<span class="_ _2a"> </span></span>+<span class="_ _2a"> </span><span class="fc7">1</span></div><div class="t m0 x11 hc yef ffa fs3 fc7 sc0 ls0 ws0">2</div><div class="t m0 x11 hc yf0 ffa fs3 fc6 sc0 ls0 ws0">>>><span class="_ _2a"> </span><span class="fc0">x<span class="_ _2a"> </span></span>=<span class="_ _2a"> </span><span class="fc7">5</span></div><div class="t m0 x11 hc yf1 ffa fs3 fc6 sc0 ls0 ws0">>>><span class="_ _2a"> </span><span class="fc0">x<span class="_ _2a"> </span></span>*<span class="_ _2a"> </span><span class="fc7">3</span></div><div class="t m0 x11 hc yf2 ffa fs3 fc7 sc0 ls0 ws0">15</div><div class="t m0 x4 h4 yf3 ff5 fs3 fc0 sc0 ls0 ws0">這個直譯器讓我們能方便地嘗試一些<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>程式碼片段並嘗試一些短的操作序列。</div><div class="t m0 x5 h8 yf4 ff1 fs1 fc0 sc0 ls0 ws0">2.2<span class="_ _39"> </span>IPython<span class="_ _10"> </span><span class="ff2">直譯器</span></div><div class="t m0 x4 h4 yf5 ff5 fs3 fc0 sc0 ls0 ws0">如果你在原始的<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>直譯器上花費了很多時間,你會發現它缺少作為一個成熟的互動</div><div class="t m0 x5 h4 yf6 ff5 fs3 fc0 sc0 ls0 ws0">開發環境的許多功能。另一種可選的直譯器叫做<span class="_ _3"> </span><span class="ff7">IPython</span>(<span class="ff7">Interactiv<span class="_ _8"></span>e<span class="_ _3"> </span>Python<span class="_ _3"> </span><span class="ff5">的縮寫)<span class="_ _7"></span>,它包</span></span></div><div class="t m0 x5 h4 yf7 ff5 fs3 fc0 sc0 ls0 ws0">含在<span class="_ _3"> </span><span class="ff7">Anaconda<span class="_ _3"> </span></span>發行版中,而且它包含許多相對於基礎<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>直譯器更方便的增強功能。在</div><div class="t m0 x5 h4 yf8 ff5 fs3 fc0 sc0 ls0 ws0">命令列中輸入<span class="_ _9"> </span><span class="ffa">ipython<span class="_ _3"> </span></span>可以開啟它:</div><div class="t m0 x12 hd yf9 fff fs5 fc0 sc0 ls0 ws0">1</div><div class="t m0 xa he y40 ff5 fs6 fc0 sc0 ls0 ws0">譯者注:在最近推出的<span class="_ _34"> </span><span class="ff10">Ja<span class="_ _8"></span>v<span class="_ _4"></span>a<span class="_ _34"> </span>9<span class="_ _36"> </span><span class="ff5">及以後的版本中,</span>Jav<span class="_ _8"></span>a<span class="_ _34"> </span><span class="ff5">提供了<span class="_ _36"> </span></span>JShell<span class="_ _34"> </span><span class="ff5">使得<span class="_ _34"> </span></span>Jav<span class="_ _8"></span>a<span class="_ _36"> </span><span class="ff5">具有了互動的功能。</span></span></div><div class="t m0 xe h7 y20 ff7 fs3 fc0 sc0 ls0 ws0">5</div><a class="l" href="#pfc" data-dest-detail='[12,"XYZ",88.59,85.15,null]'><div class="d m1" style="border-style:none;position:absolute;left:406.734000px;bottom:591.738000px;width:4.234000px;height:5.136000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pfd" class="pf w0 h0" data-page-no="d"><div class="pc pcd w0 h0"><img class="bi x5 y40 w1 h11" alt="" src="images/bgd.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">6<span class="_ _46"> </span><span class="ffc">2.<span class="_ _16"> </span><span class="ff6">如何執行<span class="_ _9"> </span></span>PYTHON<span class="_ _9"> </span><span class="ff6">程式碼</span></span></div><div class="t m0 x11 hc yfa ffa fs3 fc0 sc0 ls0 ws0">$<span class="_ _2a"> </span>ipython</div><div class="t m0 x11 hc yfb ffa fs3 fc0 sc0 ls0 ws0">Python<span class="_ _2a"> </span>3.5.1<span class="_ _2a"> </span>|Continuum<span class="_ _2a"> </span>Analytics,<span class="_ _2a"> </span>Inc.|<span class="_ _2a"> </span>(default,<span class="_ _2a"> </span>Dec<span class="_ _27"> </span>7<span class="_ _2a"> </span>2015,<span class="_ _2a"> </span>11:24:55)</div><div class="t m0 x11 hc yfc ffa fs3 fc0 sc0 ls0 ws0">Type<span class="_ _2a"> </span>"copyright",<span class="_ _2a"> </span>"credits"<span class="_ _2a"> </span>or<span class="_ _2a"> </span>"license"<span class="_ _2a"> </span>for<span class="_ _2a"> </span>more<span class="_ _2a"> </span>information.</div><div class="t m0 x11 hc yfd ffa fs3 fc0 sc0 ls0 ws0">IPython<span class="_ _2a"> </span>4.0.0<span class="_ _2a"> </span>--<span class="_ _2a"> </span>An<span class="_ _2a"> </span>enhanced<span class="_ _2a"> </span>Interactive<span class="_ _2a"> </span>Python.</div><div class="t m0 x11 hc yfe ffa fs3 fc0 sc0 ls0 ws0">?<span class="_ _47"> </span>-><span class="_ _2a"> </span>Introduction<span class="_ _2a"> </span>and<span class="_ _2a"> </span>overview<span class="_ _2a"> </span>of<span class="_ _2a"> </span>IPython<span class="ff12">'</span>s<span class="_ _2a"> </span>features.</div><div class="t m0 x11 hc yff ffa fs3 fc0 sc0 ls0 ws0">%quickref<span class="_ _2a"> </span>-><span class="_ _2a"> </span>Quick<span class="_ _2a"> </span>reference.</div><div class="t m0 x11 hc y100 ffa fs3 fc0 sc0 ls0 ws0">help<span class="_ _48"> </span>-><span class="_ _2a"> </span>Python<span class="ff12">'</span>s<span class="_ _2a"> </span>own<span class="_ _2a"> </span>help<span class="_ _2a"> </span>system.</div><div class="t m0 x11 hc y101 ffa fs3 fc0 sc0 ls0 ws0">object?<span class="_ _49"> </span>-><span class="_ _2a"> </span>Details<span class="_ _2a"> </span>about<span class="_ _2a"> </span><span class="ff12">'</span>object<span class="ff12">'</span>,<span class="_ _2a"> </span>use<span class="_ _2a"> </span><span class="ff12">'</span>object??<span class="ff12">'<span class="_ _2a"> </span></span>for<span class="_ _2a"> </span>extra<span class="_ _2a"> </span>details.</div><div class="t m0 x11 hc y102 ffa fs3 fc0 sc0 ls0 ws0">In<span class="_ _2a"> </span>[1]:</div><div class="t m0 x4 h4 y103 ff5 fs3 fc0 sc0 ls0 ws0">原<span class="_ _4a"></span>始<span class="_ _1"> </span><span class="ff7">Python<span class="_ _1"> </span></span>解<span class="_ _4a"></span>釋<span class="_ _4a"></span>器<span class="_ _4a"></span>和<span class="_ _10"> </span><span class="ff7">IPython<span class="_ _1"> </span></span>解<span class="_ _4a"></span>釋<span class="_ _4a"></span>器<span class="_ _4a"></span>美<span class="_ _4a"></span>學<span class="_ _4a"></span>上<span class="_ _4a"></span>的<span class="_ _4a"></span>區<span class="_ _4a"></span>別<span class="_ _4a"></span>主<span class="_ _4a"></span>要<span class="_ _4a"></span>取<span class="_ _4a"></span>決<span class="_ _4a"></span>於<span class="_ _4a"></span>命<span class="_ _4a"></span>令<span class="_ _4a"></span>行<span class="_ _4a"></span>中<span class="_ _4a"></span>的<span class="_ _4a"></span>提<span class="_ _4a"></span>示<span class="_ _4a"></span>符:</div><div class="t m0 x5 h4 y104 ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _3"> </span><span class="ff5">預設使用<span class="_ _3"> </span><span class="ffa">>>><span class="_ _3"> </span></span>作為提示符號,而<span class="_ _3"> </span></span>IPython<span class="_ _3"> </span><span class="ff5">為每行命令進行了數字編號(比如<span class="_ _3"> </span><span class="ffa">In<span class="_ _2a"> </span>[1]:</span>)<span class="_ _7"></span>。</span></div><div class="t m0 x5 h4 y105 ff5 fs3 fc0 sc0 ls0 ws0">不管怎樣,我們還是可以像前面那樣逐行地執行程式碼。</div><div class="t m0 x11 hc y106 ffa fs3 fc0 sc0 ls0 ws0">In<span class="_ _2a"> </span>[1]:<span class="_ _2a"> </span>1<span class="_ _2a"> </span>+<span class="_ _2a"> </span>1</div><div class="t m0 x11 hc y107 ffa fs3 fc0 sc0 ls0 ws0">Out[1]:<span class="_ _2a"> </span>2</div><div class="t m0 x11 hc y108 ffa fs3 fc0 sc0 ls0 ws0">In<span class="_ _2a"> </span>[2]:<span class="_ _2a"> </span>x<span class="_ _2a"> </span>=<span class="_ _2a"> </span>5</div><div class="t m0 x11 hc y109 ffa fs3 fc0 sc0 ls0 ws0">In<span class="_ _2a"> </span>[3]:<span class="_ _2a"> </span>x<span class="_ _2a"> </span>*<span class="_ _2a"> </span>3</div><div class="t m0 x11 hc y10a ffa fs3 fc0 sc0 ls0 ws0">Out[3]:<span class="_ _2a"> </span>15</div><div class="t m0 x4 h4 y10b ff5 fs3 fc0 sc0 ls0 ws0">請注意,就像輸入的命令被編號一樣,每個命令的輸出也被編號。<span class="ff7">IPython<span class="_ _3"> </span></span>實現了很廣泛</div><div class="t m0 x5 h4 y10c ff5 fs3 fc0 sc0 ls0 ws0">的有用的功能,更多的資料可以參見第<span class="_ _9"> </span><span class="ff7 fc1">17<span class="_ _3"> </span></span>章<span class="fc1">更多學習資源</span>。</div><div class="t m0 x5 h8 y10d ff1 fs1 fc0 sc0 ls0 ws0">2.3<span class="_ _39"> </span><span class="ff2">自我包含<span class="_ _10"> </span></span>Python<span class="_ _10"> </span><span class="ff2">指令碼</span></div><div class="t m0 x4 h4 y10e ff5 fs3 fc0 sc0 ls0 ws0">在某些情況下,能夠一行一行地執行<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>程式碼塊確實很有用,但是對於更多複雜的程</div><div class="t m0 x5 h4 y10f ff5 fs3 fc0 sc0 ls0 ws0">序,<span class="_ _8"></span>把程式碼儲存到檔案中並一次性執行是更明智的選擇。<span class="_ _8"></span>按照慣例,<span class="_ _8"></span><span class="ff7">Python<span class="_ _9"> </span><span class="ff5">指令碼會被儲存在副</span></span></div><div class="t m0 x5 h4 y110 ff5 fs3 fc0 sc0 ls0 ws0">檔名為<span class="_ _34"> </span><span class="ffa">.py<span class="_ _34"> </span></span>的檔案中。<span class="_ _35"></span>比如,<span class="_ _3e"></span>讓我們建立一個名叫<span class="_ _34"> </span><span class="ffa">test.py<span class="_ _34"> </span></span>的檔案,<span class="_ _3e"></span>並把下面的內容寫入進去。</div><div class="t m0 x11 h6 y111 ff14 fs3 fc8 sc0 ls0 ws0">#<span class="_ _2a"> </span><span class="ff6">檔案:</span>test.py</div><div class="t m0 x11 hc y112 ffa fs3 fc0 sc0 ls0 ws0">print(<span class="fc9">"Running<span class="_ _2a"> </span>test.py"</span>)</div><div class="t m0 x11 hc y113 ffa fs3 fc0 sc0 ls0 ws0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="fc7">5</span></span></div><div class="t m0 x11 hc y114 ffa fs3 fc0 sc0 ls0 ws0">print(<span class="fc9">"Result<span class="_ _2a"> </span>is"</span>,<span class="_ _2a"> </span><span class="fc7">3<span class="_ _2a"> </span><span class="fc6">*<span class="_ _2a"> </span></span></span>x)</div><div class="t m0 x4 h4 y115 ff5 fs3 fc0 sc0 ls0 ws0">為了執行這個檔案,<span class="_ _8"></span>我們必須確保它在一個正確的目錄下,然後在命令列中輸入:<span class="_ _36"> </span><span class="ffa">python</span></div><div class="t m0 x5 h4 y116 ff14 fs3 fc0 sc0 ls0 ws0">filename<span class="_ _4b"> </span><span class="ff5">。</span></div><div class="t m0 x11 hc y117 ffa fs3 fc0 sc0 ls0 ws0">$<span class="_ _2a"> </span>python<span class="_ _2a"> </span>test.py</div><div class="t m0 x11 hc y118 ffa fs3 fc0 sc0 ls0 ws0">Running<span class="_ _2a"> </span>test.py</div><div class="t m0 x11 hc y119 ffa fs3 fc0 sc0 ls0 ws0">Result<span class="_ _2a"> </span>is<span class="_ _2a"> </span>15</div><a class="l" href="#pf6c" data-dest-detail='[108,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:261.091000px;bottom:351.880000px;width:10.909000px;height:6.869000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf6c" data-dest-detail='[108,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:286.545000px;bottom:348.769000px;width:65.455000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pfe" class="pf w0 h0" data-page-no="e"><div class="pc pce w0 h0"><img class="bi x5 y11a w2 h12" alt="" src="images/bge.png"/><div class="t m0 x5 h7 y41 ffc fs3 fc0 sc0 ls0 ws0">2.4.<span class="_ _16"> </span>JUPYTER<span class="_ _9"> </span>NOTEBOOK<span class="_ _4c"> </span><span class="ff7">7</span></div><div class="t m0 x4 h4 y42 ff5 fs3 fc0 sc0 ls0 ws0">對於更複雜的程式,建立像這樣的自我包含指令碼</div><div class="t m0 x1e hb y11b ffe fs4 fc1 sc0 ls0 ws0">2</div><div class="t m0 x1f h4 y42 ff5 fs3 fc0 sc0 ls0 ws0">是必須的。</div><div class="t m0 x5 h13 y11c ff1 fs1 fc0 sc0 ls0 ws0">2.4<span class="_ _39"> </span>Jup<span class="_ _8"></span>yter<span class="_ _10"> </span>Notebook</div><div class="t m0 x4 h4 y11d ff7 fs3 fc0 sc0 ls0 ws0">Jup<span class="_ _4"></span>yter<span class="_ _9"> </span>Notebook<span class="_ _3"> </span><span class="ff5">是一種很好用的工具,它融合了互動式命令列和自我包含指令碼的特點。它</span></div><div class="t m0 x5 h4 y11e ff5 fs3 fc0 sc0 ls0 ws0">是一種文件格式,其中可以包含可執行程式碼、格式化的文字、圖片等,甚至可以用互動功能組</div><div class="t m0 x5 h4 y11f ff5 fs3 fc0 sc0 ls0 ws0">成整個文件。雖然這種特殊的記事本開始時只有<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>版本,但是它正在不斷地被其他許多</div><div class="t m0 x5 h4 y120 ff5 fs3 fc0 sc0 ls0 ws0">語言相容。<span class="_ _8"></span>到了現在,<span class="_ _8"></span>它已經是<span class="_ _9"> </span><span class="ff7 fc2">Jupyter<span class="_ _9"> </span>Pro ject<span class="_ _3"> </span></span>的一部分。<span class="_ _8"></span>這種記事本不止是一種好用的開發</div><div class="t m0 x5 h4 y121 ff5 fs3 fc0 sc0 ls0 ws0">環境,也是將程式碼、數字、資料和文字相結合,通過豐富的計算和資料驅動的敘述來共享工作</div><div class="t m0 x5 h4 y122 ff5 fs3 fc0 sc0 ls0 ws0">的一種手段。</div><div class="t m0 x12 hd y123 fff fs5 fc0 sc0 ls0 ws0">2</div><div class="t m0 xa he ydb ff5 fs6 fc0 sc0 ls0 ws0">譯者注:<span class="_ _2"></span>嚴格來說,<span class="_ _2"></span>自我包含指令碼意味著一個指令碼不需要額外環境就可以執行。<span class="_ _2"></span>對於<span class="_ _36"> </span><span class="ff10">Python<span class="_ _36"> </span></span>這類別直譯型語言,<span class="_ _2"></span>雖</div><div class="t m0 x5 he yba ff5 fs6 fc0 sc0 ls0 ws0">然無法編譯<span class="_ _34"> </span><span class="ff10">Python<span class="_ _9"> </span></span>指令碼,但是可以使用第三方函式庫如<span class="_ _9"> </span><span class="ff10">cxfreeze<span class="_ _34"> </span></span>和<span class="_ _34"> </span><span class="ff10">PyInstaller<span class="_ _9"> </span></span>等進行<span class="ff2">打包</span>。這裡“自包含”的意義</div><div class="t m0 x5 he y40 ff5 fs6 fc0 sc0 ls0 ws0">主要在於一個指令碼檔案包含了全部需要執行的程式碼。</div><a class="l" href="#pfe" data-dest-detail='[14,"XYZ",88.59,111.45,null]'><div class="d m1" style="border-style:none;position:absolute;left:312.000000px;bottom:762.890000px;width:4.234000px;height:5.136000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="https://jupyter.org/"><div class="d m1" style="border-style:none;position:absolute;left:227.262000px;bottom:642.984000px;width:74.800000px;height:9.636000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pff" class="pf w0 h0" data-page-no="f"><div class="pc pcf w0 h0"></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf10" class="pf w0 h0" data-page-no="10"><div class="pc pc10 w0 h0"><img class="bi x5 y124 w1 h14" alt="" src="images/bg10.png"/><div class="t m0 x20 h1 y3 ff2 fs0 fc0 sc0 ls0 ws0">第<span class="_ _0"> </span><span class="ff1">3<span class="_ _0"> </span></span>章<span class="_ _32"> </span><span class="ff1">Python<span class="_ _0"> </span></span>語法速成</div><div class="t m0 x4 h4 ydd ff5 fs3 fc0 sc0 ls0 ws0">最開始<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>是被當作一門用來教學語言來開發的,但是它的易用性和簡潔的語法讓</div><div class="t m0 x5 h4 yde ff5 fs3 fc0 sc0 ls0 ws0">它受到初學者和專家們的歡迎。</div><div class="t m0 x4 h4 ydf ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _3"> </span><span class="ff5">語法的簡潔性使得一些人將其稱為“可執行偽程式碼”<span class="_ _7"></span>。並且,實際上我自己的經驗</span></div><div class="t m0 x5 h4 ye1 ff5 fs3 fc0 sc0 ls0 ws0">是,閱讀和理解</div><div class="t m0 x21 h7 ye2 ff7 fs3 fc0 sc0 ls0 ws0">Python</div><div class="t m0 xc h4 ye1 ff5 fs3 fc0 sc0 ls0 ws0">指令碼比閱讀類似的程式碼(例如</div><div class="t m0 x22 h7 ye2 ff7 fs3 fc0 sc0 ls0 ws0">C</div><div class="t m0 x23 h4 ye1 ff5 fs3 fc0 sc0 ls0 ws0">語言)要容易得多。這裡我們將開始</div><div class="t m0 x5 h4 ye3 ff5 fs3 fc0 sc0 ls0 ws0">討論<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span><span class="ff6">語法</span></span>(<span class="ff7">syn<span class="_ _8"></span>tax<span class="ff5">)的主要特性。</span></span></div><div class="t m0 x4 h4 y125 ff5 fs3 fc0 sc0 ls0 ws0">語法指的是語言的結構(即構成正確結構的程式內容)<span class="_ _41"></span>。<span class="_ _8"></span>在開始的時候,<span class="_ _8"></span>我們不會過分地</div><div class="t m0 x5 h4 y126 ff5 fs3 fc0 sc0 ls0 ws0">糾結<span class="ff6">語意</span>(<span class="ff7">semantics</span>)——這個詞指的是語法中詞和符號的含義——但是我們在後面的學習中</div><div class="t m0 x5 h4 y127 ff5 fs3 fc0 sc0 ls0 ws0">終究繞不開它。</div><div class="t m0 x4 h4 y128 ff5 fs3 fc0 sc0 ls0 ws0">來看一下後面這個例子:</div><div class="t m0 x11 h6 y129 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[1]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">設定中點</span></span></div><div class="t m0 x24 hc y12a ffa fs3 fc0 sc0 ls0 ws0">midpoint<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>5</span></div><div class="t m0 x24 h6 y12b ff14 fs3 fca sc0 ls0 ws0">#<span class="_ _2a"> </span><span class="ff6">建立兩個空的列表</span></div><div class="t m0 x24 hc y12c ffa fs3 fc0 sc0 ls0 ws0">lower<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[];<span class="_ _2a"> </span>upper<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[]</div><div class="t m0 x24 h6 y12d ff14 fs3 fca sc0 ls0 ws0">#<span class="_ _2a"> </span><span class="ff6">把數字分為大的一半和小的一半</span></div><div class="t m0 x24 hc y12e ff11 fs3 fc4 sc0 ls0 ws0">for<span class="_ _2a"> </span><span class="ffa fc0">i<span class="_ _2a"> </span></span><span class="fcb">in<span class="_ _2a"> </span></span><span class="ffa">range<span class="fc0">(<span class="fc6">10</span>):</span></span></div><div class="t m0 x14 hc y12f ff11 fs3 fc4 sc0 ls0 ws0">if<span class="_ _2a"> </span><span class="ffa fc0">(i<span class="_ _2a"> </span><span class="fc6"><<span class="_ _2a"> </span></span>midpoint):</span></div><div class="t m0 x25 hc y130 ffa fs3 fc0 sc0 ls0 ws0">lower<span class="fc6">.</span>append(i)</div><div class="t m0 x14 hc y131 ff11 fs3 fc4 sc0 ls0 ws0">else<span class="ffa fc0">:</span></div><div class="t m0 x25 hc y132 ffa fs3 fc0 sc0 ls0 ws0">upper<span class="fc6">.</span>append(i)</div><div class="t m0 x24 hc y133 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fcc">"lower:"</span>,<span class="_ _2a"> </span>lower)</span></div><div class="t m0 x24 hc y134 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fcc">"upper:"</span>,<span class="_ _2a"> </span>upper)</span></div><div class="t m0 x11 hc y135 ffa fs3 fc0 sc0 ls0 ws0">lower:<span class="_ _2a"> </span>[0,<span class="_ _2a"> </span>1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>3,<span class="_ _2a"> </span>4]</div><div class="t m0 x11 hc y136 ffa fs3 fc0 sc0 ls0 ws0">upper:<span class="_ _2a"> </span>[5,<span class="_ _2a"> </span>6,<span class="_ _2a"> </span>7,<span class="_ _2a"> </span>8,<span class="_ _2a"> </span>9]</div><div class="t m0 x4 h4 y137 ff5 fs3 fc0 sc0 ls0 ws0">這段程式碼看起來有點傻,但是在短短幾句中就展示了<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>語法中的幾個重要部分。現</div><div class="t m0 x5 h4 y138 ff5 fs3 fc0 sc0 ls0 ws0">在讓我們一點點來看,探討一下<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>的語法特性。</div><div class="t m0 x5 h8 y139 ff1 fs1 fc0 sc0 ls0 ws0">3.1<span class="_ _39"> </span><span class="ff2">用<span class="_ _10"> </span><span class="ff11">#<span class="_ _10"> </span></span>標記註釋</span></div><div class="t m0 x4 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">這段指令碼是由一句註釋開始的:</div><div class="t m0 xe h7 y20 ff7 fs3 fc0 sc0 ls0 ws0">9</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf11" class="pf w0 h0" data-page-no="11"><div class="pc pc11 w0 h0"><img class="bi x5 y13a w1 h15" alt="" src="images/bg11.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">10<span class="_ _4d"> </span><span class="ffc">3.<span class="_ _16"> </span>PYTHON<span class="_ _9"> </span><span class="ff6">語法速成</span></span></div><div class="t m0 x11 h6 yfa ff14 fs3 fc8 sc0 ls0 ws0">#<span class="_ _2a"> </span><span class="ff6">設定中點</span></div><div class="t m0 x4 h4 y13b ff5 fs3 fc0 sc0 ls0 ws0">在<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>中,註釋是用一個<span class="_ _3"> </span><span class="ffa">#<span class="_ _9"> </span></span>字元來表示的,一行中跟在<span class="_ _9"> </span><span class="ffa">#<span class="_ _3"> </span></span>字元後面的東西都會被直譯</div><div class="t m0 x5 h4 y13c ff5 fs3 fc0 sc0 ls0 ws0">器忽略掉。這意味著,比如你可以像剛才那樣將注釋獨立成行,也可以將註釋寫在語句後的同</div><div class="t m0 x5 h4 y13d ff5 fs3 fc0 sc0 ls0 ws0">一行內。例如,你可以像下面這樣寫一個註釋:</div><div class="t m0 x11 h6 y13e ffa fs3 fc0 sc0 ls0 ws0">x<span class="_ _2a"> </span><span class="fc6">+=<span class="_ _2a"> </span><span class="fc7">2<span class="_ _27"> </span><span class="ff14 fc8">#<span class="_ _2a"> </span>x<span class="_ _2a"> </span>=<span class="_ _2a"> </span>x<span class="_ _2a"> </span>+<span class="_ _2a"> </span>2<span class="_ _2a"> </span><span class="ff6">的簡寫形式</span></span></span></span></div><div class="t m0 x4 h4 y13f ff5 fs3 fc0 sc0 ls0 ws0">雖然多行字串經常被用來替代多行註釋,但是<span class="_ _3"> </span><span class="ff7">Python<span class="_ _9"> </span></span>中並沒有類似<span class="_ _3"> </span><span class="ff7">C<span class="_ _9"> </span></span>和<span class="_ _3"> </span><span class="ff7">C++<span class="_ _3"> </span></span>中使用</div><div class="t m0 x5 h4 y140 ff5 fs3 fc0 sc0 ls0 ws0">的</div><div class="t m0 x26 hc y141 ffa fs3 fc0 sc0 ls0 ws0">/*<span class="_ _2a"> </span>...<span class="_ _2a"> </span>*/</div><div class="t m0 x27 h4 y140 ff5 fs3 fc0 sc0 ls0 ws0">的多行註釋的語法。關於這個問題可以參閱第<span class="_ _9"> </span><span class="ff7 fc1">15<span class="_ _3"> </span></span>章<span class="fc1">字串處理和正則表示式</span>。</div><div class="t m0 x5 h8 y142 ff1 fs1 fc0 sc0 ls0 ws0">3.2<span class="_ _39"> </span><span class="ff2">用換行符號結束語句</span></div><div class="t m0 x4 h4 y143 ff5 fs3 fc0 sc0 ls0 ws0">指令碼的下一行是:</div><div class="t m0 x11 hc y144 ffa fs3 fc0 sc0 ls0 ws0">midpoint<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="fc7">5</span></span></div><div class="t m0 x4 h4 y145 ff5 fs3 fc0 sc0 ls0 ws0">這是一個<span class="ff6">賦值運算子</span>(<span class="ff7">assignment<span class="_ _9"> </span>operator</span>)<span class="_ _7"></span>。我們建立了一個叫做<span class="_ _3"> </span><span class="ffa">midpoint<span class="_ _9"> </span></span>的變數,並</div><div class="t m0 x5 h4 y146 ff5 fs3 fc0 sc0 ls0 ws0">且把它的值賦為<span class="_ _3"> </span><span class="ff15">5</span>。注意到,在句子末尾我們簡單地使用了換行來標識一行的結束。這和必須</div><div class="t m0 x5 h4 y147 ff5 fs3 fc0 sc0 ls0 ws0">使用分號(<span class="ffa">;</span>)結束一個句子的<span class="_ _9"> </span><span class="ff7">C<span class="_ _3"> </span></span>和<span class="_ _9"> </span><span class="ff7">C++<span class="_ _9"> </span></span>等語言正好相反。</div><div class="t m0 x4 h4 y148 ff5 fs3 fc0 sc0 ls0 ws0">在<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>中,如果你想要在下一行繼續一個語句,你可以使用<span class="_ _9"> </span><span class="ffa">\<span class="_ _9"> </span></span>字元來來指示這一點:</div><div class="t m0 x11 hc y149 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[2]:<span class="_ _2a"> </span><span class="fc0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>1<span class="_ _2a"> </span>+<span class="_ _2a"> </span>2<span class="_ _2a"> </span>+<span class="_ _2a"> </span>3<span class="_ _2a"> </span>+<span class="_ _2a"> </span>4<span class="_ _2a"> </span>+</span>\</span></div><div class="t m0 x14 hc y14a ffa fs3 fc6 sc0 ls0 ws0">5<span class="_ _2a"> </span>+<span class="_ _2a"> </span>6<span class="_ _2a"> </span>+<span class="_ _2a"> </span>7<span class="_ _2a"> </span>+<span class="_ _2a"> </span>8</div><div class="t m0 x4 h4 y14b ff5 fs3 fc0 sc0 ls0 ws0">用一對小括號把一個語句括起來也可以讓一個表示式換行:</div><div class="t m0 x11 hc y14c ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[3]:<span class="_ _2a"> </span><span class="fc0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>(<span class="fc6">1<span class="_ _2a"> </span>+<span class="_ _2a"> </span>2<span class="_ _2a"> </span>+<span class="_ _2a"> </span>3<span class="_ _2a"> </span>+<span class="_ _2a"> </span>4<span class="_ _2a"> </span>+</span></span></div><div class="t m0 x28 hc y14d ffa fs3 fc6 sc0 ls0 ws0">5<span class="_ _2a"> </span>+<span class="_ _2a"> </span>6<span class="_ _2a"> </span>+<span class="_ _2a"> </span>7<span class="_ _2a"> </span>+<span class="_ _2a"> </span>8<span class="fc0">)</span></div><div class="t m0 x4 h4 y14e ff5 fs3 fc0 sc0 ls0 ws0">大部分<span class="_ _a"> </span><span class="ff7">Python<span class="_ _3"> </span></span>程式碼風格指南都建議第二種方法(使用括號)<span class="_ _44"></span>,而不是第一種(使用<span class="_ _a"> </span><span class="ffa">\<span class="_ _3"> </span></span>字</div><div class="t m0 x5 h4 y14f ff5 fs3 fc0 sc0 ls0 ws0">元)<span class="_ _33"></span>。</div><div class="t m0 x5 h8 y150 ff1 fs1 fc0 sc0 ls0 ws0">3.3<span class="_ _39"> </span><span class="ff2">用分號結束語句</span></div><div class="t m0 x4 h4 y151 ff5 fs3 fc0 sc0 ls0 ws0">有時候把多個語句放在一行上可能會很有用。指令碼中下一行程式碼是:</div><div class="t m0 x11 hc y152 ffa fs3 fc0 sc0 ls0 ws0">lower<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[]<span class="fc6">;<span class="_ _2a"> </span></span>upper<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[]</div><div class="t m0 x4 h4 y153 ff5 fs3 fc0 sc0 ls0 ws0">這裡展示了如何像<span class="_ _34"> </span><span class="ff7">C<span class="_ _34"> </span></span>語言中那樣在<span class="_ _9"> </span><span class="ff7">Python<span class="_ _34"> </span></span>中使用分號<span class="_ _8"></span>(<span class="ffa">;</span>)<span class="_ _8"></span>把兩個語句放到同一行裡。<span class="_ _40"></span>從</div><div class="t m0 x5 h4 y154 ff5 fs3 fc0 sc0 ls0 ws0">功能上來說,下面這種寫法和上面是等價的:</div><div class="t m0 x11 hc y155 ffa fs3 fc0 sc0 ls0 ws0">lower<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[]</div><div class="t m0 x11 hc y156 ffa fs3 fc0 sc0 ls0 ws0">upper<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[]</div><div class="t m0 x4 h4 y9a ff5 fs3 fc0 sc0 ls0 ws0">儘管偶爾使用這種方法能帶來一定的便利,使用分號來把兩個語句放到同一行中並不被大</div><div class="t m0 x5 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">部分的<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>程式碼風格指南所推薦。</div><a class="l" href="#pf58" data-dest-detail='[88,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:363.545000px;bottom:612.031000px;width:10.909000px;height:6.870000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf58" data-dest-detail='[88,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:389.000000px;bottom:608.920000px;width:120.000000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf12" class="pf w0 h0" data-page-no="12"><div class="pc pc12 w0 h0"><img class="bi x5 y157 w1 h16" alt="" src="images/bg12.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">3.4.<span class="_ _16"> </span><span class="ff6">縮排:空白字元有意義<span class="_ _4e"> </span><span class="ff7">11</span></span></div><div class="t m0 x5 h8 ybb ff1 fs1 fc0 sc0 ls0 ws0">3.4<span class="_ _39"> </span><span class="ff2">縮排:空白字元有意義</span></div><div class="t m0 x4 h4 ybc ff5 fs3 fc0 sc0 ls0 ws0">接下來讓我們來看看程式碼裡主要的部分:</div><div class="t m0 x11 hc y158 ff11 fs3 fcd sc0 ls0 ws0">for<span class="_ _2a"> </span><span class="ffa fc0">i<span class="_ _2a"> </span></span>in<span class="_ _2a"> </span><span class="ffa fc0">range(<span class="fc7">10</span>):</span></div><div class="t m0 x29 hc y159 ff11 fs3 fcd sc0 ls0 ws0">if<span class="_ _2a"> </span><span class="ffa fc0">i<span class="_ _2a"> </span><span class="fc6"><<span class="_ _2a"> </span></span>midpoint:</span></div><div class="t m0 x24 hc y15a ffa fs3 fc0 sc0 ls0 ws0">lower.append(i)</div><div class="t m0 x29 hc y15b ff11 fs3 fcd sc0 ls0 ws0">else<span class="ffa fc0">:</span></div><div class="t m0 x24 hc y15c ffa fs3 fc0 sc0 ls0 ws0">upper.append(i)</div><div class="t m0 x4 h4 y15d ff5 fs3 fc0 sc0 ls0 ws0">這是一個複合<span class="ff6">控制流程語句</span>(<span class="ff7">control<span class="_ _3"> </span>flo<span class="_ _8"></span>w<span class="_ _3"> </span>statement<span class="ff5">)<span class="_ _7"></span>,它包含了一個<span class="ff6">迴圈語句</span>(<span class="ff7">loop<span class="_ _3"> </span>state-</span></span></span></div><div class="t m0 x5 h4 y15e ff7 fs3 fc0 sc0 ls0 ws0">men<span class="_ _4"></span>t<span class="ff5">)和一個<span class="ff6">條件語句</span>(</span>conditional<span class="_ _3"> </span>statement<span class="ff5">)——我們等一下再來討論這兩種語句。現在,</span></div><div class="t m0 x5 h4 y15f ff5 fs3 fc0 sc0 ls0 ws0">先來看看下面展示出的可能是<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>語法中最有爭議的一個特性:空白字元是<span class="ff2">有意義</span>的!</div><div class="t m0 x4 h4 y160 ff5 fs3 fc0 sc0 ls0 ws0">在程式語言中,一個<span class="ff6">程式碼區塊</span>(<span class="ff7">block</span>)指的是可以看作一個單元的幾個語句的集合。比如,</div><div class="t m0 x5 h4 y161 ff5 fs3 fc0 sc0 ls0 ws0">在<span class="_ _9"> </span><span class="ff7">C<span class="_ _3"> </span></span>語言中程式碼塊被大括號包裹起來:</div><div class="t m0 x11 h6 y162 ff14 fs3 fc8 sc0 ls0 ws0">//<span class="_ _2a"> </span>C<span class="_ _2a"> </span><span class="ff6">程式碼</span></div><div class="t m0 x11 hc y163 ff11 fs3 fcd sc0 ls0 ws0">for<span class="_ _2a"> </span><span class="ffa fc0">(<span class="fce">int<span class="_ _2a"> </span></span>i<span class="_ _2a"> </span>=<span class="_ _2a"> </span><span class="fc7">0</span>;<span class="_ _2a"> </span>i<span class="_ _2a"> </span><<span class="_ _2a"> </span><span class="fc7">100</span>;<span class="_ _2a"> </span>i++)</span></div><div class="t m0 x11 hc y164 ffa fs3 fc0 sc0 ls0 ws0">{</div><div class="t m0 x2a h6 y165 ff14 fs3 fc8 sc0 ls0 ws0">//<span class="_ _2a"> </span><span class="ff6">大括號標識程式碼塊</span></div><div class="t m0 x2a hc y166 ffa fs3 fc0 sc0 ls0 ws0">total<span class="_ _2a"> </span>+=<span class="_ _2a"> </span>i;</div><div class="t m0 x11 hc y167 ffa fs3 fc0 sc0 ls0 ws0">}</div><div class="t m0 x4 h4 y168 ff5 fs3 fc0 sc0 ls0 ws0">在<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>中,程式碼塊使用<span class="ff6">縮排</span>(<span class="ff7">inden<span class="_ _8"></span>tation<span class="ff5">)來標識:</span></span></div><div class="t m0 x11 hc y169 ff11 fs3 fcd sc0 ls0 ws0">for<span class="_ _2a"> </span><span class="ffa fc0">i<span class="_ _2a"> </span></span>in<span class="_ _2a"> </span><span class="ffa fc0">range(<span class="fc7">100</span>):</span></div><div class="t m0 x29 h6 y16a ff14 fs3 fc8 sc0 ls0 ws0">#<span class="_ _2a"> </span><span class="ff6">縮排標識程式碼塊</span></div><div class="t m0 x29 hc y16b ffa fs3 fc0 sc0 ls0 ws0">total<span class="_ _2a"> </span><span class="fc6">+=<span class="_ _2a"> </span></span>i</div><div class="t m0 x4 h4 y16c ff5 fs3 fc0 sc0 ls0 ws0">在<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>中,縮排的程式碼塊的前一行末尾經常有一個冒號(<span class="ffa">:</span>)<span class="_ _33"></span>。</div><div class="t m0 x4 h4 y16d ff5 fs3 fc0 sc0 ls0 ws0">使用縮進可以強制規範出一種在大多數<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>程式碼中統一的、可讀性高的程式碼風格。但</div><div class="t m0 x5 h4 y16e ff5 fs3 fc0 sc0 ls0 ws0">是也可能讓初學者覺得很困惑,後面兩個程式碼片段會產生不同的結果:</div><div class="t m0 x11 hc y16f ffa fs3 fc6 sc0 ls0 ws0">>>><span class="_ _2a"> </span><span class="ff11 fcd">if<span class="_ _2a"> </span></span><span class="fc0">x<span class="_ _2a"> </span></span><<span class="_ _2a"> </span><span class="fc7">4<span class="fc0">:<span class="_ _47"> </span></span></span>>>><span class="_ _2a"> </span><span class="ff11 fcd">if<span class="_ _2a"> </span></span><span class="fc0">x<span class="_ _2a"> </span></span><<span class="_ _2a"> </span><span class="fc7">4<span class="fc0">:</span></span></div><div class="t m0 x11 hc y170 ffa fs3 fc0 sc0 ls0 ws0">...<span class="_ _4f"> </span>y<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>x<span class="_ _2a"> </span><span class="fc6">*<span class="_ _2a"> </span><span class="fc7">2<span class="_ _4f"> </span></span></span>...<span class="_ _4f"> </span>y<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>x<span class="_ _2a"> </span><span class="fc6">*<span class="_ _2a"> </span><span class="fc7">2</span></span></div><div class="t m0 x11 hc y171 ffa fs3 fc0 sc0 ls0 ws0">...<span class="_ _4f"> </span>print(x)<span class="_ _48"> </span>...<span class="_ _2a"> </span>print(x)</div><div class="t m0 x4 h4 y172 ff5 fs3 fc0 sc0 ls0 ws0">左邊的程式碼片段中,<span class="ffa">print(x)<span class="_ _3"> </span></span>在縮進塊裡面,所以只有<span class="_ _3"> </span><span class="ffa">x<span class="_ _3"> </span></span>小於<span class="_ _3"> </span><span class="ff15">4<span class="_ _3"> </span></span>的時候才會被執行。右</div><div class="t m0 x5 h4 y173 ff5 fs3 fc0 sc0 ls0 ws0">邊的程式碼片段中,<span class="ffa">print(x)<span class="_ _9"> </span></span>在縮排塊外面,所以不管<span class="_ _3"> </span><span class="ffa">x<span class="_ _9"> </span></span>的值為多少都會被執行!</div><div class="t m0 x4 h4 y174 ff5 fs3 fc0 sc0 ls0 ws0">一些習慣其他程式語言的程式設計師對<span class="_ _a"> </span><span class="ff7">Python<span class="_ _13"> </span></span>中有意義的空白字元很驚訝,但是從實踐中</div><div class="t m0 x5 h4 y175 ff5 fs3 fc0 sc0 ls0 ws0">來看,這樣可以<span class="_ _4a"></span>比那些不強制縮進的語<span class="_ _4a"></span>言有更好的可讀性<span class="_ _4a"></span>和風格的一致性。如<span class="_ _4a"></span>果你覺得使</div><div class="t m0 x5 h4 y176 ff5 fs3 fc0 sc0 ls0 ws0">用<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>的空白字元很不舒服,我建議你去嘗試一下,你會發現你將逐漸適應它。</div><div class="t m0 x4 h4 y98 ff5 fs3 fc0 sc0 ls0 ws0">最後,你應該注意到,用於縮排程式碼區塊的空格的<span class="ff2">數量</span>取決於使用者,只要它在整個指令碼中是</div><div class="t m0 x5 h4 y99 ff5 fs3 fc0 sc0 ls0 ws0">一致的就好。按照慣例,大多數風格指南都建議將程式碼塊縮排四個空格,這也是本書中遵循的</div><div class="t m0 x5 h4 y9a ff5 fs3 fc0 sc0 ls0 ws0">約定。<span class="_ _8"></span>請注意,<span class="_ _8"></span>許多文字編輯器(如<span class="_ _9"> </span><span class="ff7">Emacs<span class="_ _9"> </span></span>和<span class="_ _9"> </span><span class="ff7">Vim</span>)都包含<span class="_ _9"> </span><span class="ff7">Python<span class="_ _9"> </span></span>模式,<span class="_ _8"></span>能自動縮排四個空</div><div class="t m0 x5 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">格。</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf13" class="pf w0 h0" data-page-no="13"><div class="pc pc13 w0 h0"><img class="bi x5 y177 w1 h17" alt="" src="images/bg13.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">12<span class="_ _4d"> </span><span class="ffc">3.<span class="_ _16"> </span>PYTHON<span class="_ _9"> </span><span class="ff6">語法速成</span></span></div><div class="t m0 x5 h8 ybb ff1 fs1 fc0 sc0 ls0 ws0">3.5<span class="_ _39"> </span><span class="ff2">行內空白字元無意義</span></div><div class="t m0 x4 h4 ybc ff5 fs3 fc0 sc0 ls0 ws0">雖然在<span class="ff2">行(程式碼區塊)之前的空格</span>是有意義的,但<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>行內的空格並不重要。例如,這</div><div class="t m0 x5 h4 ybd ff5 fs3 fc0 sc0 ls0 ws0">三個表示式是等價的:</div><div class="t m0 x11 hc y178 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[4]:<span class="_ _2a"> </span><span class="fc0">x<span class="fc6">=1+2</span></span></div><div class="t m0 x24 hc y179 ffa fs3 fc0 sc0 ls0 ws0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>1<span class="_ _2a"> </span>+<span class="_ _2a"> </span>2</span></div><div class="t m0 x24 hc y17a ffa fs3 fc0 sc0 ls0 ws0">x<span class="_ _50"> </span><span class="fc6">=<span class="_ _51"> </span>1<span class="_ _52"> </span>+<span class="_ _53"> </span>2</span></div><div class="t m0 x4 h4 y17b ff5 fs3 fc0 sc0 ls0 ws0">濫用這種靈活性可能會導致程式碼可讀性的問題——實際上濫用空白字元通常是故意混淆的</div><div class="t m0 x5 h4 y17c ff5 fs3 fc0 sc0 ls0 ws0">程式碼的主要手段之一(有些人常常以此為消遣)<span class="_ _41"></span>。<span class="_ _8"></span>有效地使用空白可以讓程式碼可讀性更好,<span class="_ _8"></span>特別</div><div class="t m0 x5 h4 y17d ff5 fs3 fc0 sc0 ls0 ws0">是在操作符相連的情況下。比較以下兩個求冪的表示式:</div><div class="t m0 x11 hc y17e ffa fs3 fc0 sc0 ls0 ws0">x<span class="fc6">=<span class="fc7">10</span>**-<span class="fc7">2</span></span></div><div class="t m0 x11 hc y17f ffa fs3 fc0 sc0 ls0 ws0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="fc7">10<span class="_ _2a"> </span></span>**<span class="_ _2a"> </span>-<span class="fc7">2</span></span></div><div class="t m0 x4 h4 y180 ff5 fs3 fc0 sc0 ls0 ws0">我覺得一眼看過去第二個版本更容易讀懂。大多是的風格指南都推薦在二元運算子前後各</div><div class="t m0 x5 h4 y181 ff5 fs3 fc0 sc0 ls0 ws0">加一個空格,在一元運算子前後不加空格。我們會在第<span class="_ _3"> </span><span class="ff7 fc1">5<span class="_ _3"> </span></span>章<span class="fc1">基礎<span class="_ _3"> </span><span class="ff7">Python<span class="_ _9"> </span></span>語法:運算子</span>中進一</div><div class="t m0 x5 h4 y182 ff5 fs3 fc0 sc0 ls0 ws0">步討論<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>的運算子。</div><div class="t m0 x5 h8 y183 ff1 fs1 fc0 sc0 ls0 ws0">3.6<span class="_ _39"> </span><span class="ff2">小括號用於表示式分組或函式呼叫</span></div><div class="t m0 x4 h4 y184 ff5 fs3 fc0 sc0 ls0 ws0">在前面的程式碼片段中,我們看到兩種使用小括號的方式。首先,小括號可以用在為表達式</div><div class="t m0 x5 h4 y185 ff5 fs3 fc0 sc0 ls0 ws0">或者數學運算的分組中:</div><div class="t m0 x11 hc y186 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[5]:<span class="_ _2a"> </span><span class="fc6">2<span class="_ _2a"> </span>*<span class="_ _2a"> </span><span class="fc0">(</span>3<span class="_ _2a"> </span>+<span class="_ _2a"> </span>4<span class="fc0">)</span></span></div><div class="t m0 x11 hc y187 ffa fs3 fcf sc0 ls0 ws0">Out[5]:<span class="_ _2a"> </span><span class="fc0">14</span></div><div class="t m0 x4 h4 y188 ff5 fs3 fc0 sc0 ls0 ws0">小括號還可以用於<span class="ff6">函式<span class="_ _4"></span><span class="ff5">(<span class="ff7">function</span>)<span class="_ _4"></span>被呼叫的時候。<span class="_ _2"></span>在下面的程式碼片段中,<span class="_ _8"></span><span class="ffa">print()<span class="_ _9"> </span><span class="ff5">函式用</span></span></span></span></div><div class="t m0 x5 h4 y189 ff5 fs3 fc0 sc0 ls0 ws0">於顯示一個變數的內容。下面這個例子中一對小括號將函數的<span class="ff6">參數</span>(<span class="ff7">argumen<span class="_ _4"></span>t<span class="ff5">)括起來,這表</span></span></div><div class="t m0 x5 h4 y18a ff5 fs3 fc0 sc0 ls0 ws0">示將這些參數傳入這個函式來呼叫它。</div><div class="t m0 x11 hc y18b ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[6]:<span class="_ _2a"> </span><span class="fc4">print<span class="fc0">(<span class="ff12 fcc">'<span class="ffa">first<span class="_ _2a"> </span>value:</span>'</span>,<span class="_ _2a"> </span><span class="fc6">1</span>)</span></span></div><div class="t m0 x11 hc y18c ffa fs3 fc0 sc0 ls0 ws0">first<span class="_ _2a"> </span>value:<span class="_ _2a"> </span>1</div><div class="t m0 x11 hc y18d ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[7]:<span class="_ _2a"> </span><span class="fc4">print<span class="fc0">(<span class="ff12 fcc">'<span class="ffa">second<span class="_ _2a"> </span>value:</span>'</span>,<span class="_ _2a"> </span><span class="fc6">2</span>)</span></span></div><div class="t m0 x11 hc y18e ffa fs3 fc0 sc0 ls0 ws0">second<span class="_ _2a"> </span>value:<span class="_ _2a"> </span>2</div><div class="t m0 x4 h4 y9a ff5 fs3 fc0 sc0 ls0 ws0">有的函數可以沒有參數,這時候也必須使用小括號來表示調用這個函數。下面是一個使</div><div class="t m0 x5 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">用列表的<span class="_ _9"> </span><span class="ffa">sort<span class="_ _3"> </span></span>方法的例子:</div><a class="l" href="#pf1a" data-dest-detail='[26,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:339.784000px;bottom:472.984000px;width:5.455000px;height:6.870000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf1a" data-dest-detail='[26,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:359.936000px;bottom:469.873000px;width:130.395000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf14" class="pf w0 h0" data-page-no="14"><div class="pc pc14 w0 h0"><img class="bi x5 y18f w1 h18" alt="" src="images/bg14.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">3.7.<span class="_ _16"> </span><span class="ff6">完成並更進一步<span class="_ _54"> </span><span class="ff7">13</span></span></div><div class="t m0 x11 hc yfa ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[8]:<span class="_ _2a"> </span><span class="fc0">L<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[<span class="fc6">4</span>,<span class="fc6">2</span>,<span class="fc6">3</span>,<span class="fc6">1</span>]</span></div><div class="t m0 x24 hc yfb ffa fs3 fc0 sc0 ls0 ws0">L<span class="fc6">.</span>sort()</div><div class="t m0 x24 hc yfc ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(L)</span></div><div class="t m0 x11 hc yfd ffa fs3 fc0 sc0 ls0 ws0">[1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>3,<span class="_ _2a"> </span>4]</div><div class="t m0 x4 h4 y190 ffa fs3 fc0 sc0 ls0 ws0">sort<span class="_ _9"> </span><span class="ff5">之後的括號(</span>()<span class="ff5">)表示函式被執行。即使不需要傳入參數,括號也是必不可少的。</span></div><div class="t m0 x2b ha y191 ff2 fs2 fc0 sc0 ls0 ws0">旁註:關於<span class="_ _13"> </span><span class="ff11">print()<span class="_ _13"> </span></span>的一點說明</div><div class="t m0 x2c h4 y192 ff5 fs3 fc0 sc0 ls0 ws0">在上面我們是用了<span class="_ _36"> </span><span class="ffa">print()<span class="_ _34"> </span></span>函式做例子,<span class="_ _3a"></span><span class="ffa">print()<span class="_ _34"> </span><span class="ff5">函式是<span class="_ _36"> </span><span class="ff7">Python<span class="_ _34"> </span>2.<span class="ffd">x<span class="_ _36"> </span></span></span>和<span class="_ _34"> </span><span class="ff7">Python<span class="_ _36"> </span>3.<span class="ffd">x<span class="_ _34"> </span></span></span>中</span></span></div><div class="t m0 x2b h4 y193 ff5 fs3 fc0 sc0 ls0 ws0">變化的一部分。在<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span>2<span class="_ _9"> </span></span>裡,<span class="ffa">print<span class="_ _9"> </span></span>表現得像一個表示式——你可以這樣寫:</div><div class="t m0 x12 h6 y194 ff14 fs3 fc8 sc0 ls0 ws0">#<span class="_ _2a"> </span>Python<span class="_ _2a"> </span>2<span class="_ _2a"> </span><span class="ff6">才能這麼做!</span></div><div class="t m0 x12 hc y195 ffa fs3 fc6 sc0 ls0 ws0">>><span class="_ _2a"> </span><span class="fc0">print<span class="_ _2a"> </span><span class="fc9">"first<span class="_ _2a"> </span>value:"</span>,<span class="_ _2a"> </span><span class="fc7">1</span></span></div><div class="t m0 x12 hc y196 ffa fs3 fc0 sc0 ls0 ws0">first<span class="_ _2a"> </span>value:<span class="_ _2a"> </span><span class="fc7">1</span></div><div class="t m0 x2c h4 y197 ff5 fs3 fc0 sc0 ls0 ws0">由於各種原因,<span class="_ _35"></span>這門語言的開發者決定在<span class="_ _34"> </span><span class="ff7">Python<span class="_ _9"> </span>3<span class="_ _34"> </span></span>中<span class="_ _9"> </span><span class="ffa">print()<span class="_ _9"> </span></span>應該成為一個函式,<span class="_ _3e"></span>所</div><div class="t m0 x2b h4 y198 ff5 fs3 fc0 sc0 ls0 ws0">以我們現在這麼寫:</div><div class="t m0 x12 h6 y199 ff14 fs3 fc8 sc0 ls0 ws0">#<span class="_ _2a"> </span><span class="ff6">僅<span class="_ _2a"> </span></span>Python<span class="_ _2a"> </span>3<span class="_ _2a"> </span><span class="ff6">支援!</span></div><div class="t m0 x12 hc y19a ffa fs3 fc6 sc0 ls0 ws0">>>><span class="_ _2a"> </span><span class="fc0">print(<span class="fc9">"first<span class="_ _2a"> </span>value:"</span>,<span class="_ _2a"> </span><span class="fc7">1</span>)</span></div><div class="t m0 x12 hc y19b ffa fs3 fc0 sc0 ls0 ws0">first<span class="_ _2a"> </span>value:<span class="_ _2a"> </span><span class="fc7">1</span></div><div class="t m0 x2c h4 y19c ff5 fs3 fc0 sc0 ls0 ws0">這是<span class="_ _9"> </span><span class="ff7">Python<span class="_ _9"> </span>2<span class="_ _9"> </span></span>和<span class="_ _9"> </span><span class="ff7">3<span class="_ _34"> </span></span>中不向後相容的語法中的一條。<span class="_ _8"></span>在撰寫本書時,<span class="_ _8"></span>你通常會發現以兩</div><div class="t m0 x2b h4 y19d ff5 fs3 fc0 sc0 ls0 ws0">個版本的<span class="_ _3"> </span><span class="ff7">Python<span class="_ _9"> </span></span>編寫的範例。並且,存在<span class="_ _3"> </span><span class="ffa">print<span class="_ _9"> </span></span>語句,而不是<span class="_ _3"> </span><span class="ffa">print()<span class="_ _9"> </span></span>函式通常是你正</div><div class="t m0 x2b h4 y19e ff5 fs3 fc0 sc0 ls0 ws0">在檢視的程式碼是<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span>2<span class="_ _9"> </span></span>程式碼的第一跡象之一。</div><div class="t m0 x5 h8 y19f ff1 fs1 fc0 sc0 ls0 ws0">3.7<span class="_ _39"> </span><span class="ff2">完成並更進一步</span></div><div class="t m0 x4 h4 y1a0 ff5 fs3 fc0 sc0 ls0 ws0">這<span class="_ _4a"></span>一<span class="_ _55"></span>章<span class="_ _4a"></span>是<span class="_ _55"> </span>對<span class="_ _10"> </span><span class="ff7">Python<span class="_ _1"> </span></span>語<span class="_ _55"> </span>法<span class="_ _4a"></span>基<span class="_ _55"> </span>本<span class="_ _4a"></span>特<span class="_ _55"> </span>徵<span class="_ _4a"></span>的<span class="_ _55"> </span>一<span class="_ _4a"></span>個<span class="_ _55"> </span>很<span class="_ _4a"></span>簡<span class="_ _55"> </span>單<span class="_ _4a"></span>的<span class="_ _55"> </span>探<span class="_ _4a"></span>索。<span class="_ _55"> </span>它<span class="_ _4a"></span>的<span class="_ _55"> </span>目<span class="_ _4a"></span>的<span class="_ _55"> </span>是<span class="_ _4a"></span>當<span class="_ _55"> </span>你<span class="_ _55"></span>在<span class="_ _4a"></span>閱<span class="_ _55"></span>讀<span class="_ _4a"></span>後</div><div class="t m0 x5 h4 y1a1 ff5 fs3 fc0 sc0 ls0 ws0">面<span class="_ _4a"></span>的<span class="_ _4a"></span>代<span class="_ _4a"></span>碼<span class="_ _4a"></span>時,<span class="_ _4a"></span>給<span class="_ _4a"></span>你<span class="_ _4a"></span>一<span class="_ _4a"></span>個<span class="_ _4a"></span>很<span class="_ _4a"></span>好<span class="_ _4a"></span>的<span class="_ _4a"></span>參<span class="_ _4a"></span>考<span class="_ _4a"></span>框<span class="_ _4a"></span>架。<span class="_ _4a"></span>我<span class="_ _4a"></span>們<span class="_ _4a"></span>幾<span class="_ _4a"></span>次<span class="_ _4a"></span>提<span class="_ _4a"></span>到<span class="_ _4a"></span>了<span class="_ _4a"></span>“<span class="ff7">Python<span class="_ _1"> </span></span>風<span class="_ _4a"></span>格<span class="_ _4a"></span>指<span class="_ _4a"></span>南”<span class="_ _2"></span>,<span class="_ _4a"></span>它<span class="_ _4a"></span>可<span class="_ _4a"></span>以</div><div class="t m0 x5 h4 y1a2 ff5 fs3 fc0 sc0 ls0 ws0">幫<span class="_ _4a"></span>助<span class="_ _55"> </span>團<span class="_ _55"> </span>隊<span class="_ _55"></span>以<span class="_ _4a"></span>一<span class="_ _55"> </span>致<span class="_ _55"> </span>的<span class="_ _55"></span>風<span class="_ _4a"></span>格<span class="_ _55"> </span>編<span class="_ _55"> </span>寫<span class="_ _55"></span>代<span class="_ _4a"></span>碼。<span class="ff7">Python<span class="_ _10"> </span></span>中<span class="_ _55"> </span>最<span class="_ _55"></span>廣<span class="_ _4a"></span>泛<span class="_ _55"> </span>使<span class="_ _55"> </span>用<span class="_ _55"></span>的<span class="_ _4a"></span>風<span class="_ _55"> </span>格<span class="_ _55"> </span>指<span class="_ _4a"></span>南<span class="_ _55"> </span>稱<span class="_ _55"> </span>為<span class="_ _10"> </span><span class="ff7">PEP<span class="_ _10"> </span>8</span>,<span class="_ _4a"></span>可<span class="_ _55"> </span>以</div><div class="t m0 x5 h4 y1a3 ff5 fs3 fc0 sc0 ls0 ws0">在<span class="_ _3"> </span><span class="ffa fc2">https://www.python.org/dev/peps/pep-0008/<span class="_ _3"> </span></span>找到。當你開始編寫更多的<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>程式碼</div><div class="t m0 x5 h4 y1a4 ff5 fs3 fc0 sc0 ls0 ws0">時,閱讀一下這份程式碼指南是很有用的!風格指南中包含許多<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>大師的智慧,大多數建</div><div class="t m0 x5 h4 y1a5 ff5 fs3 fc0 sc0 ls0 ws0">議不僅僅是簡單的教條:它們是基於無數經驗的建議所寫成,可以幫助你們避免程式碼中一些微</div><div class="t m0 x5 h4 y1a6 ff5 fs3 fc0 sc0 ls0 ws0">妙的錯誤和<span class="_ _9"> </span><span class="ff7">bug</span>。</div><a class="l" href="https://www.python.org/dev/peps/pep-0008/"><div class="d m1" style="border-style:none;position:absolute;left:86.780000px;bottom:176.165000px;width:235.316000px;height:10.001000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf15" class="pf w0 h0" data-page-no="15"><div class="pc pc15 w0 h0"></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf16" class="pf w0 h0" data-page-no="16"><div class="pc pc16 w0 h0"><img class="bi x5 y9b w1 h19" alt="" src="images/bg16.png"/><div class="t m0 x2c h1 y3 ff2 fs0 fc0 sc0 ls0 ws0">第<span class="_ _0"> </span><span class="ff1">4<span class="_ _0"> </span></span>章<span class="_ _32"> </span>基礎<span class="_ _0"> </span><span class="ff1">Python<span class="_ _0"> </span></span>語意:變數和物件</div><div class="t m0 x4 h4 y4 ff5 fs3 fc0 sc0 ls0 ws0">本節將開始介紹<span class="_ _3"> </span><span class="ff7">Python<span class="_ _a"> </span></span>語言的基本<span class="ff6">語義</span>(<span class="ff7">semantics</span>)<span class="_ _44"></span>。與上一節所述的語法不同,語言</div><div class="t m0 x5 h4 y5 ff5 fs3 fc0 sc0 ls0 ws0">的語意涉及語句的含義。正如我們對語法的討論一樣,在這裡我們大致瀏覽<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>中一些基</div><div class="t m0 x5 h4 y6 ff5 fs3 fc0 sc0 ls0 ws0">本的語意結構,為你提供一個更好的參考框架,以了解以後章節的程式碼。</div><div class="t m0 x4 h4 y7 ff5 fs3 fc0 sc0 ls0 ws0">本節將介紹<span class="ff6">變數</span>(<span class="ff7">v<span class="_ _8"></span>ariable<span class="ff5">)和<span class="ff6">物件</span>(</span>object<span class="ff5">)的語義,這是你在<span class="_ _3"> </span></span>Python<span class="_ _3"> </span><span class="ff5">腳本中儲存、引</span></span></div><div class="t m0 x5 h4 y8 ff5 fs3 fc0 sc0 ls0 ws0">用和操作資料的主要方式。</div><div class="t m0 x5 h8 y1a7 ff1 fs1 fc0 sc0 ls0 ws0">4.1<span class="_ _39"> </span><span class="ff2">變數是指標</span></div><div class="t m0 x4 h4 y1a8 ff5 fs3 fc0 sc0 ls0 ws0">在<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>中想要為變數賦值易如反掌,只需要把一個變數名放到等號(<span class="ffa">=</span>)的左邊就好。</div><div class="t m0 x11 h6 y1a9 ff14 fs3 fc8 sc0 ls0 ws0">#<span class="_ _2a"> </span><span class="ff6">把<span class="_ _2a"> </span></span>4<span class="_ _2a"> </span><span class="ff6">賦給變數<span class="_ _2a"> </span></span>x</div><div class="t m0 x11 hc y1aa ffa fs3 fc0 sc0 ls0 ws0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="fc7">4</span></span></div><div class="t m0 x4 h4 y1ab ff5 fs3 fc0 sc0 ls0 ws0">這可能看起來很直接,但如果你心中對這個操作建立了錯誤的印象,那麼<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>的工作</div><div class="t m0 x5 h4 y1ac ff5 fs3 fc0 sc0 ls0 ws0">方式可能會令你感到困惑。我們將在這裡簡要介紹一下。</div><div class="t m0 x4 h4 y1ad ff5 fs3 fc0 sc0 ls0 ws0">在許多程式語言中,變數一般被認為是存放資料的容器或一個桶一樣的東西。所以比如</div><div class="t m0 x5 h4 y1ae ff5 fs3 fc0 sc0 ls0 ws0">在<span class="_ _9"> </span><span class="ff7">C<span class="_ _3"> </span></span>中,當你寫:</div><div class="t m0 x11 h6 y1af ff14 fs3 fc8 sc0 ls0 ws0">//<span class="_ _2a"> </span>C<span class="_ _2a"> </span><span class="ff6">程式碼</span></div><div class="t m0 x11 hc y1b0 ffa fs3 fce sc0 ls0 ws0">int<span class="_ _2a"> </span><span class="fc0">x<span class="_ _2a"> </span>=<span class="_ _2a"> </span><span class="fc7">4</span>;</span></div><div class="t m0 x4 h4 y1b1 ff5 fs3 fc0 sc0 ls0 ws0">本質上,<span class="_ _42"></span>你定義了一個名為<span class="_ _34"> </span><span class="ffa">x<span class="_ _34"> </span></span>的<span class="_ _40"></span>“記憶體桶”<span class="_ _41"></span>,<span class="_ _42"></span>並將值<span class="_ _34"> </span><span class="ff15">4<span class="_ _34"> </span></span>放入其中。<span class="_ _42"></span>相比之下,<span class="_ _42"></span>在<span class="_ _34"> </span><span class="ff7">Python<span class="_ _34"> </span></span>中變</div><div class="t m0 x5 h4 y1b2 ff5 fs3 fc0 sc0 ls0 ws0">量並不是作為容器,而是作為一個<span class="ff6">指標</span>(<span class="ff7">poin<span class="_ _4"></span>ter<span class="ff5">)</span></span></div><div class="t m0 x2d hb y1b3 ffe fs4 fc1 sc0 ls0 ws0">1</div><div class="t m0 x2e h4 y1b2 ff5 fs3 fc0 sc0 ls0 ws0">。所以在<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>中,當你寫:</div><div class="t m0 x11 hc y1b4 ffa fs3 fc0 sc0 ls0 ws0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="fc7">4</span></span></div><div class="t m0 x4 h4 y1b5 ff5 fs3 fc0 sc0 ls0 ws0">你本質上定義一個名為<span class="_ _9"> </span><span class="ffa">x<span class="_ _3"> </span></span>的指標,指向一些包含值<span class="_ _9"> </span><span class="ff15">4<span class="_ _3"> </span></span>的記憶體中的位置。注意這麼做的一個</div><div class="t m0 x5 h4 y1b6 ff5 fs3 fc0 sc0 ls0 ws0">後果:因為</div><div class="t m0 x2 h7 y1b7 ff7 fs3 fc0 sc0 ls0 ws0">Python</div><div class="t m0 x20 h4 y1b6 ff5 fs3 fc0 sc0 ls0 ws0">的變數只是指向各種物件,所以不需要“宣告”變數,甚至不需要要求變</div><div class="t m0 x5 h4 y1b8 ff5 fs3 fc0 sc0 ls0 ws0">量總是指向相同型別的物件!<span class="_ _4"></span>這是人們所說的<span class="_ _9"> </span><span class="ff7">Python<span class="_ _9"> </span></span>的<span class="ff6">動態型別</span>(<span class="ff7">dynamically<span class="_ _9"> </span>typed</span>)<span class="_ _33"></span>:變數</div><div class="t m0 x5 h4 y1b9 ff5 fs3 fc0 sc0 ls0 ws0">名稱可以指向任何型別的物件。所以在<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>中,你可以這樣做:</div><div class="t m0 x11 h6 y1ba ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[1]:<span class="_ _2a"> </span><span class="fc0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>1<span class="_ _56"> </span><span class="ff14 fca">#<span class="_ _2a"> </span>x<span class="_ _2a"> </span><span class="ff6">是一個整數型別</span></span></span></span></div><div class="t m0 x24 h6 y1bb ffa fs3 fc0 sc0 ls0 ws0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">hello</span>'<span class="_ _4f"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">現在<span class="_ _2a"> </span></span>x<span class="_ _2a"> </span><span class="ff6">是一個字串</span></span></span></span></div><div class="t m0 x24 h6 y1bc ffa fs3 fc0 sc0 ls0 ws0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[<span class="fc6">1</span>,<span class="_ _2a"> </span><span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">3</span>]<span class="_ _49"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">現在<span class="_ _2a"> </span></span>x<span class="_ _2a"> </span><span class="ff6">是一個列表</span></span></div><div class="t m0 x12 hd yb9 fff fs5 fc0 sc0 ls0 ws0">1</div><div class="t m0 xa he yba ff5 fs6 fc0 sc0 ls0 ws0">譯者注:<span class="ff10">Python<span class="_ _9"> </span></span>中並沒有<span class="_ _9"> </span><span class="ff10">C<span class="_ _34"> </span></span>語言那樣的指針型別,它表現得更像<span class="_ _9"> </span><span class="ff10">C++<span class="_ _34"> </span></span>或者<span class="_ _9"> </span><span class="ff10">Ja<span class="_ _4"></span>v<span class="_ _8"></span>a<span class="_ _9"> </span><span class="ff5">中<span class="ff6">引用</span>(</span>reference<span class="ff5">)的概</span></span></div><div class="t m0 x5 he y40 ff5 fs6 fc0 sc0 ls0 ws0">念,後文中的指標一般是使用這裡的含義。</div><div class="t m0 x2f h7 y20 ff7 fs3 fc0 sc0 ls0 ws0">15</div><a class="l" href="#pf16" data-dest-detail='[22,"XYZ",88.59,98.3,null]'><div class="d m1" style="border-style:none;position:absolute;left:313.242000px;bottom:279.924000px;width:4.235000px;height:5.137000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf17" class="pf w0 h0" data-page-no="17"><div class="pc pc17 w0 h0"><img class="bi x5 y1bd w1 h1a" alt="" src="images/bg17.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">16<span class="_ _57"> </span><span class="ffc">4.<span class="_ _16"> </span><span class="ff6">基礎<span class="_ _9"> </span></span>PYTHON<span class="_ _9"> </span><span class="ff6">語意:變數和物件</span></span></div><div class="t m0 x4 h4 y42 ff5 fs3 fc0 sc0 ls0 ws0">然而<span class="ff6">靜態類型</span>(<span class="ff7">statically<span class="_ _3"> </span>t<span class="_ _8"></span>yped<span class="ff5">)語言的用戶可能會想念類別似於<span class="_ _3"> </span></span>C<span class="_ _9"> </span><span class="ff5">語言中的通過宣告保證</span></span></div><div class="t m0 x5 h4 y77 ff5 fs3 fc0 sc0 ls0 ws0">的型別安全性。</div><div class="t m0 x11 hc y1be ffa fs3 fce sc0 ls0 ws0">int<span class="_ _2a"> </span><span class="fc0">x<span class="_ _2a"> </span>=<span class="_ _2a"> </span><span class="fc7">4</span>;</span></div><div class="t m0 x4 h4 y1bf ff5 fs3 fc0 sc0 ls0 ws0">這種動態類型是使<span class="_ _13"> </span><span class="ff7">Python<span class="_ _a"> </span></span>能夠快速編寫並易於閱讀的原因之一。你需要注意使用這種</div><div class="t m0 x30 h4 y1c0 ff5 fs3 fc0 sc0 ls0 ws0">“指標型別變數”的後果:如果我們有兩個變數名指向相同的<span class="ff6">可變</span>(<span class="ff7">mutable</span>)物件,那麼更改</div><div class="t m0 x5 h4 y1c1 ff5 fs3 fc0 sc0 ls0 ws0">其中一個也會改變另一個!例如,我們來建立和修改列表:</div><div class="t m0 x11 hc y1c2 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[2]:<span class="_ _2a"> </span><span class="fc0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[<span class="fc6">1</span>,<span class="_ _2a"> </span><span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">3</span>]</span></div><div class="t m0 x24 hc y1c3 ffa fs3 fc0 sc0 ls0 ws0">y<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>x</div><div class="t m0 x4 h4 y1c4 ff5 fs3 fc0 sc0 ls0 ws0">我們建立的兩個變數<span class="_ _9"> </span><span class="ffa">x<span class="_ _3"> </span></span>和<span class="_ _9"> </span><span class="ffa">y<span class="_ _9"> </span></span>指向了同一個物件。因此,如果我們通過其中一個變數名來修</div><div class="t m0 x5 h4 y1c5 ff5 fs3 fc0 sc0 ls0 ws0">改這個列表,我們會看到“另一個”列表也被同時改變了。</div><div class="t m0 x11 hc y1c6 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[3]:<span class="_ _2a"> </span><span class="fc4">print<span class="fc0">(y)</span></span></div><div class="t m0 x11 hc y1c7 ffa fs3 fc0 sc0 ls0 ws0">[1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>3]</div><div class="t m0 x11 h6 y1c8 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[4]:<span class="_ _2a"> </span><span class="fc0">x<span class="fc6">.</span>append(<span class="fc6">4</span>)<span class="_ _27"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">把<span class="_ _2a"> </span></span>4<span class="_ _2a"> </span><span class="ff6">加到<span class="_ _2a"> </span></span>x<span class="_ _2a"> </span><span class="ff6">指向的列表</span></span></span></div><div class="t m0 x24 h6 y1c9 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(y)<span class="_ _4f"> </span><span class="ff14 fca">#<span class="_ _2a"> </span>y<span class="_ _2a"> </span><span class="ff6">指向的列表也被改變了</span></span></span></div><div class="t m0 x11 hc y1ca ffa fs3 fc0 sc0 ls0 ws0">[1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>3,<span class="_ _2a"> </span>4]</div><div class="t m0 x4 h4 y1cb ff5 fs3 fc0 sc0 ls0 ws0">如果你錯誤地將變數視為包含資料的<span class="_ _4"></span>“桶”<span class="_ _33"></span>,<span class="_ _8"></span>那麼這種行為可能會令人困惑。但是,<span class="_ _8"></span>如果你</div><div class="t m0 x5 h4 y1cc ff5 fs3 fc0 sc0 ls0 ws0">將變數正確地視為指向物件的指標,那麼這種行為是有道理的。</div><div class="t m0 x4 h4 y1cd ff5 fs3 fc0 sc0 ls0 ws0">如果我們用<span class="_ _9"> </span><span class="ffa">=<span class="_ _34"> </span></span>把另外一個值賦給<span class="_ _9"> </span><span class="ffa">x</span>,<span class="_ _2"></span>就不會影響到<span class="_ _9"> </span><span class="ffa">y<span class="_ _9"> </span></span>的值——賦值是改變<span class="ff2">變數指向的物件</span>:</div><div class="t m0 x11 hc y1ce ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[5]:<span class="_ _2a"> </span><span class="fc0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">something<span class="_ _2a"> </span>else</span>'</span></span></span></div><div class="t m0 x24 h6 y1cf ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(y)<span class="_ _27"> </span><span class="ff14 fca">#<span class="_ _2a"> </span>y<span class="_ _2a"> </span><span class="ff6">沒有被改變</span></span></span></div><div class="t m0 x11 hc y1d0 ffa fs3 fc0 sc0 ls0 ws0">[1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>3,<span class="_ _2a"> </span>4]</div><div class="t m0 x4 h4 y1d1 ff5 fs3 fc0 sc0 ls0 ws0">再次強調,把<span class="_ _9"> </span><span class="ffa">x<span class="_ _3"> </span></span>和<span class="_ _9"> </span><span class="ffa">y<span class="_ _9"> </span></span>看作指標,把<span class="_ _3"> </span><span class="ffa">=<span class="_ _9"> </span></span>看做改變變數名指向的東西的操作。</div><div class="t m0 x4 h4 y1d2 ff5 fs3 fc0 sc0 ls0 ws0">你可能會想知道這個指標的想法是否使得<span class="_ _34"> </span><span class="ff7">Python<span class="_ _34"> </span></span>中的算術運算難以追蹤,<span class="_ _42"></span>但是<span class="_ _34"> </span><span class="ff7">Python<span class="_ _34"> </span></span>的</div><div class="t m0 x5 h4 y1d3 ff5 fs3 fc0 sc0 ls0 ws0">設計不存在這個問題。<span class="_ _40"></span>數字、<span class="_ _3e"></span>字串和其他<span class="ff6">簡單型別<span class="_ _8"></span><span class="ff5">(<span class="ff7">simple<span class="_ _34"> </span>type</span>)<span class="_ _8"></span>是<span class="ff6">不可變<span class="_ _8"></span><span class="ff5">(<span class="ff7">imm<span class="_ _8"></span>utable<span class="ff5">)<span class="_ _8"></span>的:</span></span></span></span></span></span></div><div class="t m0 x5 h4 y1d4 ff5 fs3 fc0 sc0 ls0 ws0">你不能更改它的值,你只能更改變數<span class="ff2">指向</span>的值。所以執行如下操作是完全安全的:</div><div class="t m0 x11 hc y175 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[6]:<span class="_ _2a"> </span><span class="fc0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>10</span></span></div><div class="t m0 x24 hc y176 ffa fs3 fc0 sc0 ls0 ws0">y<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>x</div><div class="t m0 x24 h6 y98 ffa fs3 fc0 sc0 ls0 ws0">x<span class="_ _2a"> </span><span class="fc6">+=<span class="_ _2a"> </span>5<span class="_ _27"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">把<span class="_ _2a"> </span></span>x<span class="_ _2a"> </span><span class="ff6">的值加上<span class="_ _2a"> </span></span>5<span class="_ _2a"> </span><span class="ff6">再賦給<span class="_ _2a"> </span></span>x</span></span></div><div class="t m0 x24 hc y99 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fcc">"x<span class="_ _2a"> </span>="</span>,<span class="_ _2a"> </span>x)</span></div><div class="t m0 x24 hc y9a ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fcc">"y<span class="_ _2a"> </span>="</span>,<span class="_ _2a"> </span>y)</span></div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf18" class="pf w0 h0" data-page-no="18"><div class="pc pc18 w0 h0"><img class="bi x5 y1d5 w1 h1b" alt="" src="images/bg18.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">4.2.<span class="_ _16"> </span><span class="ff6">一切皆物件<span class="_ _58"> </span><span class="ff7">17</span></span></div><div class="t m0 x11 hc y1d6 ffa fs3 fc0 sc0 ls0 ws0">x<span class="_ _2a"> </span>=<span class="_ _2a"> </span>15</div><div class="t m0 x11 hc y1d7 ffa fs3 fc0 sc0 ls0 ws0">y<span class="_ _2a"> </span>=<span class="_ _2a"> </span>10</div><div class="t m0 x4 h4 y1d8 ff5 fs3 fc0 sc0 ls0 ws0">當我們執行<span class="_ _9"> </span><span class="ffa">x<span class="_ _2a"> </span>+=<span class="_ _2a"> </span>5<span class="_ _9"> </span></span>時<span class="ff7">,<span class="_ _9"> </span></span>我們不會改變值為<span class="_ _9"> </span><span class="ffa">10<span class="_ _9"> </span></span>的那個被<span class="_ _9"> </span><span class="ffa">x<span class="_ _9"> </span></span>指向的物件;<span class="_ _8"></span>我們會讓<span class="_ _9"> </span><span class="ffa">x<span class="_ _9"> </span></span>重新指</div><div class="t m0 x5 h4 y1d9 ff5 fs3 fc0 sc0 ls0 ws0">向一個值為<span class="_ _9"> </span><span class="ffa">15<span class="_ _3"> </span></span>的新的物件。因此,<span class="_ _9"> </span><span class="ffa">y<span class="_ _9"> </span></span>的值不會受到這個操作的影響。</div><div class="t m0 x5 h8 y1da ff1 fs1 fc0 sc0 ls0 ws0">4.2<span class="_ _39"> </span><span class="ff2">一切皆物件</span></div><div class="t m0 x4 h4 y1db ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _9"> </span><span class="ff5">是一門<span class="ff6">物件導向</span>(</span>object-oriented<span class="ff5">)的程式語言,所以<span class="_ _9"> </span></span>Python<span class="_ _9"> </span><span class="ff5">中的一切都是物件。</span></div><div class="t m0 x4 h4 y1dc ff5 fs3 fc0 sc0 ls0 ws0">我們來看看這是什麼意思。之前我們看到變數是簡單的指針,變數名本身沒有附加的型別</div><div class="t m0 x5 h4 y1dd ff5 fs3 fc0 sc0 ls0 ws0">資訊。這導致一些人錯誤地聲稱<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>是一種無型別的語言。但事實並非如此!考慮以下這</div><div class="t m0 x5 h4 y1de ff5 fs3 fc0 sc0 ls0 ws0">個例子:</div><div class="t m0 x11 hc y1df ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[7]:<span class="_ _2a"> </span><span class="fc0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>4</span></span></div><div class="t m0 x24 hc y1e0 ffa fs3 fc4 sc0 ls0 ws0">type<span class="fc0">(x)</span></div><div class="t m0 x11 hc y1e1 ffa fs3 fcf sc0 ls0 ws0">Out[7]:<span class="_ _2a"> </span><span class="fc0">int</span></div><div class="t m0 x11 hc y1e2 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[8]:<span class="_ _2a"> </span><span class="fc0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">hello</span>'</span></span></span></div><div class="t m0 x24 hc y1e3 ffa fs3 fc4 sc0 ls0 ws0">type<span class="fc0">(x)</span></div><div class="t m0 x11 hc y1e4 ffa fs3 fcf sc0 ls0 ws0">Out[8]:<span class="_ _2a"> </span><span class="fc0">str</span></div><div class="t m0 x11 hc y1e5 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[9]:<span class="_ _2a"> </span><span class="fc0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>3.14159</span></span></div><div class="t m0 x24 hc y1e6 ffa fs3 fc4 sc0 ls0 ws0">type<span class="fc0">(x)</span></div><div class="t m0 x11 hc y1e7 ffa fs3 fcf sc0 ls0 ws0">Out[9]:<span class="_ _2a"> </span><span class="fc0">float</span></div><div class="t m0 x4 h4 y1e8 ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _9"> </span><span class="ff5">有型別,然而這些型別不是連結到變數名,而是與物件<span class="ff2">本身相連</span>。</span></div><div class="t m0 x4 h4 y1e9 ff5 fs3 fc0 sc0 ls0 ws0">在物件導向的程式設計語言(如<span class="_ _3"> </span><span class="ff7">Python</span>)中,<span class="ff6">物件</span>(<span class="ff7">object</span>)是一個包含資料以及關聯的<span class="ff6">元數</span></div><div class="t m0 x5 h4 y1ea ff6 fs3 fc0 sc0 ls0 ws0">據<span class="ff5">(<span class="ff7">metadata</span>)和功能的實體。在<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>中一切都是物件,這意味著每個實體都有一些元數</span></div><div class="t m0 x5 h4 y1eb ff5 fs3 fc0 sc0 ls0 ws0">據(稱為<span class="ff6">屬性</span>(<span class="ff7">attribute</span>)<span class="_ _44"></span>)和關聯的功能(稱為<span class="ff6">方法</span>(<span class="ff7">method</span>)<span class="_ _44"></span>)<span class="_ _7"></span>。這些屬性和方法通過點號</div><div class="t m0 x30 h4 y1ec ff5 fs3 fc0 sc0 ls0 ws0">(<span class="ffa">.</span>)訪問。</div><div class="t m0 x4 h4 y1ed ff5 fs3 fc0 sc0 ls0 ws0">例如,在我們看到列表有一個<span class="_ _3"> </span><span class="ffa">append<span class="_ _3"> </span></span>方法之前,它將一個元素新增到列表中,並通過點</div><div class="t m0 x5 h4 y1ee ff5 fs3 fc0 sc0 ls0 ws0">號訪問:</div><div class="t m0 x11 hc y1ef ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[10]:<span class="_ _2a"> </span><span class="fc0">L<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[<span class="fc6">1</span>,<span class="_ _2a"> </span><span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">3</span>]</span></div><div class="t m0 x31 hc y1f0 ffa fs3 fc0 sc0 ls0 ws0">L<span class="fc6">.</span>append(<span class="fc6">100</span>)</div><div class="t m0 x31 hc y1f1 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(L)</span></div><div class="t m0 x11 hc y1f2 ffa fs3 fc0 sc0 ls0 ws0">[1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>3,<span class="_ _2a"> </span>100]</div><div class="t m0 x4 h4 y9a ff5 fs3 fc0 sc0 ls0 ws0">可能你一般會認為複合物件<span class="_ _8"></span>(如列表)<span class="_ _8"></span>具有屬性和方法,<span class="_ _40"></span>但令人意外的是在<span class="_ _9"> </span><span class="ff7">Python<span class="_ _34"> </span></span>中,<span class="_ _3e"></span>即</div><div class="t m0 x5 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">使是簡單型別也附加了屬性和方法。</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf19" class="pf w0 h0" data-page-no="19"><div class="pc pc19 w0 h0"><img class="bi x5 y1f3 w1 h1c" alt="" src="images/bg19.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">18<span class="_ _57"> </span><span class="ffc">4.<span class="_ _16"> </span><span class="ff6">基礎<span class="_ _9"> </span></span>PYTHON<span class="_ _9"> </span><span class="ff6">語意:變數和物件</span></span></div><div class="t m0 x4 h4 y42 ff5 fs3 fc0 sc0 ls0 ws0">比如,數值類型擁有<span class="_ _3"> </span><span class="ffa">real<span class="_ _3"> </span></span>和<span class="_ _a"> </span><span class="ffa">imag<span class="_ _3"> </span></span>屬性,儲存了這個值的實部和虛部(如果把它看成一</div><div class="t m0 x5 h4 y77 ff5 fs3 fc0 sc0 ls0 ws0">個複數)<span class="_ _33"></span>。</div><div class="t m0 x11 hc y1f4 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[11]:<span class="_ _2a"> </span><span class="fc0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>4.5</span></span></div><div class="t m0 x31 hc y1f5 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(x<span class="fc6">.</span>real,<span class="_ _2a"> </span><span class="fcc">"+"</span>,<span class="_ _2a"> </span>x<span class="fc6">.</span>imag,<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">i</span>'</span>)</span></div><div class="t m0 x11 hc y1f6 ffa fs3 fc0 sc0 ls0 ws0">4.5<span class="_ _2a"> </span>+<span class="_ _2a"> </span>0.0<span class="_ _2a"> </span>i</div><div class="t m0 x4 h4 y1f7 ff5 fs3 fc0 sc0 ls0 ws0">方法<span class="_ _4a"></span>和屬性<span class="_ _4a"></span>類別似,<span class="_ _4a"></span>不過<span class="_ _4a"></span>方法<span class="_ _4a"></span>是使用<span class="_ _4a"></span>一對<span class="_ _4a"></span>小括<span class="_ _4a"></span>號來<span class="_ _4a"></span>調用<span class="_ _4a"></span>的函數。<span class="_ _4a"></span>例如,浮<span class="_ _4a"></span>點數<span class="_ _4a"></span>有一個<span class="_ _4a"></span>叫</div><div class="t m0 x5 h4 y1f8 ff5 fs3 fc0 sc0 ls0 ws0">做<span class="_ _9"> </span><span class="ffa">is_integer<span class="_ _3"> </span></span>的方法來檢查這個值是否是一個整數:</div><div class="t m0 x11 hc y1f9 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[12]:<span class="_ _2a"> </span><span class="fc0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>4.5</span></span></div><div class="t m0 x31 hc y1fa ffa fs3 fc0 sc0 ls0 ws0">x<span class="fc6">.</span>is_integer()</div><div class="t m0 x11 hc y1fb ffa fs3 fcf sc0 ls0 ws0">Out[12]:<span class="_ _2a"> </span><span class="fc0">False</span></div><div class="t m0 x11 hc y1fc ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[13]:<span class="_ _2a"> </span><span class="fc0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>4.0</span></span></div><div class="t m0 x31 hc y1fd ffa fs3 fc0 sc0 ls0 ws0">x<span class="fc6">.</span>is_integer()</div><div class="t m0 x11 hc y1fe ffa fs3 fcf sc0 ls0 ws0">Out[13]:<span class="_ _2a"> </span><span class="fc0">True</span></div><div class="t m0 x4 h4 y1ff ff5 fs3 fc0 sc0 ls0 ws0">當我們說<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>中的所有東西都是一個物件時,我們的意思是<span class="ff2">一切都是一個物件</span>——即</div><div class="t m0 x5 h4 y200 ff5 fs3 fc0 sc0 ls0 ws0">使是物件的屬性和方法本身就是自己的一個有<span class="_ _9"> </span><span class="ffa">type<span class="_ _3"> </span></span>資訊的物件。</div><div class="t m0 x11 hc y201 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[14]:<span class="_ _2a"> </span><span class="fc4">type<span class="fc0">(x<span class="fc6">.</span>is_integer)</span></span></div><div class="t m0 x11 hc y202 ffa fs3 fcf sc0 ls0 ws0">Out[14]:<span class="_ _2a"> </span><span class="fc0">builtin_function_or_method</span></div><div class="t m0 x4 h4 y203 ff5 fs3 fc0 sc0 ls0 ws0">我們會發現<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>的一切皆物件的設計帶來了一些非常方便的語言結構。</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf1a" class="pf w0 h0" data-page-no="1a"><div class="pc pc1a w0 h0"><img class="bi x5 y40 w1 h1d" alt="" src="images/bg1a.png"/><div class="t m0 x32 h1 y3 ff2 fs0 fc0 sc0 ls0 ws0">第<span class="_ _0"> </span><span class="ff1">5<span class="_ _0"> </span></span>章<span class="_ _32"> </span>基礎<span class="_ _0"> </span><span class="ff1">Python<span class="_ _0"> </span></span>語法:運算子</div><div class="t m0 x4 h4 y204 ff5 fs3 fc0 sc0 ls0 ws0">在之前的章節中我們大致了解了<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>語法中關於變數和物件的知識。本章起我們將繼</div><div class="t m0 x5 h4 y205 ff5 fs3 fc0 sc0 ls0 ws0">續深入了解<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>語法中不同的<span class="ff6">運算子</span>(<span class="ff7">operator</span>)<span class="_ _7"></span>。本章結束之時,你將掌握一些基本的工</div><div class="t m0 x5 h4 y206 ff5 fs3 fc0 sc0 ls0 ws0">具來比較和操作<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>中的資料。</div><div class="t m0 x5 h8 y207 ff1 fs1 fc0 sc0 ls0 ws0">5.1<span class="_ _39"> </span><span class="ff2">算術運算子</span></div><div class="t m0 x4 h4 y208 ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _3"> </span><span class="ff5">實現了<span class="_ _3"> </span></span>7<span class="_ _3"> </span><span class="ff5">種基礎<span class="ff6">二元算術運算子</span>(</span>binary<span class="_ _3"> </span>arithmetic<span class="_ _3"> </span>operator<span class="ff5">)<span class="_ _7"></span>,其中的<span class="_ _3"> </span><span class="ff7">2<span class="_ _9"> </span></span>種可以</span></div><div class="t m0 x5 h4 y209 ff5 fs3 fc0 sc0 ls0 ws0">同時被用作<span class="ff6">一元運算子</span>(<span class="ff7">unary<span class="_ _9"> </span>operator</span>)<span class="_ _33"></span>。下表總結了這<span class="_ _3"> </span><span class="ff7">7<span class="_ _9"> </span></span>種運算子:</div><div class="t m0 x33 h5 y20a ff2 fs3 fc0 sc0 ls0 ws0">運算子<span class="_ _3c"> </span>名稱<span class="_ _59"> </span>描述</div><div class="t m0 x33 h4 y20b ffa fs3 fc0 sc0 ls0 ws0">a<span class="_ _2a"> </span>+<span class="_ _2a"> </span>b<span class="_ _5a"> </span><span class="ff5">加<span class="_ _5b"> </span></span>a<span class="_ _9"> </span><span class="ff5">與<span class="_ _3"> </span></span>b<span class="_ _9"> </span><span class="ff5">求和</span></div><div class="t m0 x33 h4 y20c ffa fs3 fc0 sc0 ls0 ws0">a<span class="_ _2a"> </span>-<span class="_ _2a"> </span>b<span class="_ _5a"> </span><span class="ff5">減<span class="_ _5b"> </span></span>a<span class="_ _9"> </span><span class="ff5">與<span class="_ _3"> </span></span>b<span class="_ _9"> </span><span class="ff5">求差</span></div><div class="t m0 x33 h4 y20d ffa fs3 fc0 sc0 ls0 ws0">a<span class="_ _2a"> </span>*<span class="_ _2a"> </span>b<span class="_ _5a"> </span><span class="ff5">乘<span class="_ _5b"> </span></span>a<span class="_ _9"> </span><span class="ff5">與<span class="_ _3"> </span></span>b<span class="_ _9"> </span><span class="ff5">求積</span></div><div class="t m0 x33 h4 y20e ffa fs3 fc0 sc0 ls0 ws0">a<span class="_ _2a"> </span>/<span class="_ _2a"> </span>b<span class="_ _5a"> </span><span class="ff5">真除<span class="_ _59"> </span></span>a<span class="_ _9"> </span><span class="ff5">與<span class="_ _9"> </span></span>b<span class="_ _3"> </span><span class="ff5">求商</span></div><div class="t m0 x33 h4 y20f ffa fs3 fc0 sc0 ls0 ws0">a<span class="_ _2a"> </span>//<span class="_ _2a"> </span>b<span class="_ _5c"> </span><span class="ff5">向下整除<span class="_ _5c"> </span></span>a<span class="_ _9"> </span><span class="ff5">與<span class="_ _3"> </span></span>b<span class="_ _9"> </span><span class="ff5">求商,去掉小數部分</span></div><div class="t m0 x33 h4 y210 ffa fs3 fc0 sc0 ls0 ws0">a<span class="_ _2a"> </span>%<span class="_ _2a"> </span>b<span class="_ _5a"> </span><span class="ff5">模<span class="_ _5b"> </span></span>a<span class="_ _9"> </span><span class="ff5">與<span class="_ _3"> </span></span>b<span class="_ _9"> </span><span class="ff5">取模</span></div><div class="t m0 x33 h4 y211 ffa fs3 fc0 sc0 ls0 ws0">a<span class="_ _2a"> </span>**<span class="_ _2a"> </span>b<span class="_ _5c"> </span><span class="ff5">乘方<span class="_ _59"> </span></span>a<span class="_ _9"> </span><span class="ff5">的<span class="_ _9"> </span></span>b<span class="_ _3"> </span><span class="ff5">次方</span></div><div class="t m0 x33 h4 y212 ffa fs3 fc0 sc0 ls0 ws0">-a<span class="_ _5d"> </span><span class="ff5">取反<span class="_ _59"> </span></span>a<span class="_ _9"> </span><span class="ff5">符號取反</span></div><div class="t m0 x33 h4 y213 ffa fs3 fc0 sc0 ls0 ws0">+a<span class="_ _5d"> </span><span class="ff5">單目加<span class="_ _52"> </span></span>a<span class="_ _9"> </span><span class="ff5">不變(極少使用)</span></div><div class="t m0 x4 h4 y214 ff5 fs3 fc0 sc0 ls0 ws0">這些運算子可以直接按照直覺使用並組合,並且按照標準規範添加括號來規定計算的優先</div><div class="t m0 x5 h4 y215 ff5 fs3 fc0 sc0 ls0 ws0">等級。如:</div><div class="t m0 x11 h6 y216 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[1]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">加、減、乘</span></span></div><div class="t m0 x24 hc y217 ffa fs3 fc0 sc0 ls0 ws0">(<span class="fc6">4<span class="_ _2a"> </span>+<span class="_ _2a"> </span>8</span>)<span class="_ _2a"> </span><span class="fc6">*<span class="_ _2a"> </span></span>(<span class="fc6">6.5<span class="_ _2a"> </span>-<span class="_ _2a"> </span>3</span>)</div><div class="t m0 x11 hc y218 ffa fs3 fcf sc0 ls0 ws0">Out[1]:<span class="_ _2a"> </span><span class="fc0">42.0</span></div><div class="t m0 x4 h4 y219 ff6 fs3 fc0 sc0 ls0 ws0">向下整除<span class="ff5">(<span class="ff7">floor<span class="_ _9"> </span>division</span>)的結果是</span>真除<span class="ff5">(<span class="ff7">true<span class="_ _3"> </span>division</span>)後去掉小數部分:</span></div><div class="t m0 x11 h6 y21a ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[2]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">真除</span></span></div><div class="t m0 x24 hc y117 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fc6">11<span class="_ _2a"> </span>/<span class="_ _2a"> </span>2</span>)</span></div><div class="t m0 x11 hc y119 ffa fs3 fc0 sc0 ls0 ws0">5.5</div><div class="t m0 x2f h7 y20 ff7 fs3 fc0 sc0 ls0 ws0">19</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf1b" class="pf w0 h0" data-page-no="1b"><div class="pc pc1b w0 h0"><img class="bi x5 y40 w1 h11" alt="" src="images/bg1b.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">20<span class="_ _5e"> </span><span class="ffc">5.<span class="_ _16"> </span><span class="ff6">基礎<span class="_ _9"> </span></span>PYTHON<span class="_ _9"> </span><span class="ff6">語法:運算子</span></span></div><div class="t m0 x11 h6 yfa ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[3]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">向下整除</span></span></div><div class="t m0 x24 hc yfb ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fc6">11<span class="_ _2a"> </span>//<span class="_ _2a"> </span>2</span>)</span></div><div class="t m0 x11 hc y21b ffa fs3 fc0 sc0 ls0 ws0">5</div><div class="t m0 x4 h4 y21c ff5 fs3 fc0 sc0 ls0 ws0">向下整除是在<span class="_ _9"> </span><span class="ff7">Python<span class="_ _9"> </span>3<span class="_ _3"> </span></span>中新增的新運算子。<span class="_ _8"></span>你應當意識到在<span class="_ _3"> </span><span class="ff7">Python<span class="_ _9"> </span>2<span class="_ _9"> </span></span>中標準除法運算子</div><div class="t m0 x30 h4 y190 ff5 fs3 fc0 sc0 ls0 ws0">(<span class="ffa">/</span>)對於整數來說執行的是向下整除,而對浮點數來說執行的是真除。</div><div class="t m0 x4 h4 y21d ff5 fs3 fc0 sc0 ls0 ws0">最後,<span class="_ _8"></span>我將提到<span class="_ _9"> </span><span class="ff7">Python<span class="_ _9"> </span>3.5<span class="_ _9"> </span></span>中新增的第<span class="_ _9"> </span><span class="ff7">8<span class="_ _9"> </span></span>個算術運算子:<span class="_ _8"></span><span class="ffa">a<span class="_ _10"> </span>@<span class="_ _2a"> </span>b<span class="ff5">。<span class="_ _8"></span>這個運算子對<span class="_ _9"> </span><span class="ffa">a<span class="_ _9"> </span></span>和<span class="_ _9"> </span><span class="ffa">b<span class="_ _9"> </span></span>執</span></span></div><div class="t m0 x5 h4 y21e ff5 fs3 fc0 sc0 ls0 ws0">行<span class="ff6">矩陣乘法</span>(<span class="ff7">matrix<span class="_ _9"> </span>product</span>)<span class="_ _33"></span>,用於許多<span class="ff6">線性代數</span>(<span class="ff7">linear<span class="_ _3"> </span>algebra</span>)的擴充套件函式庫。</div><div class="t m0 x5 h8 y21f ff1 fs1 fc0 sc0 ls0 ws0">5.2<span class="_ _39"> </span><span class="ff2">位元運算子</span></div><div class="t m0 x4 h4 y220 ff5 fs3 fc0 sc0 ls0 ws0">除<span class="_ _4a"></span>了<span class="_ _55"> </span>基<span class="_ _55"> </span>本<span class="_ _4a"></span>的算<span class="_ _55"> </span>術<span class="_ _55"> </span>運<span class="_ _4a"></span>算<span class="_ _55"> </span>符<span class="_ _55"> </span>外,<span class="ff7">Python<span class="_ _10"> </span></span>還<span class="_ _4a"></span>提<span class="_ _55"> </span>供<span class="_ _55"></span>了<span class="_ _4a"></span>對整<span class="_ _55"> </span>數<span class="_ _55"></span>進<span class="_ _4a"></span>行位<span class="_ _55"> </span>運<span class="_ _55"></span>算<span class="_ _4a"></span>的<span class="ff6">位<span class="_ _55"> </span>運<span class="_ _55"> </span>算<span class="_ _4a"></span>符<span class="_ _55"> </span></span>(<span class="ff7">bitwise</span></div><div class="t m0 x5 h4 y221 ff7 fs3 fc0 sc0 ls0 ws0">operator<span class="ff5">)<span class="_ _5f"></span>。相比基本運算子,這些運算子更少被使用,但是知道它們的存在是有用的。下表總</span></div><div class="t m0 x5 h4 y222 ff5 fs3 fc0 sc0 ls0 ws0">結了<span class="_ _9"> </span><span class="ff7">6<span class="_ _3"> </span></span>個位元運算子:</div><div class="t m0 x34 h5 y223 ff2 fs3 fc0 sc0 ls0 ws0">運算子<span class="_ _3c"> </span>名稱<span class="_ _59"> </span>描述</div><div class="t m0 x34 h4 y224 ffa fs3 fc0 sc0 ls0 ws0">a<span class="_ _2a"> </span>&<span class="_ _2a"> </span>b<span class="_ _5a"> </span><span class="ff5">按位與<span class="_ _52"> </span></span>a<span class="_ _9"> </span><span class="ff5">與<span class="_ _3"> </span></span>b<span class="_ _9"> </span><span class="ff5">全為<span class="_ _9"> </span></span>1<span class="_ _3"> </span><span class="ff5">時為<span class="_ _9"> </span></span>1<span class="ff5">,否則為<span class="_ _9"> </span></span>0</div><div class="t m0 x34 h4 y225 ffa fs3 fc0 sc0 ls0 ws0">a<span class="_ _2a"> </span>|<span class="_ _2a"> </span>b<span class="_ _5a"> </span><span class="ff5">按位或<span class="_ _52"> </span></span>a<span class="_ _9"> </span><span class="ff5">與<span class="_ _3"> </span></span>b<span class="_ _9"> </span><span class="ff5">全為<span class="_ _9"> </span></span>0<span class="_ _3"> </span><span class="ff5">時為<span class="_ _9"> </span></span>0<span class="ff5">,否則為<span class="_ _9"> </span></span>1</div><div class="t m0 x34 h4 y226 ffa fs3 fc0 sc0 ls0 ws0">a<span class="_ _2a"> </span>ˆ<span class="_ _2a"> </span>b<span class="_ _5a"> </span><span class="ff5">按位異或<span class="_ _5c"> </span></span>a<span class="_ _9"> </span><span class="ff5">與<span class="_ _3"> </span></span>b<span class="_ _9"> </span><span class="ff5">位相等時為<span class="_ _2a"> </span></span>0<span class="ff5">,否則為<span class="_ _9"> </span></span>1</div><div class="t m0 x34 h4 y227 ffa fs3 fc0 sc0 ls0 ws0">a<span class="_ _2a"> </span><<<span class="_ _2a"> </span>b<span class="_ _5c"> </span><span class="ff5">按位左移<span class="_ _5c"> </span></span>a<span class="_ _9"> </span><span class="ff5">左移<span class="_ _2a"> </span></span>b<span class="_ _3"> </span><span class="ff5">個單位</span></div><div class="t m0 x34 h4 y228 ffa fs3 fc0 sc0 ls0 ws0">a<span class="_ _2a"> </span>>><span class="_ _2a"> </span>b<span class="_ _5c"> </span><span class="ff5">按位右移<span class="_ _5c"> </span></span>a<span class="_ _9"> </span><span class="ff5">右移<span class="_ _3"> </span></span>b<span class="_ _9"> </span><span class="ff5">個單位</span></div><div class="t m0 x34 h4 y229 ffa fs3 fc0 sc0 ls0 ws0">~a<span class="_ _5d"> </span><span class="ff5">按位取反<span class="_ _5c"> </span>對<span class="_ _9"> </span></span>a<span class="_ _3"> </span><span class="ff5">按位取反</span></div><div class="t m0 x4 h4 y22a ff5 fs3 fc0 sc0 ls0 ws0">這些位元運算子在僅僅對數字使用<span class="ff6">二進位制表示</span>(<span class="ff7">binary<span class="_ _3"> </span>represen<span class="_ _8"></span>tation<span class="ff5">)時才有意義。你可以</span></span></div><div class="t m0 x5 h4 y22b ff5 fs3 fc0 sc0 ls0 ws0">使用內建的<span class="_ _9"> </span><span class="ffa">bin<span class="_ _3"> </span></span>函式檢視一個數的二進位制表示。</div><div class="t m0 x11 hc y22c ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[4]:<span class="_ _2a"> </span><span class="fc4">bin<span class="fc0">(<span class="fc6">10</span>)</span></span></div><div class="t m0 x11 hc y22d ffa fs3 fcf sc0 ls0 ws0">Out[4]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">0b1010</span>'</span></div><div class="t m0 x4 h4 y22e ff5 fs3 fc0 sc0 ls0 ws0">上述結果均有一個<span class="_ _34"> </span><span class="ffa">0b<span class="_ _9"> </span></span>字首,<span class="_ _2"></span>表示這是一個二進位制表示。<span class="_ _35"></span>剩餘的數字表明數字<span class="_ _9"> </span><span class="ff15">10<span class="_ _34"> </span></span>用<span class="_ _9"> </span><span class="ff15">1<span class="_ _4b"> </span><span class="ff16">·<span class="_ _4b"> </span></span>2</span></div><div class="t m0 x35 h1e y22f ff17 fs4 fc0 sc0 ls0 ws0">3</div><div class="t m0 x36 h7 y22e ff15 fs3 fc0 sc0 ls0 ws0">+</div><div class="t m0 x5 h7 y230 ff15 fs3 fc0 sc0 ls0 ws0">0<span class="_ _60"> </span><span class="ff16">·<span class="_ _60"> </span></span>2</div><div class="t m0 x37 h1e y231 ff17 fs4 fc0 sc0 ls0 ws0">2</div><div class="t m0 x29 h7 y230 ff15 fs3 fc0 sc0 ls0 ws0">+<span class="_ _60"> </span>1<span class="_ _60"> </span><span class="ff16">·<span class="_ _60"> </span></span>2</div><div class="t m0 x31 h1e y231 ff17 fs4 fc0 sc0 ls0 ws0">1</div><div class="t m0 x38 h7 y230 ff15 fs3 fc0 sc0 ls0 ws0">+<span class="_ _60"> </span>0<span class="_ _60"> </span><span class="ff16">·<span class="_ _60"> </span></span>2</div><div class="t m0 x39 h1e y231 ff17 fs4 fc0 sc0 ls0 ws0">0</div><div class="t m0 x3a h4 y230 ff5 fs3 fc0 sc0 ls0 ws0">的和表示出來了。類似地,我們可以輸入:</div><div class="t m0 x11 hc y232 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[5]:<span class="_ _2a"> </span><span class="fc4">bin<span class="fc0">(<span class="fc6">4</span>)</span></span></div><div class="t m0 x11 hc y233 ffa fs3 fcf sc0 ls0 ws0">Out[5]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">0b100</span>'</span></div><div class="t m0 x4 h4 y234 ff5 fs3 fc0 sc0 ls0 ws0">現在,使用按位或運算,我們可以將<span class="_ _9"> </span><span class="ff15">4<span class="_ _3"> </span></span>和<span class="_ _9"> </span><span class="ff15">10<span class="_ _9"> </span></span>的位合併起來:</div><div class="t m0 x11 hc y117 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[6]:<span class="_ _2a"> </span><span class="fc6">4<span class="_ _2a"> </span>|<span class="_ _2a"> </span>10</span></div><div class="t m0 x11 hc y119 ffa fs3 fcf sc0 ls0 ws0">Out[6]:<span class="_ _2a"> </span><span class="fc0">14</span></div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf1c" class="pf w0 h0" data-page-no="1c"><div class="pc pc1c w0 h0"><img class="bi x5 y235 w1 h1f" alt="" src="images/bg1c.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">5.3.<span class="_ _16"> </span><span class="ff6">賦值運算子<span class="_ _58"> </span><span class="ff7">21</span></span></div><div class="t m0 x11 hc yfa ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[7]:<span class="_ _2a"> </span><span class="fc4">bin<span class="fc0">(<span class="fc6">4<span class="_ _2a"> </span>|<span class="_ _2a"> </span>10</span>)</span></span></div><div class="t m0 x11 hc yfc ffa fs3 fcf sc0 ls0 ws0">Out[7]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">0b1110</span>'</span></div><div class="t m0 x4 h4 y236 ff5 fs3 fc0 sc0 ls0 ws0">這些位元運算子並不像基本算術運算子那樣直接有用,但是至少我們需要知道它們執行什麼</div><div class="t m0 x5 h4 y21c ff5 fs3 fc0 sc0 ls0 ws0">類型的操作。特別地,其他語言的用戶有時會在需要表示乘方運算(即<span class="_ _a"> </span><span class="ffa">a<span class="_ _2a"> </span>**<span class="_ _2a"> </span>b</span>)的時候誤用</div><div class="t m0 x5 h4 y190 ff5 fs3 fc0 sc0 ls0 ws0">成異或運算子(即<span class="_ _9"> </span><span class="ffa">a<span class="_ _2a"> </span>ˆ<span class="_ _2a"> </span>b</span>)<span class="_ _33"></span>。</div><div class="t m0 x5 h8 y237 ff1 fs1 fc0 sc0 ls0 ws0">5.3<span class="_ _39"> </span><span class="ff2">賦值運算子</span></div><div class="t m0 x4 h4 y238 ff5 fs3 fc0 sc0 ls0 ws0">我們已經知道了變數可以使用賦值運算子<span class="_ _8"></span>(<span class="ffa">=</span>)進行賦值,<span class="_ _2"></span>這時變數的值被儲存下來以便於</div><div class="t m0 x5 h4 y239 ff5 fs3 fc0 sc0 ls0 ws0">之後的使用。例如:</div><div class="t m0 x11 hc y23a ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[8]:<span class="_ _2a"> </span><span class="fc0">a<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>24</span></span></div><div class="t m0 x24 hc y23b ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(a)</span></div><div class="t m0 x11 hc y23c ffa fs3 fc0 sc0 ls0 ws0">24</div><div class="t m0 x4 h4 y23d ff5 fs3 fc0 sc0 ls0 ws0">我們可<span class="_ _4a"></span>以在前<span class="_ _4a"></span>面提到<span class="_ _4a"></span>的任何運<span class="_ _4a"></span>算符的<span class="_ _4a"></span>表達式<span class="_ _4a"></span>中使用這<span class="_ _4a"></span>些變數。例<span class="_ _4a"></span>如,如下<span class="_ _4a"></span>的語句<span class="_ _4a"></span>可以</div><div class="t m0 x5 h4 y23e ff5 fs3 fc0 sc0 ls0 ws0">給<span class="_ _9"> </span><span class="ffa">a<span class="_ _3"> </span></span>加上<span class="_ _9"> </span><span class="ff15">2</span>:</div><div class="t m0 x11 hc y23f ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[9]:<span class="_ _2a"> </span><span class="fc0">a<span class="_ _2a"> </span><span class="fc6">+<span class="_ _2a"> </span>2</span></span></div><div class="t m0 x11 hc y240 ffa fs3 fcf sc0 ls0 ws0">Out[9]:<span class="_ _2a"> </span><span class="fc0">26</span></div><div class="t m0 x4 h4 y241 ff5 fs3 fc0 sc0 ls0 ws0">我們可能需要將變數<span class="_ _13"> </span><span class="ffa">a<span class="_ _1"> </span></span>更新為這個新的值;在此情況下,我們可以將賦值運算子和加</div><div class="t m0 x5 h4 y242 ff5 fs3 fc0 sc0 ls0 ws0">法運算子合並起來:<span class="ffa">a<span class="_ _2a"> </span>=<span class="_ _2a"> </span>a<span class="_ _2a"> </span>+<span class="_ _2a"> </span>2</span>。因為這種將賦值運算子和算術運算子結合的操作非常普遍,</div><div class="t m0 x5 h4 y243 ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _9"> </span><span class="ff5">對所有算術運算子都內建了如下的更新運算子:</span></div><div class="t m0 x11 h6 y244 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[10]:<span class="_ _2a"> </span><span class="fc0">a<span class="_ _2a"> </span><span class="fc6">+=<span class="_ _2a"> </span>2<span class="_ _27"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">與<span class="_ _2a"> </span></span>a<span class="_ _2a"> </span>=<span class="_ _2a"> </span>a<span class="_ _2a"> </span>+<span class="_ _2a"> </span>2<span class="_ _2a"> </span><span class="ff6">等價</span></span></span></span></div><div class="t m0 x31 hc y245 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(a)</span></div><div class="t m0 x11 hc y246 ffa fs3 fc0 sc0 ls0 ws0">26</div><div class="t m0 x4 h4 y247 ff5 fs3 fc0 sc0 ls0 ws0">對應於之前列出的所有二元運算子,<span class="ff7">Python<span class="_ _9"> </span></span>提供了擴充套件的賦值運算子。下表總結了它們:</div><div class="t m0 x3b hc y248 ffa fs3 fc0 sc0 ls0 ws0">a<span class="_ _2a"> </span>+=<span class="_ _2a"> </span>b<span class="_ _5a"> </span>a<span class="_ _2a"> </span>-=<span class="_ _2a"> </span>b</div><div class="t m0 x3b hc y249 ffa fs3 fc0 sc0 ls0 ws0">a<span class="_ _2a"> </span>//=<span class="_ _2a"> </span>b<span class="_ _5c"> </span>a<span class="_ _2a"> </span>%=<span class="_ _2a"> </span>b</div><div class="t m0 x3b hc y24a ffa fs3 fc0 sc0 ls0 ws0">a<span class="_ _2a"> </span>|=<span class="_ _2a"> </span>b<span class="_ _5a"> </span>a<span class="_ _2a"> </span>ˆ=<span class="_ _2a"> </span>b</div><div class="t m0 x4 h4 y9a ff5 fs3 fc0 sc0 ls0 ws0">表中每一個運算子都與進行完對應的算術運算後跟一個賦值運算等價。具體來說:對於任</div><div class="t m0 x5 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">意運算子<span class="_ _9"> </span><span class="ff12">•</span>,<span class="_ _8"></span>表示式<span class="_ _2a"> </span><span class="ffa">a<span class="_ _2a"> </span><span class="ff12">•</span>=<span class="_ _2a"> </span>b<span class="_ _9"> </span></span>與<span class="_ _9"> </span><span class="ffa">a<span class="_ _2a"> </span>=<span class="_ _f"> </span>a<span class="_ _10"> </span><span class="ff12">•<span class="_ _2a"> </span></span>b<span class="_ _9"> </span></span>等價。<span class="_ _4"></span>對於可變物件,<span class="_ _8"></span>比如列表、<span class="_ _8"></span>陣列或者資料幀,</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf1d" class="pf w0 h0" data-page-no="1d"><div class="pc pc1d w0 h0"><img class="bi x5 y40 w1 h20" alt="" src="images/bg1d.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">22<span class="_ _5e"> </span><span class="ffc">5.<span class="_ _16"> </span><span class="ff6">基礎<span class="_ _9"> </span></span>PYTHON<span class="_ _9"> </span><span class="ff6">語法:運算子</span></span></div><div class="t m0 x5 h4 y42 ff5 fs3 fc0 sc0 ls0 ws0">上述擴充的賦值運算子實際上與它們更複雜的對應操作略有不同:它們<span class="ff2">直接修改</span>原有物件的內</div><div class="t m0 x5 h4 y77 ff5 fs3 fc0 sc0 ls0 ws0">容,而不是建立一個新的物件儲存運算結果。</div><div class="t m0 x5 h8 y24b ff1 fs1 fc0 sc0 ls0 ws0">5.4<span class="_ _39"> </span><span class="ff2">比較運算子</span></div><div class="t m0 x4 h4 y11e ff5 fs3 fc0 sc0 ls0 ws0">另一種非常有用的運算子是對不同的值進行比較。<span class="_ _2"></span>對此,<span class="_ _8"></span><span class="ff7">Python<span class="_ _34"> </span><span class="ff5">實現了標準的<span class="ff6">比較運算子</span></span></span></div><div class="t m0 x30 h4 y11f ff5 fs3 fc0 sc0 ls0 ws0">(<span class="ff7">comparison<span class="_ _9"> </span>operator</span>)<span class="_ _33"></span>,回傳布林值<span class="_ _3"> </span><span class="ffa">True<span class="_ _9"> </span></span>和<span class="_ _9"> </span><span class="ffa">False</span>。下表列出了所有的比較運算子:</div><div class="t m0 x3c h5 y24c ff2 fs3 fc0 sc0 ls0 ws0">運算子<span class="_ _3c"> </span>名稱<span class="_ _59"> </span>描述</div><div class="t m0 x3c h4 y24d ffa fs3 fc0 sc0 ls0 ws0">a<span class="_ _2a"> </span>==<span class="_ _2a"> </span>b<span class="_ _5c"> </span><span class="ff5">等於<span class="_ _59"> </span></span>a<span class="_ _9"> </span><span class="ff5">與<span class="_ _9"> </span></span>b<span class="_ _3"> </span><span class="ff5">相等</span></div><div class="t m0 x3c h4 y24e ffa fs3 fc0 sc0 ls0 ws0">a<span class="_ _2a"> </span>!=<span class="_ _2a"> </span>b<span class="_ _5c"> </span><span class="ff5">不等於<span class="_ _52"> </span></span>a<span class="_ _9"> </span><span class="ff5">不等於<span class="_ _3"> </span></span>b</div><div class="t m0 x3c h4 y24f ffa fs3 fc0 sc0 ls0 ws0">a<span class="_ _2a"> </span><<span class="_ _2a"> </span>b<span class="_ _5a"> </span><span class="ff5">小於<span class="_ _59"> </span></span>a<span class="_ _9"> </span><span class="ff5">小於<span class="_ _9"> </span></span>b</div><div class="t m0 x3c h4 y250 ffa fs3 fc0 sc0 ls0 ws0">a<span class="_ _2a"> </span>><span class="_ _2a"> </span>b<span class="_ _5a"> </span><span class="ff5">大於<span class="_ _59"> </span></span>a<span class="_ _9"> </span><span class="ff5">大於<span class="_ _9"> </span></span>b</div><div class="t m0 x3c h4 y251 ffa fs3 fc0 sc0 ls0 ws0">a<span class="_ _2a"> </span><=<span class="_ _2a"> </span>b<span class="_ _5c"> </span><span class="ff5">小於等於<span class="_ _5c"> </span></span>a<span class="_ _9"> </span><span class="ff5">小於或等於<span class="_ _3"> </span></span>b</div><div class="t m0 x3c h4 y252 ffa fs3 fc0 sc0 ls0 ws0">a<span class="_ _2a"> </span>>=<span class="_ _2a"> </span>b<span class="_ _5c"> </span><span class="ff5">大於等於<span class="_ _5c"> </span></span>a<span class="_ _9"> </span><span class="ff5">大於或等於<span class="_ _3"> </span></span>b</div><div class="t m0 x4 h4 y253 ff5 fs3 fc0 sc0 ls0 ws0">這些比較運算子可以與算術運算子和位元運算子任意組合來表達對數字幾乎各種的測試邏</div><div class="t m0 x5 h4 y254 ff5 fs3 fc0 sc0 ls0 ws0">輯。例如,我們可以通過檢查與<span class="_ _9"> </span><span class="ff15">2<span class="_ _3"> </span></span>的模是否回傳<span class="_ _9"> </span><span class="ff15">1<span class="_ _9"> </span></span>來測試一個數字是否為奇數:</div><div class="t m0 x11 h6 y255 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[11]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span>25<span class="_ _2a"> </span><span class="ff6">是奇數嗎?</span></span></div><div class="t m0 x31 hc y256 ffa fs3 fc6 sc0 ls0 ws0">25<span class="_ _2a"> </span>%<span class="_ _2a"> </span>2<span class="_ _2a"> </span>==<span class="_ _2a"> </span>1</div><div class="t m0 x11 hc y257 ffa fs3 fcf sc0 ls0 ws0">Out[11]:<span class="_ _2a"> </span><span class="fc0">True</span></div><div class="t m0 x11 h6 y258 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[12]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span>66<span class="_ _2a"> </span><span class="ff6">是奇數嗎?</span></span></div><div class="t m0 x31 hc y259 ffa fs3 fc6 sc0 ls0 ws0">66<span class="_ _2a"> </span>%<span class="_ _2a"> </span>2<span class="_ _2a"> </span>==<span class="_ _2a"> </span>1</div><div class="t m0 x11 hc y25a ffa fs3 fcf sc0 ls0 ws0">Out[12]:<span class="_ _2a"> </span><span class="fc0">False</span></div><div class="t m0 x4 h4 y25b ff5 fs3 fc0 sc0 ls0 ws0">我們可以串聯多個比較運算子來檢查更複雜的關係:</div><div class="t m0 x11 h6 y25c ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[13]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">檢查是否<span class="_ _2a"> </span></span>a<span class="_ _2a"> </span><span class="ff6">屬於區間<span class="_ _2a"> </span></span>(15,<span class="_ _2a"> </span>30)</span></div><div class="t m0 x31 hc y25d ffa fs3 fc0 sc0 ls0 ws0">a<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>25</span></div><div class="t m0 x31 hc y25e ffa fs3 fc6 sc0 ls0 ws0">15<span class="_ _2a"> </span><<span class="_ _2a"> </span><span class="fc0">a<span class="_ _2a"> </span></span><<span class="_ _2a"> </span>30</div><div class="t m0 x11 hc y25f ffa fs3 fcf sc0 ls0 ws0">Out[13]:<span class="_ _2a"> </span><span class="fc0">True</span></div><div class="t m0 x4 h4 y260 ff5 fs3 fc0 sc0 ls0 ws0">之後,來動動腦子想一下,看看下面這個比較關係:</div><div class="t m0 x11 hc y117 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[14]:<span class="_ _2a"> </span><span class="fc6">-1<span class="_ _2a"> </span>==<span class="_ _2a"> </span>~0</span></div><div class="t m0 x11 hc y119 ffa fs3 fcf sc0 ls0 ws0">Out[14]:<span class="_ _2a"> </span><span class="fc0">True</span></div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf1e" class="pf w0 h0" data-page-no="1e"><div class="pc pc1e w0 h0"><img class="bi x5 y261 w1 h21" alt="" src="images/bg1e.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">5.5.<span class="_ _16"> </span><span class="ff6">布林運算子<span class="_ _58"> </span><span class="ff7">23</span></span></div><div class="t m0 x4 h4 y42 ff5 fs3 fc0 sc0 ls0 ws0">回憶一下,<span class="_ _8"></span><span class="ffa">~<span class="_ _9"> </span><span class="ff5">是按位取反運算子。<span class="_ _8"></span>顯然,<span class="_ _4"></span>當你按位取反<span class="_ _9"> </span><span class="ff15">0<span class="_ _9"> </span></span>,<span class="_ _8"></span>你最後將得到<span class="_ _9"> </span><span class="ff16">−<span class="ff15">1</span></span>。<span class="_ _4"></span>如果你對為</span></span></div><div class="t m0 x5 h4 y77 ff5 fs3 fc0 sc0 ls0 ws0">什麼會得到這樣的結果而感到好奇,你可以查看<span class="ff6">補碼</span>(<span class="ff7">tw<span class="_ _8"></span>o’s<span class="_ _3"> </span>complement<span class="ff5">)整數編碼方案。這</span></span></div><div class="t m0 x5 h4 y78 ff5 fs3 fc0 sc0 ls0 ws0">是<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>用來編碼有符號整數的方案。思考一下,當你翻轉以這種方式編碼的整數的所有位</div><div class="t m0 x5 h4 y262 ff5 fs3 fc0 sc0 ls0 ws0">時會發生什麼?</div><div class="t m0 x5 h8 y263 ff1 fs1 fc0 sc0 ls0 ws0">5.5<span class="_ _39"> </span><span class="ff2">布林運算子</span></div><div class="t m0 x4 h4 y264 ff5 fs3 fc0 sc0 ls0 ws0">當使用布林值時,<span class="_ _2"></span><span class="ff7">Python<span class="_ _9"> </span><span class="ff5">提供了<span class="ff6">布林運算子<span class="_ _8"></span><span class="ff5">(<span class="ff7">boolean<span class="_ _9"> </span>operator</span>)使用<span class="_ _2"></span>“和”<span class="_ _61"></span>“或”<span class="_ _2"></span>和<span class="_ _8"></span>“非”</span></span></span></span></div><div class="t m0 x5 h4 y265 ff5 fs3 fc0 sc0 ls0 ws0">的標準概念組合不同的布林值。<span class="_ _35"></span>不出意外地,<span class="_ _35"></span>這些運算子直接使用單詞<span class="_ _9"> </span><span class="ffa">and</span>、<span class="_ _35"></span><span class="ffa">or<span class="_ _9"> </span><span class="ff5">和<span class="_ _34"> </span></span>not<span class="_ _9"> </span><span class="ff5">來表達:</span></span></div><div class="t m0 x11 hc y266 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[15]:<span class="_ _2a"> </span><span class="fc0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>4</span></span></div><div class="t m0 x31 hc y267 ffa fs3 fc0 sc0 ls0 ws0">(x<span class="_ _2a"> </span><span class="fc6"><<span class="_ _2a"> </span>6</span>)<span class="_ _2a"> </span><span class="ff11 fcb">and<span class="_ _2a"> </span></span>(x<span class="_ _2a"> </span><span class="fc6">><span class="_ _2a"> </span>2</span>)</div><div class="t m0 x11 hc y268 ffa fs3 fcf sc0 ls0 ws0">Out[15]:<span class="_ _2a"> </span><span class="fc0">True</span></div><div class="t m0 x11 hc y269 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[16]:<span class="_ _2a"> </span><span class="fc0">(x<span class="_ _2a"> </span><span class="fc6">><span class="_ _2a"> </span>10</span>)<span class="_ _2a"> </span><span class="ff11 fcb">or<span class="_ _2a"> </span></span>(x<span class="_ _2a"> </span><span class="fc6">%<span class="_ _2a"> </span>2<span class="_ _2a"> </span>==<span class="_ _2a"> </span>0</span>)</span></div><div class="t m0 x11 hc y26a ffa fs3 fcf sc0 ls0 ws0">Out[16]:<span class="_ _2a"> </span><span class="fc0">True</span></div><div class="t m0 x11 hc y26b ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[17]:<span class="_ _2a"> </span><span class="ff11 fcb">not<span class="_ _2a"> </span></span><span class="fc0">(x<span class="_ _2a"> </span><span class="fc6"><<span class="_ _2a"> </span>6</span>)</span></div><div class="t m0 x11 hc y26c ffa fs3 fcf sc0 ls0 ws0">Out[17]:<span class="_ _2a"> </span><span class="fc0">False</span></div><div class="t m0 x4 h4 y26d ff6 fs3 fc0 sc0 ls0 ws0">布<span class="_ _4a"></span>林<span class="_ _4a"></span>代<span class="_ _4a"></span>數<span class="_ _4a"></span><span class="ff5">(<span class="ff7">boolean<span class="_ _1"> </span>algebra</span>)<span class="_ _4a"></span>的<span class="_ _4a"></span>狂<span class="_ _4a"></span>熱<span class="_ _4a"></span>愛<span class="_ _4a"></span>好<span class="_ _55"></span>者<span class="_ _4a"></span>可<span class="_ _4a"></span>能<span class="_ _4a"></span>不<span class="_ _4a"></span>會<span class="_ _4a"></span>注<span class="_ _4a"></span>意<span class="_ _55"></span>到</span>異<span class="_ _4a"></span>或<span class="_ _4a"></span><span class="ff5">(<span class="ff7">exclusive<span class="_ _1"> </span>or</span>)<span class="_ _4a"></span>運<span class="_ _4a"></span>算</span></div><div class="t m0 x5 h4 y26e ff5 fs3 fc0 sc0 ls0 ws0">符<span class="_ _3"> </span><span class="ff7">X<span class="_ _8"></span>OR<span class="_ _3"> </span><span class="ff5">並沒有包括在內。達到異或的效果可以通過組合多種其他運算子的方式實現。你可以</span></span></div><div class="t m0 x5 h4 y26f ff5 fs3 fc0 sc0 ls0 ws0">使用下面這種聰明的小竅門實現布林值的異或運算:</div><div class="t m0 x11 hc y270 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[18]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span>(x<span class="_ _2a"> </span>><span class="_ _2a"> </span>1)<span class="_ _2a"> </span>xor<span class="_ _2a"> </span>(x<span class="_ _2a"> </span><<span class="_ _2a"> </span>10)</span></div><div class="t m0 x31 hc y271 ffa fs3 fc0 sc0 ls0 ws0">(x<span class="_ _2a"> </span><span class="fc6">><span class="_ _2a"> </span>1</span>)<span class="_ _2a"> </span><span class="fc6">!=<span class="_ _2a"> </span></span>(x<span class="_ _2a"> </span><span class="fc6"><<span class="_ _2a"> </span>10</span>)</div><div class="t m0 x11 hc y272 ffa fs3 fcf sc0 ls0 ws0">Out[18]:<span class="_ _2a"> </span><span class="fc0">False</span></div><div class="t m0 x4 h4 y273 ff5 fs3 fc0 sc0 ls0 ws0">當我們開始討論控制流程語句(比如迴圈語句和條件語句)時,這些布林運算子將會非常有</div><div class="t m0 x5 h4 y274 ff5 fs3 fc0 sc0 ls0 ws0">用。</div><div class="t m0 x4 h4 y275 ff5 fs3 fc0 sc0 ls0 ws0">一個較容易混淆的點在於什麼時候使用布林運算子(<span class="ffa">and</span>、<span class="ffa">or<span class="_ _9"> </span></span>和<span class="_ _3"> </span><span class="ffa">not</span>)<span class="_ _33"></span>,什麼時候使用位運</div><div class="t m0 x5 h4 y276 ff5 fs3 fc0 sc0 ls0 ws0">算符<span class="_ _8"></span>(<span class="ffa">&</span>、<span class="_ _8"></span><span class="ffa">|<span class="_ _34"> </span><span class="ff5">和<span class="_ _9"> </span></span>~<span class="ff5">)<span class="_ _33"></span>。<span class="_ _2"></span>答案取決於它們的名字:<span class="_ _2"></span>布林運算子應當在你需要對整個語句的<span class="ff2">布林值</span>(即</span></span></div><div class="t m0 x5 h4 y277 ff2 fs3 fc0 sc0 ls0 ws0">正確<span class="ff5">或者</span>錯誤<span class="ff5">)進行計算時用到;而位元運算子則應當在你需要對</span>單獨的位元<span class="ff5">或者不清楚物件的</span>某</div><div class="t m0 x5 h4 y278 ff2 fs3 fc0 sc0 ls0 ws0">個部分<span class="ff5">進行操作時用到。</span></div><div class="t m0 x5 h8 y279 ff1 fs1 fc0 sc0 ls0 ws0">5.6<span class="_ _39"> </span><span class="ff2">身份和成員運算子</span></div><div class="t m0 x4 h4 y27a ff5 fs3 fc0 sc0 ls0 ws0">就像<span class="_ _13"> </span><span class="ffa">and</span>、<span class="ffa">or<span class="_ _13"> </span></span>和<span class="_ _a"> </span><span class="ffa">not<span class="_ _13"> </span></span>一樣,<span class="ff7">Python<span class="_ _13"> </span></span>同樣提供了像英語散文一樣直接的運算子來檢查兩</div><div class="t m0 x5 h4 y27b ff5 fs3 fc0 sc0 ls0 ws0">個物件是否相同以及一個物件是否包含在另一個物件中。下表總結了這些運算子:</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf1f" class="pf w0 h0" data-page-no="1f"><div class="pc pc1f w0 h0"><img class="bi x5 y27c w1 h22" alt="" src="images/bg1f.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">24<span class="_ _5e"> </span><span class="ffc">5.<span class="_ _16"> </span><span class="ff6">基礎<span class="_ _9"> </span></span>PYTHON<span class="_ _9"> </span><span class="ff6">語法:運算子</span></span></div><div class="t m0 x3d h5 y27d ff2 fs3 fc0 sc0 ls0 ws0">運算子<span class="_ _62"> </span>描述</div><div class="t m0 x3d h4 y27e ffa fs3 fc0 sc0 ls0 ws0">a<span class="_ _2a"> </span>is<span class="_ _2a"> </span>b<span class="_ _5d"> </span><span class="ff5">當<span class="_ _9"> </span></span>a<span class="_ _3"> </span><span class="ff5">和<span class="_ _9"> </span></span>b<span class="_ _9"> </span><span class="ff5">為相同物件時為真</span></div><div class="t m0 x3d h4 y27f ffa fs3 fc0 sc0 ls0 ws0">a<span class="_ _2a"> </span>is<span class="_ _2a"> </span>not<span class="_ _2a"> </span>b<span class="_ _5c"> </span><span class="ff5">當<span class="_ _9"> </span></span>a<span class="_ _3"> </span><span class="ff5">和<span class="_ _9"> </span></span>b<span class="_ _9"> </span><span class="ff5">為相同物件時為假</span></div><div class="t m0 x3d h4 y280 ffa fs3 fc0 sc0 ls0 ws0">a<span class="_ _2a"> </span>in<span class="_ _2a"> </span>b<span class="_ _5d"> </span><span class="ff5">當<span class="_ _9"> </span></span>a<span class="_ _3"> </span><span class="ff5">是<span class="_ _9"> </span></span>b<span class="_ _9"> </span><span class="ff5">的一個成員時為真</span></div><div class="t m0 x3d h4 y281 ffa fs3 fc0 sc0 ls0 ws0">a<span class="_ _2a"> </span>not<span class="_ _2a"> </span>in<span class="_ _2a"> </span>b<span class="_ _5c"> </span><span class="ff5">當<span class="_ _9"> </span></span>a<span class="_ _3"> </span><span class="ff5">是<span class="_ _9"> </span></span>b<span class="_ _9"> </span><span class="ff5">的一個成員時為假</span></div><div class="t m0 x5 ha y282 ff1 fs2 fc0 sc0 ls0 ws0">5.6.1<span class="_ _3c"> </span><span class="ff2">身份運算子</span></div><div class="t m0 x4 h4 y283 ff6 fs3 fc0 sc0 ls0 ws0">身份運算子<span class="_ _8"></span><span class="ff5">(<span class="ff7">identit<span class="_ _8"></span>y<span class="_ _9"> </span>operator<span class="ff5">)<span class="_ _36"> </span><span class="ffa">is<span class="_ _9"> </span></span>和<span class="_ _9"> </span><span class="ffa">is<span class="_ _2a"> </span>not<span class="_ _9"> </span></span>檢查<span class="ff6">物件身份<span class="_ _8"></span><span class="ff5">(<span class="ff7">object<span class="_ _9"> </span>identit<span class="_ _8"></span>y<span class="ff5">)<span class="_ _8"></span>是否相同。</span></span></span></span></span></span></span></div><div class="t m0 x5 h4 y284 ff5 fs3 fc0 sc0 ls0 ws0">物件身份相同與<span class="ff6">物件相等</span>(<span class="ff7">object<span class="_ _9"> </span>equality</span>)不同,我們可以在下列例子中看到:</div><div class="t m0 x11 hc y285 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[19]:<span class="_ _2a"> </span><span class="fc0">a<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[<span class="fc6">1</span>,<span class="_ _2a"> </span><span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">3</span>]</span></div><div class="t m0 x31 hc y286 ffa fs3 fc0 sc0 ls0 ws0">b<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[<span class="fc6">1</span>,<span class="_ _2a"> </span><span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">3</span>]</div><div class="t m0 x11 hc y287 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[20]:<span class="_ _2a"> </span><span class="fc0">a<span class="_ _2a"> </span><span class="fc6">==<span class="_ _2a"> </span></span>b</span></div><div class="t m0 x11 hc y288 ffa fs3 fcf sc0 ls0 ws0">Out[20]:<span class="_ _2a"> </span><span class="fc0">True</span></div><div class="t m0 x11 hc y289 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[21]:<span class="_ _2a"> </span><span class="fc0">a<span class="_ _2a"> </span><span class="ff11 fcb">is<span class="_ _2a"> </span></span>b</span></div><div class="t m0 x11 hc y28a ffa fs3 fcf sc0 ls0 ws0">Out[21]:<span class="_ _2a"> </span><span class="fc0">False</span></div><div class="t m0 x11 hc y28b ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[22]:<span class="_ _2a"> </span><span class="fc0">a<span class="_ _2a"> </span><span class="ff11 fcb">is<span class="_ _2a"> </span>not<span class="_ _2a"> </span></span>b</span></div><div class="t m0 x11 hc y28c ffa fs3 fcf sc0 ls0 ws0">Out[22]:<span class="_ _2a"> </span><span class="fc0">True</span></div><div class="t m0 x4 h4 y28d ff5 fs3 fc0 sc0 ls0 ws0">什麼樣才算是物件身份相同呢?下列例子說明了這個問題:</div><div class="t m0 x11 hc y28e ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[23]:<span class="_ _2a"> </span><span class="fc0">a<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[<span class="fc6">1</span>,<span class="_ _2a"> </span><span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">3</span>]</span></div><div class="t m0 x31 hc y28f ffa fs3 fc0 sc0 ls0 ws0">b<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>a</div><div class="t m0 x31 hc y290 ffa fs3 fc0 sc0 ls0 ws0">a<span class="_ _2a"> </span><span class="ff11 fcb">is<span class="_ _2a"> </span></span>b</div><div class="t m0 x11 hc y291 ffa fs3 fcf sc0 ls0 ws0">Out[23]:<span class="_ _2a"> </span><span class="fc0">True</span></div><div class="t m0 x4 h4 y292 ff5 fs3 fc0 sc0 ls0 ws0">上述兩個例子的區別在於:第一個例子中<span class="_ _9"> </span><span class="ffa">a<span class="_ _9"> </span></span>和<span class="_ _3"> </span><span class="ffa">b<span class="_ _9"> </span></span>指向了<span class="ff2">不同的物件</span>,而第二個例子中它們</div><div class="t m0 x5 h4 y293 ff5 fs3 fc0 sc0 ls0 ws0">指向了<span class="ff2">相同的物件</span>。如我們在上一節中看到的,<span class="ff7">Python<span class="_ _3"> </span></span>變數實際上是指標。<span class="ffa">is<span class="_ _3"> </span></span>運算子檢查的</div><div class="t m0 x5 h4 y294 ff5 fs3 fc0 sc0 ls0 ws0">是兩個變數是否指向同一個容器(物件)<span class="_ _41"></span>,<span class="_ _8"></span>而不是檢查它們指向的容器中內容是否一致。<span class="_ _8"></span>有了這</div><div class="t m0 x5 h4 y295 ff5 fs3 fc0 sc0 ls0 ws0">個概念,你就會發現很多情況下初學者想要使用<span class="_ _9"> </span><span class="ffa">is</span>,實際上他們大多真正意味著<span class="_ _3"> </span><span class="ffa">==</span>。</div><div class="t m0 x5 ha y296 ff1 fs2 fc0 sc0 ls0 ws0">5.6.2<span class="_ _3c"> </span><span class="ff2">成員運算子</span></div><div class="t m0 x4 h4 y9a ff6 fs3 fc0 sc0 ls0 ws0">成員運算子<span class="ff5">(<span class="ff7">membership<span class="_ _3"> </span>operator</span>)在</span>複合物件<span class="ff5">(<span class="ff7">compound<span class="_ _a"> </span>object</span>)中檢查成員存在情</span></div><div class="t m0 x5 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">況。比如,我們可以寫:</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf20" class="pf w0 h0" data-page-no="20"><div class="pc pc20 w0 h0"><img class="bi x5 y297 w1 h23" alt="" src="images/bg20.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">5.6.<span class="_ _16"> </span><span class="ff6">身份和成員運算子<span class="_ _63"> </span><span class="ff7">25</span></span></div><div class="t m0 x11 hc yfa ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[24]:<span class="_ _2a"> </span><span class="fc6">1<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span><span class="fc0">[</span>1<span class="fc0">,<span class="_ _2a"> </span></span>2<span class="fc0">,<span class="_ _2a"> </span></span>3<span class="fc0">]</span></span></div><div class="t m0 x11 hc yfc ffa fs3 fcf sc0 ls0 ws0">Out[24]:<span class="_ _2a"> </span><span class="fc0">True</span></div><div class="t m0 x11 hc yfd ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[25]:<span class="_ _2a"> </span><span class="fc6">2<span class="_ _2a"> </span><span class="ff11 fcb">not<span class="_ _2a"> </span>in<span class="_ _2a"> </span></span><span class="fc0">[</span>1<span class="fc0">,<span class="_ _2a"> </span></span>2<span class="fc0">,<span class="_ _2a"> </span></span>3<span class="fc0">]</span></span></div><div class="t m0 x11 hc yff ffa fs3 fcf sc0 ls0 ws0">Out[25]:<span class="_ _2a"> </span><span class="fc0">False</span></div><div class="t m0 x4 h4 y21e ff5 fs3 fc0 sc0 ls0 ws0">這些成員操作是讓<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>相比低階語言(如<span class="_ _3"> </span><span class="ff7">C<span class="_ _3"> </span></span>語言)更容易使用的一個例子。在<span class="_ _3"> </span><span class="ff7">C<span class="_ _a"> </span></span>語</div><div class="t m0 x5 h4 y298 ff5 fs3 fc0 sc0 ls0 ws0">言<span class="_ _4a"></span>中,<span class="_ _4a"></span>成<span class="_ _4a"></span>員<span class="_ _4a"></span>的<span class="_ _4a"></span>檢<span class="_ _4a"></span>查<span class="_ _4a"></span>需<span class="_ _55"></span>要<span class="_ _4a"></span>在列<span class="_ _4a"></span>表<span class="_ _4a"></span>中<span class="_ _4a"></span>手<span class="_ _4a"></span>動<span class="_ _4a"></span>構<span class="_ _4a"></span>造<span class="_ _4a"></span>一<span class="_ _55"></span>個<span class="_ _4a"></span>循<span class="_ _4a"></span>環<span class="_ _4a"></span>後<span class="_ _4a"></span>檢<span class="_ _4a"></span>查<span class="_ _4a"></span>每<span class="_ _4a"></span>個<span class="_ _4a"></span>元<span class="_ _55"></span>素<span class="_ _4a"></span>是<span class="_ _4a"></span>否<span class="_ _4a"></span>相<span class="_ _4a"></span>等<span class="_ _4a"></span>來<span class="_ _4a"></span>實<span class="_ _4a"></span>現。<span class="_ _4a"></span>而</div><div class="t m0 x5 h4 y299 ff5 fs3 fc0 sc0 ls0 ws0">在<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>中,你只需要像寫英語句子一樣直接地鍵入你想要知道的就可以實現。</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf21" class="pf w0 h0" data-page-no="21"><div class="pc pc21 w0 h0"></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf22" class="pf w0 h0" data-page-no="22"><div class="pc pc22 w0 h0"><img class="bi x5 y29a w1 h24" alt="" src="images/bg22.png"/><div class="t m0 x3e h1 y3 ff2 fs0 fc0 sc0 ls0 ws0">第<span class="_ _0"> </span><span class="ff1">6<span class="_ _0"> </span></span>章<span class="_ _32"> </span>內建型別:簡單變數型別</div><div class="t m0 x4 h4 ydd ff5 fs3 fc0 sc0 ls0 ws0">談到<span class="_ _34"> </span><span class="ff7">Python<span class="_ _9"> </span></span>的變數和物件,<span class="_ _35"></span>我們會發現所有的<span class="_ _34"> </span><span class="ff7">Python<span class="_ _9"> </span></span>物件都附帶有相應的型別資訊。<span class="_ _35"></span>這</div><div class="t m0 x5 h4 yde ff5 fs3 fc0 sc0 ls0 ws0">裡我們將簡要地介紹一遍<span class="_ _34"> </span><span class="ff7">Python<span class="_ _34"> </span></span>內建的簡單變數型別。<span class="_ _3e"></span>我們所說的<span class="_ _35"></span>“<span class="ff6">簡單型別<span class="_ _8"></span><span class="ff5">(<span class="ff7">simple<span class="_ _34"> </span>type</span>)<span class="_ _33"></span>”</span></span></div><div class="t m0 x5 h4 ydf ff5 fs3 fc0 sc0 ls0 ws0">是與一些<span class="ff6">複合型別</span>(<span class="ff7">compound<span class="_ _9"> </span>type</span>)相區別的,在後面的章節中我們會繼續討論複合型別。</div><div class="t m0 x4 h4 ye1 ff5 fs3 fc0 sc0 ls0 ws0">下表總結了</div><div class="t m0 x21 h7 ye2 ff7 fs3 fc0 sc0 ls0 ws0">Python</div><div class="t m0 xc h4 ye1 ff5 fs3 fc0 sc0 ls0 ws0">的簡單變數型別:</div><div class="t m0 x28 h5 y29b ff2 fs3 fc0 sc0 ls0 ws0">型別<span class="_ _64"> </span>舉例<span class="_ _65"> </span>描述</div><div class="t m0 x28 h4 y29c ffa fs3 fc0 sc0 ls0 ws0">int<span class="_ _66"> </span>x<span class="_ _2a"> </span>=<span class="_ _2a"> </span>1<span class="_ _66"> </span><span class="ff5">整數(對應包括負數和零的整數域)</span></div><div class="t m0 x28 h4 y29d ffa fs3 fc0 sc0 ls0 ws0">float<span class="_ _67"> </span>x<span class="_ _2a"> </span>=<span class="_ _2a"> </span>1.0<span class="_ _67"> </span><span class="ff5">浮點數(對應實數域)</span></div><div class="t m0 x28 h4 y29e ffa fs3 fc0 sc0 ls0 ws0">complex<span class="_ _5a"> </span>x<span class="_ _2a"> </span>=<span class="_ _2a"> </span>1<span class="_ _2a"> </span>+<span class="_ _2a"> </span>2j<span class="_ _5c"> </span><span class="ff5">複數(包括實部與虛部的複數)</span></div><div class="t m0 x28 h4 y29f ffa fs3 fc0 sc0 ls0 ws0">bool<span class="_ _5d"> </span>x<span class="_ _2a"> </span>=<span class="_ _2a"> </span>True<span class="_ _32"> </span><span class="ff5">布林值:表示真和假</span></div><div class="t m0 x28 h4 y2a0 ffa fs3 fc0 sc0 ls0 ws0">str<span class="_ _66"> </span>x<span class="_ _2a"> </span>=<span class="_ _2a"> </span><span class="ff12">'</span>abc<span class="ff12">'<span class="_ _5a"> </span><span class="ff5">字串:表示字元或者文字</span></span></div><div class="t m0 x28 h4 y2a1 ffa fs3 fc0 sc0 ls0 ws0">NoneType<span class="_ _5c"> </span>x<span class="_ _2a"> </span>=<span class="_ _2a"> </span>None<span class="_ _32"> </span><span class="ff5">特殊的用以表示空的物件</span></div><div class="t m0 x4 h4 y2a2 ff5 fs3 fc0 sc0 ls0 ws0">我們會快速地將這些型別依次介紹一遍。</div><div class="t m0 x5 h8 y2a3 ff1 fs1 fc0 sc0 ls0 ws0">6.1<span class="_ _39"> </span><span class="ff2">整數</span></div><div class="t m0 x4 h4 y2a4 ff5 fs3 fc0 sc0 ls0 ws0">最基本的用來表示數字的型別是<span class="ff6">整數</span>(<span class="ff7">in<span class="_ _4"></span>teger<span class="ff5">)<span class="_ _33"></span>。所有不含小數點的數字都是整數:</span></span></div><div class="t m0 x11 hc y2a5 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[1]:<span class="_ _2a"> </span><span class="fc0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>1</span></span></div><div class="t m0 x24 hc y2a6 ffa fs3 fc4 sc0 ls0 ws0">type<span class="fc0">(x)</span></div><div class="t m0 x11 hc y2a7 ffa fs3 fcf sc0 ls0 ws0">Out[1]:<span class="_ _2a"> </span><span class="fc0">int</span></div><div class="t m0 x4 h4 y2a8 ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _34"> </span><span class="ff5">的整數實際上比<span class="_ _34"> </span></span>C<span class="_ _34"> </span><span class="ff5">語言中的整數更為複雜。<span class="_ _40"></span><span class="ff7">C<span class="_ _34"> </span><span class="ff5">的整數是<span class="ff6">固定精度<span class="_ _8"></span><span class="ff5">(<span class="ff7">fixed-precision</span>)</span></span></span></span></span></div><div class="t m0 x5 h4 y2a9 ff5 fs3 fc0 sc0 ls0 ws0">的,<span class="_ _3e"></span>而且通常會發生溢位現象<span class="_ _8"></span>(通常發生在整數值靠近<span class="_ _34"> </span><span class="ff15">2</span></div><div class="t m0 x3f h1e y2aa ff17 fs4 fc0 sc0 ls0 ws0">31</div><div class="t m0 x40 h4 y2a9 ff5 fs3 fc0 sc0 ls0 ws0">或者<span class="_ _34"> </span><span class="ff15">2</span></div><div class="t m0 x41 h1e y2aa ff17 fs4 fc0 sc0 ls0 ws0">63</div><div class="t m0 x42 h4 y2a9 ff5 fs3 fc0 sc0 ls0 ws0">時,<span class="_ _3e"></span>具體取決於你的系統)<span class="_ _33"></span>。</div><div class="t m0 x5 h4 y2ab ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _3"> </span><span class="ff5">的整數則是<span class="ff6">可變精度</span>(</span>v<span class="_ _8"></span>ariable-precision<span class="ff5">)的,因而你可以完成一些在其他語言中往往</span></div><div class="t m0 x5 h4 y2ac ff5 fs3 fc0 sc0 ls0 ws0">會溢位的計算:</div><div class="t m0 x11 hc y2ad ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[2]:<span class="_ _2a"> </span><span class="fc6">2<span class="_ _2a"> </span>**<span class="_ _2a"> </span>200</span></div><div class="t m0 x11 hc y2ae ffa fs3 fcf sc0 ls0 ws0">Out[2]:<span class="_ _2a"> </span><span class="fc0">1606938044258990275541962092341162602522202993782792835301376</span></div><div class="t m0 x4 h4 y40 ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _9"> </span><span class="ff5">整數另外一個方便的特性是它的除法結果可以被預設地轉換成浮點型別:</span></div><div class="t m0 x2f h7 y20 ff7 fs3 fc0 sc0 ls0 ws0">27</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf23" class="pf w0 h0" data-page-no="23"><div class="pc pc23 w0 h0"><img class="bi x5 y40 w1 h11" alt="" src="images/bg23.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">28<span class="_ _68"> </span><span class="ffc">6.<span class="_ _16"> </span><span class="ff6">內建型別:簡單變數型別</span></span></div><div class="t m0 x11 hc yfa ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[3]:<span class="_ _2a"> </span><span class="fc6">5<span class="_ _2a"> </span>/<span class="_ _2a"> </span>2</span></div><div class="t m0 x11 hc yfc ffa fs3 fcf sc0 ls0 ws0">Out[3]:<span class="_ _2a"> </span><span class="fc0">2.5</span></div><div class="t m0 x4 h4 y2af ff5 fs3 fc0 sc0 ls0 ws0">需要注意的是這一向上轉換是<span class="_ _9"> </span><span class="ff7">Python<span class="_ _9"> </span>3<span class="_ _3"> </span></span>的一個特性;而在<span class="_ _34"> </span><span class="ff7">Python<span class="_ _3"> </span>2<span class="_ _9"> </span></span>中,整數除法截斷了</div><div class="t m0 x5 h4 y2b0 ff5 fs3 fc0 sc0 ls0 ws0">小數部分並回傳一個整數的結果,就像許多類似於<span class="_ _9"> </span><span class="ff7">C<span class="_ _3"> </span></span>語言的靜態型別語言一樣:</div><div class="t m0 x11 h6 y2b1 ff14 fs3 fc8 sc0 ls0 ws0">#<span class="_ _2a"> </span>Python<span class="_ _2a"> </span>2<span class="_ _2a"> </span><span class="ff6">的特性</span></div><div class="t m0 x11 hc y2b2 ffa fs3 fc6 sc0 ls0 ws0">>>><span class="_ _2a"> </span><span class="fc7">5<span class="_ _2a"> </span></span>/<span class="_ _2a"> </span><span class="fc7">2</span></div><div class="t m0 x11 hc y2b3 ffa fs3 fc7 sc0 ls0 ws0">2</div><div class="t m0 x4 h4 y2b4 ff5 fs3 fc0 sc0 ls0 ws0">如果想要在</div><div class="t m0 x21 h7 y2b5 ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _9"> </span>3</div><div class="t m0 x43 h4 y2b4 ff5 fs3 fc0 sc0 ls0 ws0">中復原這種行為的整除,你可以採用向下整除運算子:</div><div class="t m0 x11 hc y2b6 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[4]:<span class="_ _2a"> </span><span class="fc6">5<span class="_ _2a"> </span>//<span class="_ _2a"> </span>2</span></div><div class="t m0 x11 hc y2b7 ffa fs3 fcf sc0 ls0 ws0">Out[4]:<span class="_ _2a"> </span><span class="fc0">2</span></div><div class="t m0 x4 h4 y2b8 ff5 fs3 fc0 sc0 ls0 ws0">最後,需要注意的是<span class="_ _3"> </span><span class="ff7">Python<span class="_ _9"> </span>2.<span class="ffd">x<span class="_ _3"> </span></span></span>同時擁有<span class="_ _3"> </span><span class="ffa">int<span class="_ _9"> </span></span>和<span class="_ _3"> </span><span class="ffa">long<span class="_ _9"> </span></span>兩種型別,而<span class="_ _3"> </span><span class="ff7">Python<span class="_ _9"> </span>3<span class="_ _3"> </span></span>則將這兩</div><div class="t m0 x5 h4 y2b9 ff5 fs3 fc0 sc0 ls0 ws0">種類型整合到了<span class="_ _9"> </span><span class="ffa">int<span class="_ _3"> </span></span>這一個單獨的型別當中。</div><div class="t m0 x5 h8 y2ba ff1 fs1 fc0 sc0 ls0 ws0">6.2<span class="_ _39"> </span><span class="ff2">浮點數</span></div><div class="t m0 x4 h4 y2bb ff6 fs3 fc0 sc0 ls0 ws0">浮點數<span class="_ _8"></span><span class="ff5">(<span class="ff7">floating-poin<span class="_ _8"></span>t<span class="_ _9"> </span>num<span class="_ _8"></span>ber<span class="ff5">)型別可以用來儲存小數。<span class="_ _2"></span>它們可以用標準十進位制表示法或</span></span></span></div><div class="t m0 x5 h4 y2bc ff5 fs3 fc0 sc0 ls0 ws0">指數表示法來表示:</div><div class="t m0 x11 hc y2bd ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[5]:<span class="_ _2a"> </span><span class="fc0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>0.000005</span></span></div><div class="t m0 x24 hc y2be ffa fs3 fc0 sc0 ls0 ws0">y<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>5e-6</span></div><div class="t m0 x24 hc y2bf ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(x<span class="_ _2a"> </span><span class="fc6">==<span class="_ _2a"> </span></span>y)</span></div><div class="t m0 x11 hc y2c0 ffa fs3 fc0 sc0 ls0 ws0">True</div><div class="t m0 x11 hc y2c1 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[6]:<span class="_ _2a"> </span><span class="fc0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>1400000.00</span></span></div><div class="t m0 x24 hc y2c2 ffa fs3 fc0 sc0 ls0 ws0">y<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>1.4e6</span></div><div class="t m0 x24 hc y2c3 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(x<span class="_ _2a"> </span><span class="fc6">==<span class="_ _2a"> </span></span>y)</span></div><div class="t m0 x11 hc y2c4 ffa fs3 fc0 sc0 ls0 ws0">True</div><div class="t m0 x4 h4 y2c5 ff5 fs3 fc0 sc0 ls0 ws0">在<span class="ff6">指數表示法</span>(<span class="ff7">exponential<span class="_ _3"> </span>notation</span>)中,<span class="ffa">e<span class="_ _a"> </span></span>或者<span class="_ _3"> </span><span class="ffa">E<span class="_ _a"> </span></span>可以被解讀為“乘以<span class="_ _a"> </span><span class="ff15">10<span class="_ _3"> </span></span>的幾次方”<span class="_ _44"></span>。</div><div class="t m0 x5 h4 y2c6 ff5 fs3 fc0 sc0 ls0 ws0">因而<span class="_ _9"> </span><span class="ffa">1.4e6<span class="_ _3"> </span></span>可以被解釋成<span class="_ _9"> </span><span class="ff15">1<span class="ffd">.</span>4<span class="_ _60"> </span><span class="ff16">×<span class="_ _60"> </span></span>10</span></div><div class="t m0 x18 h1e y2c7 ff17 fs4 fc0 sc0 ls0 ws0">6</div><div class="t m0 x44 h4 y2c6 ff5 fs3 fc0 sc0 ls0 ws0">。</div><div class="t m0 x4 h4 y2c8 ff5 fs3 fc0 sc0 ls0 ws0">一個整數可以用<span class="_ _9"> </span><span class="ffa">float<span class="_ _3"> </span></span>的<span class="ff6">建構函式</span>(<span class="ff7">constructor</span>)來明確地轉換為一個浮點數:</div><div class="t m0 x11 hc y117 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[7]:<span class="_ _2a"> </span><span class="fc4">float<span class="fc0">(<span class="fc6">1</span>)</span></span></div><div class="t m0 x11 hc y119 ffa fs3 fcf sc0 ls0 ws0">Out[7]:<span class="_ _2a"> </span><span class="fc0">1.0</span></div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf24" class="pf w0 h0" data-page-no="24"><div class="pc pc24 w0 h0"><img class="bi x5 y2c9 w1 h25" alt="" src="images/bg24.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">6.2.<span class="_ _16"> </span><span class="ff6">浮點數<span class="_ _69"> </span><span class="ff7">29</span></span></div><div class="t m0 x5 ha y2ca ff1 fs2 fc0 sc0 ls0 ws0">6.2.1<span class="_ _3c"> </span><span class="ff2">浮點數精度</span></div><div class="t m0 x4 h4 y2cb ff5 fs3 fc0 sc0 ls0 ws0">在浮點數的計算中需要注意的一點是它的小數部分精度<span class="ff2">有限</span>,因而有可能導致在進行<span class="ff2">等於</span></div><div class="t m0 x5 h4 y2cc ff5 fs3 fc0 sc0 ls0 ws0">比較的時候結果不穩定。例如:</div><div class="t m0 x11 hc y2cd ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[8]:<span class="_ _2a"> </span><span class="fc6">0.1<span class="_ _2a"> </span>+<span class="_ _2a"> </span>0.2<span class="_ _2a"> </span>==<span class="_ _2a"> </span>0.3</span></div><div class="t m0 x11 hc y2ce ffa fs3 fcf sc0 ls0 ws0">Out[8]:<span class="_ _2a"> </span><span class="fc0">False</span></div><div class="t m0 x4 h4 y2cf ff5 fs3 fc0 sc0 ls0 ws0">為什麼會這樣呢?事實上這不是<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>一種語言中存在的問題,而是因為大多數科學計</div><div class="t m0 x5 h4 y2d0 ff5 fs3 fc0 sc0 ls0 ws0">算平台都採用固定精度的格式來儲存二進制浮點數。所有使用浮點數的程式語言都使用固定的</div><div class="t m0 x5 h4 y2d1 ff5 fs3 fc0 sc0 ls0 ws0">位數來存放這些浮點數,這就導致只能表示出某些數字的大約的值。我們可以通過輸出這三個</div><div class="t m0 x5 h4 y2d2 ff5 fs3 fc0 sc0 ls0 ws0">數字的高精度形式來發現這一點:</div><div class="t m0 x11 hc y2d3 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[9]:<span class="_ _2a"> </span><span class="fc4">print<span class="fc0">(<span class="fcc">"0.1<span class="_ _2a"> </span>=<span class="_ _2a"> </span><span class="ff11 fc10">{0:.17f}</span>"<span class="fc6">.</span></span>format(<span class="fc6">0.1</span>))</span></span></div><div class="t m0 x24 hc y2d4 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fcc">"0.2<span class="_ _2a"> </span>=<span class="_ _2a"> </span><span class="ff11 fc10">{0:.17f}</span>"<span class="fc6">.</span></span>format(<span class="fc6">0.2</span>))</span></div><div class="t m0 x24 hc y2d5 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fcc">"0.3<span class="_ _2a"> </span>=<span class="_ _2a"> </span><span class="ff11 fc10">{0:.17f}</span>"<span class="fc6">.</span></span>format(<span class="fc6">0.3</span>))</span></div><div class="t m0 x11 hc y2d6 ffa fs3 fc0 sc0 ls0 ws0">0.1<span class="_ _2a"> </span>=<span class="_ _2a"> </span>0.10000000000000001</div><div class="t m0 x11 hc y2d7 ffa fs3 fc0 sc0 ls0 ws0">0.2<span class="_ _2a"> </span>=<span class="_ _2a"> </span>0.20000000000000001</div><div class="t m0 x11 hc y2d8 ffa fs3 fc0 sc0 ls0 ws0">0.3<span class="_ _2a"> </span>=<span class="_ _2a"> </span>0.29999999999999999</div><div class="t m0 x4 h4 y2d9 ff5 fs3 fc0 sc0 ls0 ws0">我們已經習慣了十進制下的小數表示方式,因而所有的分數都必須被表示成<span class="_ _3"> </span><span class="ff15">10<span class="_ _a"> </span></span>的冪次的</div><div class="t m0 x5 h4 y2da ff5 fs3 fc0 sc0 ls0 ws0">形式:</div><div class="t m0 x45 h7 y2db ff15 fs3 fc0 sc0 ls0 ws0">1</div><div class="t m0 x45 h7 y2dc ff15 fs3 fc0 sc0 ls0 ws0">8</div><div class="t m0 x46 h7 y2dd ff15 fs3 fc0 sc0 ls0 ws0">=<span class="_ _36"> </span>1<span class="_ _60"> </span><span class="ff16">·<span class="_ _60"> </span></span>10</div><div class="t m0 x3 h1e y2de ff18 fs4 fc0 sc0 ls0 ws0">−<span class="ff17">1</span></div><div class="t m0 x47 h7 y2dd ff15 fs3 fc0 sc0 ls0 ws0">+<span class="_ _60"> </span>2<span class="_ _60"> </span><span class="ff16">·<span class="_ _60"> </span></span>10</div><div class="t m0 x48 h1e y2de ff18 fs4 fc0 sc0 ls0 ws0">−<span class="ff17">2</span></div><div class="t m0 x49 h7 y2dd ff15 fs3 fc0 sc0 ls0 ws0">+<span class="_ _60"> </span>5<span class="_ _60"> </span><span class="ff16">·<span class="_ _60"> </span></span>10</div><div class="t m0 x4a h1e y2de ff18 fs4 fc0 sc0 ls0 ws0">−<span class="ff17">3</span></div><div class="t m0 x5 h4 y2df ff5 fs3 fc0 sc0 ls0 ws0">在十進位制下,我們將其表達成熟悉的小數形式:<span class="ff15">0<span class="ffd">.</span>125</span>。</div><div class="t m0 x4 h4 y2e0 ff5 fs3 fc0 sc0 ls0 ws0">計算機通常將數值用二進位制的方式儲存,因而每一個數字又被表示成<span class="_ _9"> </span><span class="ff15">2<span class="_ _3"> </span></span>的不同冪次的和:</div><div class="t m0 x17 h7 y2e1 ff15 fs3 fc0 sc0 ls0 ws0">1</div><div class="t m0 x17 h7 y2e2 ff15 fs3 fc0 sc0 ls0 ws0">8</div><div class="t m0 x4b h7 y2e3 ff15 fs3 fc0 sc0 ls0 ws0">=<span class="_ _36"> </span>0<span class="_ _60"> </span><span class="ff16">·<span class="_ _60"> </span></span>2</div><div class="t m0 x4c h1e y2e4 ff18 fs4 fc0 sc0 ls0 ws0">−<span class="ff17">1</span></div><div class="t m0 x4d h7 y2e3 ff15 fs3 fc0 sc0 ls0 ws0">+<span class="_ _60"> </span>0<span class="_ _60"> </span><span class="ff16">·<span class="_ _60"> </span></span>2</div><div class="t m0 x1e h1e y2e4 ff18 fs4 fc0 sc0 ls0 ws0">−<span class="ff17">2</span></div><div class="t m0 x4e h7 y2e3 ff15 fs3 fc0 sc0 ls0 ws0">+<span class="_ _60"> </span>1<span class="_ _60"> </span><span class="ff16">·<span class="_ _60"> </span></span>2</div><div class="t m0 x4f h1e y2e4 ff18 fs4 fc0 sc0 ls0 ws0">−<span class="ff17">3</span></div><div class="t m0 x5 h4 y2e5 ff5 fs3 fc0 sc0 ls0 ws0">在二進制中,我們把它寫作<span class="_ _3"> </span><span class="ff15">0<span class="ffd">.</span>001</span></div><div class="t m0 x50 h1e y2e6 ff17 fs4 fc0 sc0 ls0 ws0">2</div><div class="t m0 x4b h4 y2e5 ff5 fs3 fc0 sc0 ls0 ws0">,其中</div><div class="t m0 x51 h1e y2e6 ff17 fs4 fc0 sc0 ls0 ws0">2</div><div class="t m0 x4d h4 y2e5 ff5 fs3 fc0 sc0 ls0 ws0">的下標作為二進位制的標識。<span class="ff15">0<span class="ffd">.</span>125<span class="_ _9"> </span>=<span class="_ _9"> </span>0<span class="ffd">.</span>001</span></div><div class="t m0 x52 h1e y2e6 ff17 fs4 fc0 sc0 ls0 ws0">2</div><div class="t m0 x53 h4 y2e5 ff5 fs3 fc0 sc0 ls0 ws0">這一等</div><div class="t m0 x5 h4 y2e7 ff5 fs3 fc0 sc0 ls0 ws0">式通常意味著數字可以同時在二進位制和十進位制下被表示成有限位的數字。</div><div class="t m0 x4 h4 y2e8 ff5 fs3 fc0 sc0 ls0 ws0">在熟悉的十進位制中,你可能會遇到數字無法被表達成有限數字的形式。比如,<span class="ff15">1<span class="_ _3"> </span></span>除以<span class="_ _3"> </span><span class="ff15">3<span class="_ _3"> </span></span>的</div><div class="t m0 x5 h4 y2e9 ff5 fs3 fc0 sc0 ls0 ws0">結果在標準十進位制表示中為:</div><div class="t m0 x54 h7 y2ea ff15 fs3 fc0 sc0 ls0 ws0">1</div><div class="t m0 x54 h7 y2eb ff15 fs3 fc0 sc0 ls0 ws0">3</div><div class="t m0 x55 h7 y2ec ff15 fs3 fc0 sc0 ls0 ws0">=<span class="_ _36"> </span>0<span class="ffd">.</span>333333333<span class="_ _4b"> </span><span class="ff16">·<span class="_ _4b"> </span>·<span class="_ _4b"> </span>·</span></div><div class="t m0 x5 h4 y2ed ff5 fs3 fc0 sc0 ls0 ws0">小數末尾的<span class="_ _9"> </span><span class="ff15">3<span class="_ _3"> </span></span>將一直迴圈下去:也就是說,這個除法的商需要用無限的數字來表示!</div><div class="t m0 x4 h4 y2ee ff5 fs3 fc0 sc0 ls0 ws0">類似地,同樣存在數字在二進位制下需要用無限的數字來表示。比如:</div><div class="t m0 x56 h7 y2ef ff15 fs3 fc0 sc0 ls0 ws0">1</div><div class="t m0 x57 h7 y2f0 ff15 fs3 fc0 sc0 ls0 ws0">10</div><div class="t m0 x4b h7 y2f1 ff15 fs3 fc0 sc0 ls0 ws0">=<span class="_ _36"> </span>0<span class="ffd">.</span>00011001100110011<span class="_ _4b"> </span><span class="ff16">·<span class="_ _4b"> </span>·<span class="_ _4b"> </span>·</span></div><div class="t m0 x58 h1e y2f2 ff17 fs4 fc0 sc0 ls0 ws0">2</div><div class="t m0 x5 h4 y176 ff5 fs3 fc0 sc0 ls0 ws0">就像小數<span class="_ _3"> </span><span class="ff15">1/3<span class="_ _a"> </span></span>需要無限的數字才能完全正確地表示出來,<span class="ff15">1/10<span class="_ _a"> </span></span>在二進制下也需要無限的數字</div><div class="t m0 x5 h4 y98 ff5 fs3 fc0 sc0 ls0 ws0">來表示。<span class="_ _2"></span><span class="ff7">Python<span class="_ _9"> </span><span class="ff5">在內部截斷了這些小數位數,<span class="_ _8"></span>在大多數系統中通常保留為從第一個不為零的數</span></span></div><div class="t m0 x5 h4 y99 ff5 fs3 fc0 sc0 ls0 ws0">字開始至多<span class="_ _9"> </span><span class="ff15">52<span class="_ _3"> </span></span>位的數字。</div><div class="t m0 x4 h4 y9a ff5 fs3 fc0 sc0 ls0 ws0">這個由於精度引發的錯誤是使用浮點數時不可避免的。最好的解決辦法是作為程式員,始</div><div class="t m0 x5 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">終意識到浮點計算的表達是並不精確的,同時<span class="ff2">絕不</span>依賴完全相等來比較浮點數的值。</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf25" class="pf w0 h0" data-page-no="25"><div class="pc pc25 w0 h0"><img class="bi x5 y2f3 w1 h26" alt="" src="images/bg25.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">30<span class="_ _68"> </span><span class="ffc">6.<span class="_ _16"> </span><span class="ff6">內建型別:簡單變數型別</span></span></div><div class="t m0 x5 h8 ybb ff1 fs1 fc0 sc0 ls0 ws0">6.3<span class="_ _39"> </span><span class="ff2">複數</span></div><div class="t m0 x4 h4 y2f4 ff6 fs3 fc0 sc0 ls0 ws0">復數<span class="ff5">(<span class="ff7">complex<span class="_ _3"> </span>num<span class="_ _8"></span>ber<span class="ff5">)是有著<span class="ff6">實部</span>(</span>real<span class="_ _a"> </span>part<span class="ff5">)和<span class="ff6">虛部</span>(</span>imaginary<span class="_ _3"> </span>part<span class="ff5">)<span class="_ _38"></span>(浮點表示)</span></span></span></div><div class="t m0 x5 h4 y2f5 ff5 fs3 fc0 sc0 ls0 ws0">的數字。我們之前已經介紹過了整數和實數,我們可以用它們來建立一個複數:</div><div class="t m0 x11 hc y2f6 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[10]:<span class="_ _2a"> </span><span class="fc4">complex<span class="fc0">(<span class="fc6">1</span>,<span class="_ _2a"> </span><span class="fc6">2</span>)</span></span></div><div class="t m0 x11 hc y2f7 ffa fs3 fcf sc0 ls0 ws0">Out[10]:<span class="_ _2a"> </span><span class="fc0">(1+2j)</span></div><div class="t m0 x4 h4 y2f8 ff5 fs3 fc0 sc0 ls0 ws0">我們也可以選擇用新增字尾<span class="_ _9"> </span><span class="ffa">j<span class="_ _3"> </span></span>來表示複數的虛部:</div><div class="t m0 x11 hc y2f9 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[11]:<span class="_ _2a"> </span><span class="fc6">1<span class="_ _2a"> </span>+<span class="_ _2a"> </span>2<span class="fc0">j</span></span></div><div class="t m0 x11 hc y2fa ffa fs3 fcf sc0 ls0 ws0">Out[11]:<span class="_ _2a"> </span><span class="fc0">(1+2j)</span></div><div class="t m0 x4 h4 y2fb ff5 fs3 fc0 sc0 ls0 ws0">複數有著許多有趣的屬性和方法,我們將在這裡進行簡單的介紹:</div><div class="t m0 x11 hc y2fc ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[12]:<span class="_ _2a"> </span><span class="fc0">c<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>3<span class="_ _2a"> </span>+<span class="_ _2a"> </span>4</span>j</span></div><div class="t m0 x11 h6 y2fd ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[13]:<span class="_ _2a"> </span><span class="fc0">c<span class="fc6">.</span>real<span class="_ _27"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">實部</span></span></span></div><div class="t m0 x11 hc y2fe ffa fs3 fcf sc0 ls0 ws0">Out[13]:<span class="_ _2a"> </span><span class="fc0">3.0</span></div><div class="t m0 x11 h6 y2ff ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[14]:<span class="_ _2a"> </span><span class="fc0">c<span class="fc6">.</span>imag<span class="_ _27"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">虛部</span></span></span></div><div class="t m0 x11 hc y300 ffa fs3 fcf sc0 ls0 ws0">Out[14]:<span class="_ _2a"> </span><span class="fc0">4.0</span></div><div class="t m0 x11 h6 y301 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[15]:<span class="_ _2a"> </span><span class="fc0">c<span class="fc6">.</span>conjugate()<span class="_ _27"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">共軛複數</span></span></span></div><div class="t m0 x11 hc y302 ffa fs3 fcf sc0 ls0 ws0">Out[15]:<span class="_ _2a"> </span><span class="fc0">(3-4j)</span></div><div class="t m0 x11 h6 y303 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[16]:<span class="_ _2a"> </span><span class="fc4">abs<span class="fc0">(c)<span class="_ _27"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">模,即<span class="_ _2a"> </span></span>sqrt(c.real<span class="_ _2a"> </span>**<span class="_ _2a"> </span>2<span class="_ _2a"> </span>+<span class="_ _2a"> </span>c.imag<span class="_ _2a"> </span>**<span class="_ _2a"> </span>2)</span></span></span></div><div class="t m0 x11 hc y304 ffa fs3 fcf sc0 ls0 ws0">Out[16]:<span class="_ _2a"> </span><span class="fc0">5.0</span></div><div class="t m0 x5 h8 y305 ff1 fs1 fc0 sc0 ls0 ws0">6.4<span class="_ _39"> </span><span class="ff2">字串型別</span></div><div class="t m0 x4 h4 y306 ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _9"> </span><span class="ff5">中用單引號或雙引號建立字串:</span></div><div class="t m0 x11 hc y307 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[17]:<span class="_ _2a"> </span><span class="fc0">message<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="fcc">"what<span class="_ _2a"> </span>do<span class="_ _2a"> </span>you<span class="_ _2a"> </span>like?"</span></span></span></div><div class="t m0 x31 hc y308 ffa fs3 fc0 sc0 ls0 ws0">response<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">spam</span>'</span></span></div><div class="t m0 x4 h4 y40 ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _9"> </span><span class="ff5">有著許多方便的字串處理函式;下面是其中一些的介紹:</span></div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf26" class="pf w0 h0" data-page-no="26"><div class="pc pc26 w0 h0"><img class="bi x5 y309 w1 h27" alt="" src="images/bg26.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">6.5.<span class="_ _16"> </span><span class="ff19">NONE<span class="_ _9"> </span><span class="ff6">型別<span class="_ _6a"> </span><span class="ff7">31</span></span></span></div><div class="t m0 x11 h6 yfa ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[18]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">字串長度</span></span></div><div class="t m0 x31 hc yfb ffa fs3 fc4 sc0 ls0 ws0">len<span class="fc0">(response)</span></div><div class="t m0 x11 hc y21b ffa fs3 fcf sc0 ls0 ws0">Out[18]:<span class="_ _2a"> </span><span class="fc0">4</span></div><div class="t m0 x11 h6 yfe ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[19]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">大寫轉換。同樣還有小寫轉換:</span>str.lower()</span></div><div class="t m0 x31 hc yff ffa fs3 fc0 sc0 ls0 ws0">response<span class="fc6">.</span>upper()</div><div class="t m0 x11 hc y101 ffa fs3 fcf sc0 ls0 ws0">Out[19]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">SPAM</span>'</span></div><div class="t m0 x11 h6 y102 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[20]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">首字母大寫。</span>str.title()<span class="_ _2a"> </span><span class="ff6">有同樣的功能</span></span></div><div class="t m0 x31 hc y30a ffa fs3 fc0 sc0 ls0 ws0">message<span class="fc6">.</span>capitalize()</div><div class="t m0 x11 hc y30b ffa fs3 fcf sc0 ls0 ws0">Out[20]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">What<span class="_ _2a"> </span>do<span class="_ _2a"> </span>you<span class="_ _2a"> </span>like?</span>'</span></div><div class="t m0 x11 h6 y30c ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[21]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">用<span class="_ _2a"> </span></span>+<span class="_ _2a"> </span><span class="ff6">串接</span></span></div><div class="t m0 x31 hc y30d ffa fs3 fc0 sc0 ls0 ws0">message<span class="_ _2a"> </span><span class="fc6">+<span class="_ _2a"> </span></span>response</div><div class="t m0 x11 hc y30e ffa fs3 fcf sc0 ls0 ws0">Out[21]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">what<span class="_ _2a"> </span>do<span class="_ _2a"> </span>you<span class="_ _2a"> </span>like?spam</span>'</span></div><div class="t m0 x11 h6 y30f ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[22]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">乘法意味著自身多次串接</span></span></div><div class="t m0 x31 hc y310 ffa fs3 fc6 sc0 ls0 ws0">5<span class="_ _2a"> </span>*<span class="_ _2a"> </span><span class="fc0">response</span></div><div class="t m0 x11 hc y311 ffa fs3 fcf sc0 ls0 ws0">Out[22]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">spamspamspamspamspam</span>'</span></div><div class="t m0 x11 h6 y312 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[23]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">任意訪問單個字元(下標從<span class="_ _2a"> </span></span>0<span class="_ _2a"> </span><span class="ff6">開始)</span></span></div><div class="t m0 x31 hc y313 ffa fs3 fc0 sc0 ls0 ws0">message[<span class="fc6">0</span>]</div><div class="t m0 x11 hc y314 ffa fs3 fcf sc0 ls0 ws0">Out[23]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">w</span>'</span></div><div class="t m0 x4 h4 y315 ff5 fs3 fc0 sc0 ls0 ws0">更多關於下標的討論,詳見後續章節:第<span class="_ _9"> </span><span class="ff7 fc1">7.1<span class="_ _3"> </span></span>節<span class="fc1">列表</span>。</div><div class="t m0 x5 h8 y316 ff1 fs1 fc0 sc0 ls0 ws0">6.5<span class="_ _39"> </span><span class="ff11">None<span class="_ _10"> </span><span class="ff2">型別</span></span></div><div class="t m0 x4 h4 y317 ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _9"> </span><span class="ff5">包括一個特殊的型別:<span class="ffa">NoneType</span>。它只有一個值:<span class="ffa">None</span>。例如:</span></div><div class="t m0 x11 hc y318 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[24]:<span class="_ _2a"> </span><span class="fc4">type<span class="fc0">(</span><span class="ff11">None</span><span class="fc0">)</span></span></div><div class="t m0 x11 hc y319 ffa fs3 fcf sc0 ls0 ws0">Out[24]:<span class="_ _2a"> </span><span class="fc0">NoneType</span></div><div class="t m0 x4 h4 y99 ff5 fs3 fc0 sc0 ls0 ws0">你會發現<span class="_ _a"> </span><span class="ffa">None<span class="_ _13"> </span></span>被使用在很多地方,不過最常見的還是被用作一個函數返回值的<span class="ff6">預設值</span></div><div class="t m0 x30 h4 y9a ff5 fs3 fc0 sc0 ls0 ws0">(<span class="ff7">default<span class="_ _9"> </span>v<span class="_ _8"></span>alue<span class="ff5">)<span class="_ _33"></span>。例如,<span class="ff7">Python<span class="_ _9"> </span>3<span class="_ _3"> </span></span>中的<span class="_ _9"> </span><span class="ffa">print()<span class="_ _3"> </span></span>函式不會回傳任何值,但我們依然可以得到它</span></span></div><div class="t m0 x5 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">的值:</div><a class="l" href="#pf2a" data-dest-detail='[42,"XYZ",72,435.51,null]'><div class="d m1" style="border-style:none;position:absolute;left:293.818000px;bottom:257.544000px;width:13.939000px;height:6.869000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf2a" data-dest-detail='[42,"XYZ",72,435.51,null]'><div class="d m1" style="border-style:none;position:absolute;left:322.303000px;bottom:254.433000px;width:21.818000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf27" class="pf w0 h0" data-page-no="27"><div class="pc pc27 w0 h0"><img class="bi x5 y40 w1 h11" alt="" src="images/bg27.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">32<span class="_ _68"> </span><span class="ffc">6.<span class="_ _16"> </span><span class="ff6">內建型別:簡單變數型別</span></span></div><div class="t m0 x11 hc yfa ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[25]:<span class="_ _2a"> </span><span class="fc0">return_value<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="fc4">print</span></span>(<span class="ff12 fcc">'<span class="ffa">abc</span>'</span>)</span></div><div class="t m0 x11 hc yfc ffa fs3 fc0 sc0 ls0 ws0">abc</div><div class="t m0 x11 hc yfd ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[26]:<span class="_ _2a"> </span><span class="fc4">print<span class="fc0">(return_value)</span></span></div><div class="t m0 x11 hc yff ffa fs3 fc0 sc0 ls0 ws0">None</div><div class="t m0 x4 h4 y31a ff5 fs3 fc0 sc0 ls0 ws0">同樣地,<span class="ff7">Python<span class="_ _9"> </span></span>中所有沒有回傳值的函式都會事實上回傳<span class="_ _3"> </span><span class="ffa">None</span>。</div><div class="t m0 x5 h8 y31b ff1 fs1 fc0 sc0 ls0 ws0">6.6<span class="_ _39"> </span><span class="ff2">布林型別</span></div><div class="t m0 x4 h4 y31c ff5 fs3 fc0 sc0 ls0 ws0">布林類型是一種簡單的型別,它只擁有<span class="_ _3"> </span><span class="ffa">True<span class="_ _9"> </span></span>和<span class="_ _3"> </span><span class="ffa">False<span class="_ _3"> </span></span>兩種值,它由之前提到的比較運算</div><div class="t m0 x5 h4 y31d ff5 fs3 fc0 sc0 ls0 ws0">符得到:</div><div class="t m0 x11 hc y31e ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[27]:<span class="_ _2a"> </span><span class="fc0">result<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>(<span class="fc6">4<span class="_ _2a"> </span><<span class="_ _2a"> </span>5</span>)</span></div><div class="t m0 x31 hc y31f ffa fs3 fc0 sc0 ls0 ws0">result</div><div class="t m0 x11 hc y320 ffa fs3 fcf sc0 ls0 ws0">Out[27]:<span class="_ _2a"> </span><span class="fc0">True</span></div><div class="t m0 x11 hc y321 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[28]:<span class="_ _2a"> </span><span class="fc4">type<span class="fc0">(result)</span></span></div><div class="t m0 x11 hc y322 ffa fs3 fcf sc0 ls0 ws0">Out[28]:<span class="_ _2a"> </span><span class="fc0">bool</span></div><div class="t m0 x4 h4 y323 ff5 fs3 fc0 sc0 ls0 ws0">注意布林型別是區分大小寫的:<span class="_ _40"></span>不像其它語言,<span class="_ _40"></span><span class="ff7">Python<span class="_ _9"> </span><span class="ff5">中<span class="_ _34"> </span><span class="ffa">True<span class="_ _34"> </span></span>和<span class="_ _34"> </span><span class="ffa">False<span class="_ _34"> </span></span>必須<span class="ff2">首字母大寫</span>!</span></span></div><div class="t m0 x11 hc y324 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[29]:<span class="_ _2a"> </span><span class="fc4">print<span class="fc0">(</span><span class="ff11">True</span><span class="fc0">,<span class="_ _2a"> </span></span><span class="ff11">False</span><span class="fc0">)</span></span></div><div class="t m0 x11 hc y325 ffa fs3 fc0 sc0 ls0 ws0">True<span class="_ _2a"> </span>False</div><div class="t m0 x4 h4 y326 ff5 fs3 fc0 sc0 ls0 ws0">布林類型變數同樣可以用<span class="_ _3"> </span><span class="ffa">bool()<span class="_ _3"> </span></span>來結構:其它類型的變數可以通過預設的規則進行<span class="ff6">類型</span></div><div class="t m0 x5 h4 y327 ff6 fs3 fc0 sc0 ls0 ws0">轉換<span class="ff5">(<span class="ff7">t<span class="_ _4"></span>ype<span class="_ _9"> </span>conv<span class="_ _8"></span>ersion<span class="ff5">)<span class="_ _33"></span>。例如,所有的數字型別將<span class="_ _3"> </span><span class="ff15">0<span class="_ _9"> </span></span>視作<span class="_ _9"> </span><span class="ffa">False</span>,其餘則為<span class="_ _3"> </span><span class="ffa">True</span>:</span></span></span></div><div class="t m0 x11 hc y328 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[30]:<span class="_ _2a"> </span><span class="fc4">bool<span class="fc0">(<span class="fc6">2014</span>)</span></span></div><div class="t m0 x11 hc y329 ffa fs3 fcf sc0 ls0 ws0">Out[30]:<span class="_ _2a"> </span><span class="fc0">True</span></div><div class="t m0 x11 hc y117 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[31]:<span class="_ _2a"> </span><span class="fc4">bool<span class="fc0">(<span class="fc6">0</span>)</span></span></div><div class="t m0 x11 hc y119 ffa fs3 fcf sc0 ls0 ws0">Out[31]:<span class="_ _2a"> </span><span class="fc0">False</span></div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf28" class="pf w0 h0" data-page-no="28"><div class="pc pc28 w0 h0"><img class="bi x5 y32a w1 h28" alt="" src="images/bg28.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">6.6.<span class="_ _16"> </span><span class="ff6">布林型別<span class="_ _6b"> </span><span class="ff7">33</span></span></div><div class="t m0 x11 hc yfa ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[32]:<span class="_ _2a"> </span><span class="fc4">bool<span class="fc0">(<span class="fc6">3.1415</span>)</span></span></div><div class="t m0 x11 hc yfc ffa fs3 fcf sc0 ls0 ws0">Out[32]:<span class="_ _2a"> </span><span class="fc0">True</span></div><div class="t m0 x4 h4 y236 ffa fs3 fc0 sc0 ls0 ws0">None<span class="_ _9"> </span><span class="ff5">的布林值永遠是<span class="_ _3"> </span></span>False<span class="ff5">:</span></div><div class="t m0 x11 hc y32b ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[33]:<span class="_ _2a"> </span><span class="fc4">bool<span class="fc0">(</span><span class="ff11">None</span><span class="fc0">)</span></span></div><div class="t m0 x11 hc y32c ffa fs3 fcf sc0 ls0 ws0">Out[33]:<span class="_ _2a"> </span><span class="fc0">False</span></div><div class="t m0 x4 h4 y32d ff5 fs3 fc0 sc0 ls0 ws0">對字串來說,如果字串為空串,則<span class="_ _9"> </span><span class="ffa">bool(s)<span class="_ _3"> </span></span>為假,否則為真:</div><div class="t m0 x11 hc y32e ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[34]:<span class="_ _2a"> </span><span class="fc4">bool<span class="fc0">(<span class="fcc">""</span>)</span></span></div><div class="t m0 x11 hc y32f ffa fs3 fcf sc0 ls0 ws0">Out[34]:<span class="_ _2a"> </span><span class="fc0">False</span></div><div class="t m0 x11 hc y330 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[35]:<span class="_ _2a"> </span><span class="fc4">bool<span class="fc0">(<span class="fcc">"abc"</span>)</span></span></div><div class="t m0 x11 hc y331 ffa fs3 fcf sc0 ls0 ws0">Out[35]:<span class="_ _2a"> </span><span class="fc0">True</span></div><div class="t m0 x4 h4 y332 ff5 fs3 fc0 sc0 ls0 ws0">對序列而言,我們會在下一節看到,空序列的布林值為</div><div class="t m0 x59 hc y333 ffa fs3 fc0 sc0 ls0 ws0">False</div><div class="t m0 x5a h4 y332 ff5 fs3 fc0 sc0 ls0 ws0">,其餘則為</div><div class="t m0 x5b hc y333 ffa fs3 fc0 sc0 ls0 ws0">True</div><div class="t m0 x5c h4 y332 ff5 fs3 fc0 sc0 ls0 ws0">:</div><div class="t m0 x11 hc y334 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[36]:<span class="_ _2a"> </span><span class="fc4">bool<span class="fc0">([<span class="fc6">1</span>,<span class="_ _2a"> </span><span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">3</span>])</span></span></div><div class="t m0 x11 hc y335 ffa fs3 fcf sc0 ls0 ws0">Out[36]:<span class="_ _2a"> </span><span class="fc0">True</span></div><div class="t m0 x11 hc y336 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[37]:<span class="_ _2a"> </span><span class="fc4">bool<span class="fc0">([])</span></span></div><div class="t m0 x11 hc y337 ffa fs3 fcf sc0 ls0 ws0">Out[37]:<span class="_ _2a"> </span><span class="fc0">False</span></div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf29" class="pf w0 h0" data-page-no="29"><div class="pc pc29 w0 h0"></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf2a" class="pf w0 h0" data-page-no="2a"><div class="pc pc2a w0 h0"><img class="bi x5 y1bd w1 h29" alt="" src="images/bg2a.png"/><div class="t m0 xc h1 y3 ff2 fs0 fc0 sc0 ls0 ws0">第<span class="_ _0"> </span><span class="ff1">7<span class="_ _0"> </span></span>章<span class="_ _32"> </span>內建資料結構</div><div class="t m0 x4 h4 y204 ff5 fs3 fc0 sc0 ls0 ws0">我們已經了解了<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>的一些基本資料類型:<span class="ffa">int</span>、<span class="ffa">float</span>、<span class="ffa">complex</span>、<span class="ffa">bool</span>、<span class="ffa">str<span class="_ _3"> </span></span>等等。</div><div class="t m0 x5 h4 y205 ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _9"> </span><span class="ff5">同樣也擁有一些內建的複合型別作為其它型別的容器。這些複合型別有:</span></div><div class="t m0 x5d h5 y338 ff2 fs3 fc0 sc0 ls0 ws0">型別名<span class="_ _5c"> </span>舉例<span class="_ _6c"> </span>描述</div><div class="t m0 x5d h4 y339 ffa fs3 fc0 sc0 ls0 ws0">list<span class="_ _6d"> </span>[1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>3]<span class="_ _6e"> </span><span class="ff5">列表,有序集合</span></div><div class="t m0 x5d h4 y33a ffa fs3 fc0 sc0 ls0 ws0">tuple<span class="_ _11"> </span>(1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>3)<span class="_ _6e"> </span><span class="ff5">Tuple,不可變的有序集合</span></div><div class="t m0 x5d h4 y33b ffa fs3 fc0 sc0 ls0 ws0">dict<span class="_ _6d"> </span>{<span class="ff12">'</span>a<span class="ff12">'</span>:1,<span class="_ _2a"> </span><span class="ff12">'</span>b<span class="ff12">'</span>:2,<span class="_ _2a"> </span><span class="ff12">'</span>c<span class="ff12">'</span>:3}<span class="_ _5c"> </span><span class="ff5">字典,無序的鍵值對對映</span></div><div class="t m0 x5d h4 y33c ffa fs3 fc0 sc0 ls0 ws0">set<span class="_ _6f"> </span>{1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>3}<span class="_ _6e"> </span><span class="ff5">集合,具有無序性和唯一性</span></div><div class="t m0 x4 h4 y33d ff5 fs3 fc0 sc0 ls0 ws0">正如你所見,圓括號、方括號和大括號對於這些集合類型有著不同的意義。接下來,我們</div><div class="t m0 x5 h4 y33e ff5 fs3 fc0 sc0 ls0 ws0">將在這裡快速認識這些資料結構。</div><div class="t m0 x5 h8 y33f ff1 fs1 fc0 sc0 ls0 ws0">7.1<span class="_ _39"> </span><span class="ff2">列表</span></div><div class="t m0 x4 h4 y340 ff6 fs3 fc0 sc0 ls0 ws0">列表<span class="ff5">(<span class="ff7">list</span>)是<span class="_ _3"> </span><span class="ff7">Python<span class="_ _9"> </span></span>中基本的<span class="ff2">有序</span>且<span class="ff2">可變</span>的資料集合型別。列表用方括號表示,元素之</span></div><div class="t m0 x5 h4 y341 ff5 fs3 fc0 sc0 ls0 ws0">間用逗號隔開。下面是一個包括前幾項質數的列表:</div><div class="t m0 x11 hc y342 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[1]:<span class="_ _2a"> </span><span class="fc0">L<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[<span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">3</span>,<span class="_ _2a"> </span><span class="fc6">5</span>,<span class="_ _2a"> </span><span class="fc6">7</span>]</span></div><div class="t m0 x4 h4 y343 ff5 fs3 fc0 sc0 ls0 ws0">列表有著許多方便好用的方法。這裡我們將快速介紹那些最普遍和最常用的方法:</div><div class="t m0 x11 h6 y344 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[2]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">列表長度</span></span></div><div class="t m0 x24 hc y345 ffa fs3 fc4 sc0 ls0 ws0">len<span class="fc0">(L)</span></div><div class="t m0 x11 hc y346 ffa fs3 fcf sc0 ls0 ws0">Out[2]:<span class="_ _2a"> </span><span class="fc0">4</span></div><div class="t m0 x11 h6 y172 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[3]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">在列表尾部新增一個元素</span></span></div><div class="t m0 x24 hc y173 ffa fs3 fc0 sc0 ls0 ws0">L<span class="fc6">.</span>append(<span class="fc6">11</span>)</div><div class="t m0 x24 hc y174 ffa fs3 fc0 sc0 ls0 ws0">L</div><div class="t m0 x11 hc y176 ffa fs3 fcf sc0 ls0 ws0">Out[3]:<span class="_ _2a"> </span><span class="fc0">[2,<span class="_ _2a"> </span>3,<span class="_ _2a"> </span>5,<span class="_ _2a"> </span>7,<span class="_ _2a"> </span>11]</span></div><div class="t m0 x11 h6 y99 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[4]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">用<span class="_ _2a"> </span></span>+<span class="_ _2a"> </span><span class="ff6">串接</span></span></div><div class="t m0 x24 hc y9a ffa fs3 fc0 sc0 ls0 ws0">L<span class="_ _2a"> </span><span class="fc6">+<span class="_ _2a"> </span></span>[<span class="fc6">13</span>,<span class="_ _2a"> </span><span class="fc6">17</span>,<span class="_ _2a"> </span><span class="fc6">19</span>]</div><div class="t m0 x2f h7 y20 ff7 fs3 fc0 sc0 ls0 ws0">35</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf2b" class="pf w0 h0" data-page-no="2b"><div class="pc pc2b w0 h0"><img class="bi x5 y347 w1 h2a" alt="" src="images/bg2b.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">36<span class="_ _70"> </span><span class="ffc">7.<span class="_ _16"> </span><span class="ff6">內建資料結構</span></span></div><div class="t m0 x11 hc y1d6 ffa fs3 fcf sc0 ls0 ws0">Out[4]:<span class="_ _2a"> </span><span class="fc0">[2,<span class="_ _2a"> </span>3,<span class="_ _2a"> </span>5,<span class="_ _2a"> </span>7,<span class="_ _2a"> </span>11,<span class="_ _2a"> </span>13,<span class="_ _2a"> </span>17,<span class="_ _2a"> </span>19]</span></div><div class="t m0 x11 h6 y348 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[5]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">進行原地排序</span></span></div><div class="t m0 x24 hc y349 ffa fs3 fc0 sc0 ls0 ws0">L<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[<span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">5</span>,<span class="_ _2a"> </span><span class="fc6">1</span>,<span class="_ _2a"> </span><span class="fc6">6</span>,<span class="_ _2a"> </span><span class="fc6">3</span>,<span class="_ _2a"> </span><span class="fc6">4</span>]</div><div class="t m0 x24 hc y34a ffa fs3 fc0 sc0 ls0 ws0">L<span class="fc6">.</span>sort()</div><div class="t m0 x24 hc y34b ffa fs3 fc0 sc0 ls0 ws0">L</div><div class="t m0 x11 hc y34c ffa fs3 fcf sc0 ls0 ws0">Out[5]:<span class="_ _2a"> </span><span class="fc0">[1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>3,<span class="_ _2a"> </span>4,<span class="_ _2a"> </span>5,<span class="_ _2a"> </span>6]</span></div><div class="t m0 x4 h4 y34d ff5 fs3 fc0 sc0 ls0 ws0">除此之外,還有許多內建的列表方法,它們在<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>的官方文件中有詳細介紹:<span class="ffa fc2">https:</span></div><div class="t m0 x5 h4 y34e ffa fs3 fc2 sc0 ls0 ws0">//docs.python.org/3/tutorial/datastructures.html<span class="ff5 fc0">。</span></div><div class="t m0 x4 h4 y34f ff5 fs3 fc0 sc0 ls0 ws0">我們已經演示了列表包含單一類型元素的使用方法,然而<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>複合類型的強大之處在</div><div class="t m0 x5 h4 y350 ff5 fs3 fc0 sc0 ls0 ws0">於它可以包含<span class="ff2">任何</span>型別的物件,甚至是不同型別的混合,例如:</div><div class="t m0 x11 hc y351 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[6]:<span class="_ _2a"> </span><span class="fc0">L<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[<span class="fc6">1</span>,<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">two</span>'</span>,<span class="_ _2a"> </span><span class="fc6">3.14</span>,<span class="_ _2a"> </span>[<span class="fc6">0</span>,<span class="_ _2a"> </span><span class="fc6">3</span>,<span class="_ _2a"> </span><span class="fc6">5</span>]]</span></div><div class="t m0 x4 h4 y352 ff5 fs3 fc0 sc0 ls0 ws0">這種靈活性是<span class="_ _3"> </span><span class="ff7">Python<span class="_ _9"> </span></span>動態型別系統帶來的好處之一,而在<span class="_ _3"> </span><span class="ff7">C<span class="_ _9"> </span></span>這種靜態型別語言中建立這</div><div class="t m0 x5 h4 y353 ff5 fs3 fc0 sc0 ls0 ws0">樣一個複合型別的序列可以說是非常讓人頭疼了。<span class="_ _2"></span>然而,<span class="_ _8"></span><span class="ff7">Python<span class="_ _34"> </span><span class="ff5">的列表甚至還能將其它列表作</span></span></div><div class="t m0 x5 h4 y354 ff5 fs3 fc0 sc0 ls0 ws0">為其元素。正是這種變數型別的靈活性使得<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>的程式碼寫起來更為快速和簡單。</div><div class="t m0 x4 h4 y355 ff5 fs3 fc0 sc0 ls0 ws0">到現在為止,我們一直著眼於將列表作為一個整體來操作,而相對地,列表另外一個重要</div><div class="t m0 x5 h4 y356 ff5 fs3 fc0 sc0 ls0 ws0">的部分就是訪問其中的元素個體。這一部分通過索引以及切片來完成,我們將在下面討論它們。</div><div class="t m0 x5 ha y357 ff1 fs2 fc0 sc0 ls0 ws0">7.1.1<span class="_ _3c"> </span><span class="ff2">列表索引與切片</span></div><div class="t m0 x4 h4 y358 ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _3"> </span><span class="ff5">對於複合類別型變數的元素訪問提供了<span class="ff6">索引</span>(</span>index<span class="ff5">)和<span class="ff6">切片</span>(</span>slice<span class="ff5">)的方法,前者</span></div><div class="t m0 x5 h4 y359 ff5 fs3 fc0 sc0 ls0 ws0">用於訪問單個元素,後者用於多個元素。我們會看到,兩者都使用一個方括號的語法來表示。</div><div class="t m0 x5 h4 y35a ff5 fs3 fc0 sc0 ls0 ws0">讓我們回到之前的質數列表:</div><div class="t m0 x11 hc y35b ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[7]:<span class="_ _2a"> </span><span class="fc0">L<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[<span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">3</span>,<span class="_ _2a"> </span><span class="fc6">5</span>,<span class="_ _2a"> </span><span class="fc6">7</span>,<span class="_ _2a"> </span><span class="fc6">11</span>]</span></div><div class="t m0 x4 h4 y35c ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _3"> </span><span class="ff5">使用了<span class="ff2">從零開始</span>的索引,所以我們用下面的語法來取得列表中的第一個以及第二</span></div><div class="t m0 x5 h4 y35d ff5 fs3 fc0 sc0 ls0 ws0">個元素:</div><div class="t m0 x11 hc y35e ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[8]:<span class="_ _2a"> </span><span class="fc0">L[<span class="fc6">0</span>]</span></div><div class="t m0 x11 hc y35f ffa fs3 fcf sc0 ls0 ws0">Out[8]:<span class="_ _2a"> </span><span class="fc0">2</span></div><div class="t m0 x11 hc y360 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[9]:<span class="_ _2a"> </span><span class="fc0">L[<span class="fc6">1</span>]</span></div><div class="t m0 x11 hc y361 ffa fs3 fcf sc0 ls0 ws0">Out[9]:<span class="_ _2a"> </span><span class="fc0">3</span></div><div class="t m0 x4 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">位於列表末尾的元素可以用負數來訪問,<span class="ff16">−<span class="ff15">1<span class="_ _9"> </span></span></span>代表最後一個元素,依次類別推:</div><a class="l" href="https://docs.python.org/3/tutorial/datastructures.html"><div class="d m1" style="border-style:none;position:absolute;left:488.912000px;bottom:602.755000px;width:34.364000px;height:8.960000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="https://docs.python.org/3/tutorial/datastructures.html"><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:586.496000px;width:274.909000px;height:10.000000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf2c" class="pf w0 h0" data-page-no="2c"><div class="pc pc2c w0 h0"><img class="bi x5 y40 w1 h11" alt="" src="images/bg2c.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">7.1.<span class="_ _16"> </span><span class="ff6">列表<span class="_ _71"> </span><span class="ff7">37</span></span></div><div class="t m0 x11 hc yfa ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[10]:<span class="_ _2a"> </span><span class="fc0">L[<span class="fc6">-1</span>]</span></div><div class="t m0 x11 hc yfc ffa fs3 fcf sc0 ls0 ws0">Out[10]:<span class="_ _2a"> </span><span class="fc0">11</span></div><div class="t m0 x11 hc yfd ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[11]:<span class="_ _2a"> </span><span class="fc0">L[<span class="fc6">-2</span>]</span></div><div class="t m0 x11 hc yff ffa fs3 fcf sc0 ls0 ws0">Out[11]:<span class="_ _2a"> </span><span class="fc0">7</span></div><div class="t m0 x4 h4 y362 ff5 fs3 fc0 sc0 ls0 ws0">你可以用圖<span class="_ _a"> </span><span class="ff7 fc1">1<span class="_ _a"> </span></span>這種更直觀可視的方式來理解索引:列表中的元素用方框中字號更大的數</div><div class="t m0 x5 h4 y363 ff5 fs3 fc0 sc0 ls0 ws0">字表示;列表索引則用方框上下方較小的數字來表示。在這裡<span class="_ _a"> </span><span class="ffa">L[2]<span class="_ _3"> </span></span>返回數字<span class="_ _3"> </span><span class="ff15">5</span>,因為那是索</div><div class="t m0 x5 h4 y364 ff5 fs3 fc0 sc0 ls0 ws0">引<span class="_ _9"> </span><span class="ffa">2<span class="_ _3"> </span></span>下的數值。</div><div class="t m0 x5e h4 y365 ff5 fs3 fc0 sc0 ls0 ws0">圖<span class="_ _9"> </span><span class="ff7">1:<span class="_ _1"> </span></span>列表索引示意圖</div><div class="t m0 x4 h4 y366 ff5 fs3 fc0 sc0 ls0 ws0">索引用來取出列表中的單個元素,而切片則是取出在子列表中取出多個值的方法。它用一</div><div class="t m0 x5 h4 y367 ff5 fs3 fc0 sc0 ls0 ws0">個冒號來表示子序列的起點(包含)<span class="_ _8"></span>和終點(不包含)<span class="_ _33"></span>。<span class="_ _8"></span>舉個例子,<span class="_ _4"></span>為了得到列表中的前三個元</div><div class="t m0 x5 h4 y368 ff5 fs3 fc0 sc0 ls0 ws0">素,我們可以這樣做:</div><div class="t m0 x11 hc y369 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[12]:<span class="_ _2a"> </span><span class="fc0">L[<span class="fc6">0</span>:<span class="fc6">3</span>]</span></div><div class="t m0 x11 hc y36a ffa fs3 fcf sc0 ls0 ws0">Out[12]:<span class="_ _2a"> </span><span class="fc0">[2,<span class="_ _2a"> </span>3,<span class="_ _2a"> </span>5]</span></div><div class="t m0 x4 h4 y36b ff5 fs3 fc0 sc0 ls0 ws0">留意<span class="_ _9"> </span><span class="ffa">0<span class="_ _3"> </span></span>和<span class="_ _9"> </span><span class="ffa">3<span class="_ _9"> </span></span>在語法中的位置以及切片在兩個索引之間的取值方式。如果我們把第一個索引</div><div class="t m0 x5 h4 y36c ff5 fs3 fc0 sc0 ls0 ws0">值(也就是<span class="_ _9"> </span><span class="ffa">0</span>)省去,那麼我們也會得到同樣的結果:</div><div class="t m0 x11 hc y36d ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[13]:<span class="_ _2a"> </span><span class="fc0">L[:<span class="fc6">3</span>]</span></div><div class="t m0 x11 hc y36e ffa fs3 fcf sc0 ls0 ws0">Out[13]:<span class="_ _2a"> </span><span class="fc0">[2,<span class="_ _2a"> </span>3,<span class="_ _2a"> </span>5]</span></div><div class="t m0 x4 h4 y36f ff5 fs3 fc0 sc0 ls0 ws0">同樣地,如果我們省去後面的索引值,它的預設值為列表的長度。這樣一來,訪問列表最</div><div class="t m0 x5 h4 y370 ff5 fs3 fc0 sc0 ls0 ws0">後<span class="_ _9"> </span><span class="ff15">3<span class="_ _3"> </span></span>個元素可以用下面這種方式:</div><div class="t m0 x11 hc y117 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[14]:<span class="_ _2a"> </span><span class="fc0">L[<span class="fc6">-3</span>:]</span></div><div class="t m0 x11 hc y119 ffa fs3 fcf sc0 ls0 ws0">Out[14]:<span class="_ _2a"> </span><span class="fc0">[5,<span class="_ _2a"> </span>7,<span class="_ _2a"> </span>11]</span></div><a class="l" href="#pf2c" data-dest-detail='[44,"XYZ",72,577.07,null]'><div class="d m1" style="border-style:none;position:absolute;left:154.031000px;bottom:614.464000px;width:5.455000px;height:6.870000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf2d" class="pf w0 h0" data-page-no="2d"><div class="pc pc2d w0 h0"><img class="bi x5 y40 w1 h2b" alt="" src="images/bg2d.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">38<span class="_ _70"> </span><span class="ffc">7.<span class="_ _16"> </span><span class="ff6">內建資料結構</span></span></div><div class="t m0 x4 h4 y42 ff5 fs3 fc0 sc0 ls0 ws0">最後,我們可以用第三個整數來表示步長。例如,要在列表中每<span class="_ _3"> </span><span class="ff15">2<span class="_ _3"> </span></span>個元素選取一個,我們</div><div class="t m0 x5 h4 y77 ff5 fs3 fc0 sc0 ls0 ws0">可以這樣寫:</div><div class="t m0 x11 h6 y371 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[15]:<span class="_ _2a"> </span><span class="fc0">L[::<span class="fc6">2</span>]<span class="_ _27"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">等同於<span class="_ _2a"> </span></span>L[0:len(L):2]</span></span></div><div class="t m0 x11 hc y372 ffa fs3 fcf sc0 ls0 ws0">Out[15]:<span class="_ _2a"> </span><span class="fc0">[2,<span class="_ _2a"> </span>5,<span class="_ _2a"> </span>11]</span></div><div class="t m0 x4 h4 y373 ff5 fs3 fc0 sc0 ls0 ws0">另外一個特別有用的方法是定義一個負數的步長,它可以把序列翻轉:</div><div class="t m0 x11 hc y374 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[16]:<span class="_ _2a"> </span><span class="fc0">L[::<span class="fc6">-1</span>]</span></div><div class="t m0 x11 hc y375 ffa fs3 fcf sc0 ls0 ws0">Out[16]:<span class="_ _2a"> </span><span class="fc0">[11,<span class="_ _2a"> </span>7,<span class="_ _2a"> </span>5,<span class="_ _2a"> </span>3,<span class="_ _2a"> </span>2]</span></div><div class="t m0 x4 h4 y376 ff5 fs3 fc0 sc0 ls0 ws0">索引和切片都不僅可以被用來訪問元素,它們也可以改變元素。語法就和你所想象的一樣:</div><div class="t m0 x11 hc y377 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[17]:<span class="_ _2a"> </span><span class="fc0">L[<span class="fc6">0</span>]<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>100</span></span></div><div class="t m0 x31 hc y378 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(L)</span></div><div class="t m0 x11 hc y379 ffa fs3 fc0 sc0 ls0 ws0">[100,<span class="_ _2a"> </span>3,<span class="_ _2a"> </span>5,<span class="_ _2a"> </span>7,<span class="_ _2a"> </span>11]</div><div class="t m0 x11 hc y37a ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[18]:<span class="_ _2a"> </span><span class="fc0">L[<span class="fc6">1</span>:<span class="fc6">3</span>]<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[<span class="fc6">55</span>,<span class="_ _2a"> </span><span class="fc6">56</span>]</span></div><div class="t m0 x31 hc y37b ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(L)</span></div><div class="t m0 x11 hc y37c ffa fs3 fc0 sc0 ls0 ws0">[100,<span class="_ _2a"> </span>55,<span class="_ _2a"> </span>56,<span class="_ _2a"> </span>7,<span class="_ _2a"> </span>11]</div><div class="t m0 x4 h4 y37d ff5 fs3 fc0 sc0 ls0 ws0">切片的類別似用法也同樣<span class="_ _4a"></span>出現在許多其它的科學計算套件中,包<span class="_ _4a"></span>括<span class="_ _13"> </span><span class="ff7">NumPy<span class="_ _72"> </span></span>和<span class="_ _13"> </span><span class="ff7">Pandas</span>(在</div><div class="t m0 x5 h4 y37e ff5 fs3 fc0 sc0 ls0 ws0">第<span class="_ _9"> </span><span class="ff7 fc1">1<span class="_ _3"> </span></span>章<span class="_ _9"> </span><span class="ff7 fc1">Python<span class="_ _9"> </span><span class="ff5">介紹</span></span>中提到過)<span class="_ _33"></span>。</div><div class="t m0 x4 h4 y37f ff5 fs3 fc0 sc0 ls0 ws0">既然我們已經學習了<span class="_ _3"> </span><span class="ff7">Python<span class="_ _9"> </span></span>的列表,並且知道了如何在順序複合型別中訪問其中的元素,</div><div class="t m0 x5 h4 y380 ff5 fs3 fc0 sc0 ls0 ws0">那麼現在我們可以看看之前提到的其它三種標準複合資料型別。</div><div class="t m0 x5 h8 y381 ff1 fs1 fc0 sc0 ls0 ws0">7.2<span class="_ _39"> </span><span class="ff2">Tuple</span></div><div class="t m0 x4 h4 y382 ff6 fs3 fc0 sc0 ls0 ws0">Tuple<span class="ff5">(<span class="ff7">tuple</span>)在各種意義上都和列表非常相似,但是它們是用圓括號而不是方括號來定義</span></div><div class="t m0 x5 h4 y383 ff5 fs3 fc0 sc0 ls0 ws0">的:</div><div class="t m0 x11 hc y384 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[19]:<span class="_ _2a"> </span><span class="fc0">t<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>(<span class="fc6">1</span>,<span class="_ _2a"> </span><span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">3</span>)</span></div><div class="t m0 x4 h4 y385 ff5 fs3 fc0 sc0 ls0 ws0">它們也可以完全不用括號來進行定義:</div><div class="t m0 x11 hc y21a ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[20]:<span class="_ _2a"> </span><span class="fc0">t<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>1</span>,<span class="_ _2a"> </span><span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">3</span></span></div><div class="t m0 x31 hc y117 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(t)</span></div><div class="t m0 x11 hc y119 ffa fs3 fc0 sc0 ls0 ws0">(1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>3)</div><a class="l" href="#pf8" data-dest-detail='[8,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:86.545000px;bottom:336.330000px;width:5.455000px;height:6.870000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf8" data-dest-detail='[8,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:110.182000px;bottom:333.219000px;width:60.455000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf2e" class="pf w0 h0" data-page-no="2e"><div class="pc pc2e w0 h0"><img class="bi x5 y1bd w1 h2c" alt="" src="images/bg2e.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">7.2.<span class="_ _16"> </span><span class="ff6">Tuple<span class="_ _71"> </span><span class="ff7">39</span></span></div><div class="t m0 x4 h4 y42 ff5 fs3 fc0 sc0 ls0 ws0">與列表相似,Tuple也有長度,其單個元素也可以用方括號來提取:</div><div class="t m0 x11 hc y386 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[21]:<span class="_ _2a"> </span><span class="fc4">len<span class="fc0">(t)</span></span></div><div class="t m0 x11 hc y387 ffa fs3 fcf sc0 ls0 ws0">Out[21]:<span class="_ _2a"> </span><span class="fc0">3</span></div><div class="t m0 x11 hc y388 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[22]:<span class="_ _2a"> </span><span class="fc0">t[<span class="fc6">0</span>]</span></div><div class="t m0 x11 hc y389 ffa fs3 fcf sc0 ls0 ws0">Out[22]:<span class="_ _2a"> </span><span class="fc0">1</span></div><div class="t m0 x4 h4 y38a ff5 fs3 fc0 sc0 ls0 ws0">Tuple和列表最主要的區別還是在於Tuple是<span class="ff2">不可變</span>的,這意味著一旦它們被建立,它們的大</div><div class="t m0 x5 h4 y38b ff5 fs3 fc0 sc0 ls0 ws0">小和內容都不能被改變:</div><div class="t m0 x11 hc y38c ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[23]:<span class="_ _2a"> </span><span class="fc0">t[<span class="fc6">1</span>]<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>4</span></span></div><div class="t m0 x24 hc y38d ffa fs3 fc0 sc0 ls0 ws0">----------------------------------------------------------------------</div><div class="t m0 x24 hc y38e ffa fs3 fc0 sc0 ls0 ws0">TypeError<span class="_ _73"> </span>Traceback<span class="_ _2a"> </span>(most<span class="_ _2a"> </span>recent<span class="_ _2a"> </span>call<span class="_ _2a"> </span>last)</div><div class="t m0 x24 hc y38f ffa fs3 fc0 sc0 ls0 ws0"><ipython-input-23-141c76cb54a2><span class="_ _2a"> </span>in<span class="_ _2a"> </span><module>()</div><div class="t m0 x29 hc y390 ffa fs3 fc0 sc0 ls0 ws0">----><span class="_ _2a"> </span>1<span class="_ _2a"> </span>t[1]<span class="_ _2a"> </span>=<span class="_ _2a"> </span>4</div><div class="t m0 x24 hc y391 ffa fs3 fc0 sc0 ls0 ws0">TypeError:<span class="_ _2a"> </span><span class="ff12">'</span>tuple<span class="ff12">'<span class="_ _2a"> </span></span>object<span class="_ _2a"> </span>does<span class="_ _2a"> </span>not<span class="_ _2a"> </span>support<span class="_ _2a"> </span>item<span class="_ _2a"> </span>assignment</div><div class="t m0 x11 hc y392 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[24]:<span class="_ _2a"> </span><span class="fc0">t<span class="fc6">.</span>append(<span class="fc6">4</span>)</span></div><div class="t m0 x24 hc y393 ffa fs3 fc0 sc0 ls0 ws0">----------------------------------------------------------------------</div><div class="t m0 x24 hc y394 ffa fs3 fc0 sc0 ls0 ws0">AttributeError<span class="_ _74"> </span>Traceback<span class="_ _2a"> </span>(most<span class="_ _2a"> </span>recent<span class="_ _2a"> </span>call<span class="_ _2a"> </span>last)</div><div class="t m0 x24 hc y395 ffa fs3 fc0 sc0 ls0 ws0"><ipython-input-24-e8bd1632f9dd><span class="_ _2a"> </span>in<span class="_ _2a"> </span><module>()</div><div class="t m0 x29 hc y396 ffa fs3 fc0 sc0 ls0 ws0">----><span class="_ _2a"> </span>1<span class="_ _2a"> </span>t.append(4)</div><div class="t m0 x24 hc y397 ffa fs3 fc0 sc0 ls0 ws0">AttributeError:<span class="_ _2a"> </span><span class="ff12">'</span>tuple<span class="ff12">'<span class="_ _2a"> </span></span>object<span class="_ _2a"> </span>has<span class="_ _2a"> </span>no<span class="_ _2a"> </span>attribute<span class="_ _2a"> </span><span class="ff12">'</span>append<span class="ff12">'</span></div><div class="t m0 x4 h4 y398 ff5 fs3 fc0 sc0 ls0 ws0">Tuple經常被用在<span class="_ _3"> </span><span class="ff7">Python<span class="_ _9"> </span></span>的程式設計中;一個特別常見的例子就是函式返回多個值。舉個例子,</div><div class="t m0 x5 h4 y399 ff5 fs3 fc0 sc0 ls0 ws0">浮點物件的方法<span class="_ _3"> </span><span class="ffa">as_integer_ratio()<span class="_ _3"> </span></span>可以回傳浮點數對應的分子和分母,這個雙值就是以元</div><div class="t m0 x5 h4 y39a ff5 fs3 fc0 sc0 ls0 ws0">組的形式回傳的:</div><div class="t m0 x11 hc y9a ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[25]:<span class="_ _2a"> </span><span class="fc0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>0.125</span></span></div><div class="t m0 x31 hc y40 ffa fs3 fc0 sc0 ls0 ws0">x<span class="fc6">.</span>as_integer_ratio()</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf2f" class="pf w0 h0" data-page-no="2f"><div class="pc pc2f w0 h0"><img class="bi x5 y39b w1 h2d" alt="" src="images/bg2f.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">40<span class="_ _70"> </span><span class="ffc">7.<span class="_ _16"> </span><span class="ff6">內建資料結構</span></span></div><div class="t m0 x11 hc y1d7 ffa fs3 fcf sc0 ls0 ws0">Out[25]:<span class="_ _2a"> </span><span class="fc0">(1,<span class="_ _2a"> </span>8)</span></div><div class="t m0 x4 h4 y39c ff5 fs3 fc0 sc0 ls0 ws0">這些回傳的多個值也可以用下面的方法分別獨立賦值:</div><div class="t m0 x11 hc y39d ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[26]:<span class="_ _2a"> </span><span class="fc0">numerator,<span class="_ _2a"> </span>denominator<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>x<span class="fc6">.</span>as_integer_ratio()</span></div><div class="t m0 x31 hc y39e ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(numerator<span class="_ _2a"> </span><span class="fc6">/<span class="_ _2a"> </span></span>denominator)</span></div><div class="t m0 x11 hc y39f ffa fs3 fc0 sc0 ls0 ws0">0.125</div><div class="t m0 x4 h4 y3a0 ff5 fs3 fc0 sc0 ls0 ws0">之前提到的列表索引和切片的邏輯同樣也適用於Tuple,同時還有一些別的方法。詳細的方</div><div class="t m0 x5 h4 y3a1 ff5 fs3 fc0 sc0 ls0 ws0">法列表請參考<span class="_ _3"> </span><span class="ff7">Python<span class="_ _a"> </span></span>官方文件:<span class="ffa fc2">https://docs.python.org/3/tutorial/datastructures.</span></div><div class="t m0 x5 h4 y3a2 ffa fs3 fc2 sc0 ls0 ws0">html<span class="ff5 fc0">。</span></div><div class="t m0 x5 h8 y3a3 ff1 fs1 fc0 sc0 ls0 ws0">7.3<span class="_ _39"> </span><span class="ff2">字典</span></div><div class="t m0 x4 h4 y3a4 ff6 fs3 fc0 sc0 ls0 ws0">字典<span class="ff5">(<span class="ff7">dictionary</span>)是一種非常靈活的</span>鍵值對<span class="ff5">(<span class="ff7">k<span class="_ _8"></span>ey-v<span class="_ _8"></span>alue<span class="_ _9"> </span>pair<span class="ff5">)的對映,它也是<span class="_ _9"> </span></span>Python<span class="_ _3"> </span><span class="ff5">許</span></span></span></div><div class="t m0 x5 h4 y3a5 ff5 fs3 fc0 sc0 ls0 ws0">多內部實現的基礎。它們可以通過在大括號內用逗號分隔的鍵值對(形如<span class="_ _9"> </span><span class="ffa">key:value</span>)建立:</div><div class="t m0 x11 hc y3a6 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[27]:<span class="_ _2a"> </span><span class="fc0">numbers<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>{<span class="ff12 fcc">'<span class="ffa">one</span>'</span>:<span class="fc6">1</span>,<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">two</span>'</span>:<span class="fc6">2</span>,<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">three</span>'</span>:<span class="fc6">3</span>}</span></div><div class="t m0 x4 h4 y3a7 ff5 fs3 fc0 sc0 ls0 ws0">我們可以用列表和Tuple中提到的索引的方式來訪問和改變字典中的項,只是這裡的索引不</div><div class="t m0 x5 h4 y3a8 ff5 fs3 fc0 sc0 ls0 ws0">再是從零開始的順序下標,而是一個有效的字典索引鍵:</div><div class="t m0 x11 h6 y3a9 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[28]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">通過鍵來訪問字典中的值</span></span></div><div class="t m0 x31 hc y3aa ffa fs3 fc0 sc0 ls0 ws0">numbers[<span class="ff12 fcc">'<span class="ffa">two</span>'</span>]</div><div class="t m0 x11 hc y3ab ffa fs3 fcf sc0 ls0 ws0">Out[28]:<span class="_ _2a"> </span><span class="fc0">2</span></div><div class="t m0 x4 h4 y3ac ff5 fs3 fc0 sc0 ls0 ws0">我們也可以用索引來加入新的項:</div><div class="t m0 x11 h6 y3ad ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[29]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">設定一個新的鍵值對</span></span></div><div class="t m0 x31 hc y3ae ffa fs3 fc0 sc0 ls0 ws0">numbers[<span class="ff12 fcc">'<span class="ffa">ninety</span>'</span>]<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>90</span></div><div class="t m0 x31 hc y3af ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(numbers)</span></div><div class="t m0 x11 hc y3b0 ffa fs3 fc0 sc0 ls0 ws0">{<span class="ff12">'</span>one<span class="ff12">'</span>:<span class="_ _2a"> </span>1,<span class="_ _2a"> </span><span class="ff12">'</span>two<span class="ff12">'</span>:<span class="_ _2a"> </span>2,<span class="_ _2a"> </span><span class="ff12">'</span>three<span class="ff12">'</span>:<span class="_ _2a"> </span>3,<span class="_ _2a"> </span><span class="ff12">'</span>ninety<span class="ff12">'</span>:<span class="_ _2a"> </span>90}</div><div class="t m0 x4 h4 y98 ff5 fs3 fc0 sc0 ls0 ws0">需要留心的是字典中並<span class="ff2">沒有任何順序</span>的概念,這是設定好的。這種無序性反而使得字典的</div><div class="t m0 x5 h4 y99 ff5 fs3 fc0 sc0 ls0 ws0">實現非常有效率。不論字典的大小如何,元素的<span class="ff2">隨機訪問</span>都非常快(如果你對此感到好奇,你</div><div class="t m0 x5 h4 y9a ff5 fs3 fc0 sc0 ls0 ws0">可以試著了解一下<span class="ff6">雜湊表<span class="_ _8"></span><span class="ff5">(<span class="ff7">hash<span class="_ _9"> </span>table</span>)的概念)<span class="_ _41"></span>。<span class="_ _8"></span><span class="ff7">Python<span class="_ _34"> </span><span class="ff5">的官方文件中列出了字典可用方法的</span></span></span></span></div><div class="t m0 x5 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">完整列表:<span class="ffa fc2">https://docs.python.org/3/library/stdtypes.html</span>。</div><a class="l" href="https://docs.python.org/3/tutorial/datastructures.html"><div class="d m1" style="border-style:none;position:absolute;left:236.913000px;bottom:564.347000px;width:286.363000px;height:10.000000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="https://docs.python.org/3/tutorial/datastructures.html"><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:550.512000px;width:22.909000px;height:6.535000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="https://docs.python.org/3/library/stdtypes.html"><div class="d m1" style="border-style:none;position:absolute;left:126.545000px;bottom:69.576000px;width:269.182000px;height:10.000000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf30" class="pf w0 h0" data-page-no="30"><div class="pc pc30 w0 h0"><img class="bi x5 y3b1 w1 h2e" alt="" src="images/bg30.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">7.4.<span class="_ _16"> </span><span class="ff6">集合<span class="_ _71"> </span><span class="ff7">41</span></span></div><div class="t m0 x5 h8 ybb ff1 fs1 fc0 sc0 ls0 ws0">7.4<span class="_ _39"> </span><span class="ff2">集合</span></div><div class="t m0 x4 h4 ybc ff5 fs3 fc0 sc0 ls0 ws0">第四種基本元素集是<span class="ff6">集合</span>(<span class="ff7">set</span>)<span class="_ _5f"></span>,它和數學上的集合定義相同,元素具有<span class="ff2">唯一性</span>和<span class="ff2">無序性</span>。</div><div class="t m0 x5 h4 ybd ff5 fs3 fc0 sc0 ls0 ws0">集合的定義除了它使用和字典一樣的花括號這一點以外,和列表、Tuple非常相似:</div><div class="t m0 x11 hc y3b2 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[30]:<span class="_ _2a"> </span><span class="fc0">primes<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>{<span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">3</span>,<span class="_ _2a"> </span><span class="fc6">5</span>,<span class="_ _2a"> </span><span class="fc6">7</span>}</span></div><div class="t m0 x31 hc y3b3 ffa fs3 fc0 sc0 ls0 ws0">odds<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>{<span class="fc6">1</span>,<span class="_ _2a"> </span><span class="fc6">3</span>,<span class="_ _2a"> </span><span class="fc6">5</span>,<span class="_ _2a"> </span><span class="fc6">7</span>,<span class="_ _2a"> </span><span class="fc6">9</span>}</div><div class="t m0 x4 h4 y3b4 ff5 fs3 fc0 sc0 ls0 ws0">如果你熟悉集合的數學定義,<span class="_ _3a"></span>你也一定不會對並、<span class="_ _43"></span>交、<span class="_ _43"></span>差、<span class="_ _43"></span><span class="ff6">對等差分<span class="_ _2"></span><span class="ff5">(<span class="ff7">symmetric<span class="_ _34"> </span>difference</span>)</span></span></div><div class="t m0 x5 h4 y3b5 ff5 fs3 fc0 sc0 ls0 ws0">等操作感到陌生。<span class="_ _8"></span><span class="ff7">Python<span class="_ _9"> </span><span class="ff5">的集合通過方法和運算子實現了所有這些操作。<span class="_ _8"></span>對每一個操作,<span class="_ _8"></span>我們</span></span></div><div class="t m0 x5 h4 y3b6 ff5 fs3 fc0 sc0 ls0 ws0">都會展示兩種等價的方式:</div><div class="t m0 x11 h6 y3b7 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[31]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">聯集:包含出現在任意一個集合中的元素</span></span></div><div class="t m0 x31 h6 y3b8 ffa fs3 fc0 sc0 ls0 ws0">primes<span class="_ _2a"> </span><span class="fc6">|<span class="_ _2a"> </span></span>odds<span class="_ _48"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">使用運算子</span></span></div><div class="t m0 x31 h6 y3b9 ffa fs3 fc0 sc0 ls0 ws0">primes<span class="fc6">.</span>union(odds)<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">等價地使用物件的方法</span></span></div><div class="t m0 x11 hc y3ba ffa fs3 fcf sc0 ls0 ws0">Out[31]:<span class="_ _2a"> </span><span class="fc0">{1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>3,<span class="_ _2a"> </span>5,<span class="_ _2a"> </span>7,<span class="_ _2a"> </span>9}</span></div><div class="t m0 x11 h6 y3bb ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[32]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">交集:包含同時出現在兩個集合中的元素</span></span></div><div class="t m0 x31 h6 y3bc ffa fs3 fc0 sc0 ls0 ws0">primes<span class="_ _2a"> </span><span class="fc6">&<span class="_ _2a"> </span></span>odds<span class="_ _50"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">使用運算子</span></span></div><div class="t m0 x31 h6 y3bd ffa fs3 fc0 sc0 ls0 ws0">primes<span class="fc6">.</span>intersection(odds)<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">等價地使用物件的方法</span></span></div><div class="t m0 x11 hc y3be ffa fs3 fcf sc0 ls0 ws0">Out[32]:<span class="_ _2a"> </span><span class="fc0">{3,<span class="_ _2a"> </span>5,<span class="_ _2a"> </span>7}</span></div><div class="t m0 x11 h6 y3bf ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[33]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">差分:屬於<span class="_ _2a"> </span></span>primes<span class="_ _2a"> </span><span class="ff6">但不屬於<span class="_ _2a"> </span></span>odds<span class="_ _2a"> </span><span class="ff6">的元素</span></span></div><div class="t m0 x31 h6 y3c0 ffa fs3 fc0 sc0 ls0 ws0">primes<span class="_ _2a"> </span><span class="fc6">-<span class="_ _2a"> </span></span>odds<span class="_ _56"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">使用運算子</span></span></div><div class="t m0 x31 h6 y3c1 ffa fs3 fc0 sc0 ls0 ws0">primes<span class="fc6">.</span>difference(odds)<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">等價地使用物件的方法</span></span></div><div class="t m0 x11 hc y3c2 ffa fs3 fcf sc0 ls0 ws0">Out[33]:<span class="_ _2a"> </span><span class="fc0">{2}</span></div><div class="t m0 x11 h6 y3c3 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[34]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">對稱差分</span>:<span class="_ _2a"> </span><span class="ff6">只出現在其中一個集合的元素</span></span></div><div class="t m0 x31 h6 y3c4 ffa fs3 fc0 sc0 ls0 ws0">primes<span class="_ _2a"> </span><span class="fc6">^<span class="_ _2a"> </span></span>odds<span class="_ _75"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">使用運算子</span></span></div><div class="t m0 x31 h6 y3c5 ffa fs3 fc0 sc0 ls0 ws0">primes<span class="fc6">.</span>symmetric_difference(odds)<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">等價地使用物件的方法</span></span></div><div class="t m0 x11 hc y3c6 ffa fs3 fcf sc0 ls0 ws0">Out[34]:<span class="_ _2a"> </span><span class="fc0">{1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>9}</span></div><div class="t m0 x4 h4 y3c7 ff5 fs3 fc0 sc0 ls0 ws0">集合還有更多的方法和運算子。你可能已經猜到了我想說什麼:詳情請參考<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>官方</div><div class="t m0 x5 h4 y3c8 ff5 fs3 fc0 sc0 ls0 ws0">文件:<span class="ffa fc2">https://docs.python.org/3/library/stdtypes.html</span>。</div><div class="t m0 x5 h8 y3c9 ff1 fs1 fc0 sc0 ls0 ws0">7.5<span class="_ _39"> </span><span class="ff2">其他專用資料結構</span></div><div class="t m0 x4 h4 y99 ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _f"> </span><span class="ff5">包<span class="_ _4b"> </span>含<span class="_ _55"> </span>了<span class="_ _4b"> </span>許<span class="_ _4b"> </span>多<span class="_ _55"> </span>其<span class="_ _4b"> </span>他<span class="_ _4b"> </span>你<span class="_ _76"> </span>可<span class="_ _76"> </span>能<span class="_ _76"> </span>覺<span class="_ _4b"> </span>得<span class="_ _76"> </span>非<span class="_ _76"> </span>常<span class="_ _4b"> </span>有<span class="_ _76"> </span>用<span class="_ _4b"> </span>的<span class="_ _76"> </span>數<span class="_ _76"> </span>據<span class="_ _4b"> </span>結<span class="_ _76"> </span>構,<span class="_ _4b"> </span>它<span class="_ _76"> </span>們<span class="_ _76"> </span>通<span class="_ _4b"> </span>常<span class="_ _76"> </span>可<span class="_ _76"> </span>以<span class="_ _4b"> </span>在<span class="_ _76"> </span>內<span class="_ _4b"> </span>置</span></div><div class="t m0 x5 h4 y9a ff5 fs3 fc0 sc0 ls0 ws0">的<span class="_ _f"> </span><span class="ffa">collections<span class="_ _1f"> </span></span>模<span class="_ _4b"> </span span class="_ _4b"> </span>中<span class="_ _4b"> </span>得<span class="_ _4b"> </span>到。<span class="ffa">collections<span class="_ _f"> </span></span>模<span class="_ _4b"> </span>組<span class="_ _4b"> </span>的<span class="_ _4b"> </span>完<span class="_ _4b"> </span>整<span class="_ _4b"> </span>文<span class="_ _4b"> </span>檔<span class="_ _4b"> </span>在<span class="_ _4b"> </span>這<span class="_ _4b"> </span>裡<span class="_ _4b"> </span>可<span class="_ _4b"> </span>以<span class="_ _4b"> </span>找<span class="_ _4b"> </span>到:<span class="ffa fc2">https:</span></div><div class="t m0 x5 h4 y40 ffa fs3 fc2 sc0 ls0 ws0">//docs.python.org/3/library/collections.html<span class="ff5 fc0">,你可以自行了解這些多種多樣的物件。</span></div><a class="l" href="https://docs.python.org/3/library/stdtypes.html"><div class="d m1" style="border-style:none;position:absolute;left:104.727000px;bottom:166.765000px;width:269.182000px;height:10.000000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="https://docs.python.org/3/library/collections.html"><div class="d m1" style="border-style:none;position:absolute;left:488.912000px;bottom:85.835000px;width:34.364000px;height:8.959000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="https://docs.python.org/3/library/collections.html"><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:69.576000px;width:252.000000px;height:10.000000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf31" class="pf w0 h0" data-page-no="31"><div class="pc pc31 w0 h0"><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">42<span class="_ _70"> </span><span class="ffc">7.<span class="_ _16"> </span><span class="ff6">內建資料結構</span></span></div><div class="t m0 x4 h4 y42 ff5 fs3 fc0 sc0 ls0 ws0">特別地,我偶然發現下面幾個型別非常好用:</div><div class="t m0 x4 h4 y3ca ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ffa">collections.namedtuple<span class="ff5">:像是一個Tuple,但是每一個值都有自己的名字。</span></span></div><div class="t m0 x4 h4 y3cb ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ffa">collections.defaultdict<span class="ff5">:像是一個字典,但是未定義的鍵對應一個使用者設定的預設</span></span></div><div class="t m0 xd h4 y3cc ff5 fs3 fc0 sc0 ls0 ws0">值。</div><div class="t m0 x4 h4 y3cd ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ffa">collections.OrderedDict<span class="ff7">:<span class="_ _72"> </span><span class="ff5">像是一個字典,但是維護了鍵的順序。</span></span></span></div><div class="t m0 x4 h4 y3ce ff5 fs3 fc0 sc0 ls0 ws0">一旦你了解了這些標準的內建集合型別,使用那些擴充套件的特性就非常符合直覺。同時我建</div><div class="t m0 x5 h4 y3cf ff5 fs3 fc0 sc0 ls0 ws0">議閱讀它們的使用方法:<span class="ffa fc2">https://docs.python.org/3/library/collections.html</span>。</div><a class="l" href="https://docs.python.org/3/library/collections.html"><div class="d m1" style="border-style:none;position:absolute;left:192.000000px;bottom:635.042000px;width:286.364000px;height:10.000000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf32" class="pf w0 h0" data-page-no="32"><div class="pc pc32 w0 h0"><img class="bi x5 y3d0 w1 h2f" alt="" src="images/bg32.png"/><div class="t m0 x45 h1 y3d1 ff2 fs0 fc0 sc0 ls0 ws0">第<span class="_ _0"> </span><span class="ff1">8<span class="_ _0"> </span></span>章<span class="_ _32"> </span>控制流程</div><div class="t m0 x4 h4 y3d2 ff5 fs3 fc0 sc0 ls0 ws0">在程式設計中,學習了<span class="ff6">控制流程</span>(<span class="ff7">control<span class="_ _9"> </span>flow</span>)才算是你真正開始投入程式設計的實踐。沒有它,程</div><div class="t m0 x5 h4 y3d3 ff5 fs3 fc0 sc0 ls0 ws0">序就只是一串等待順序執行的語句列表。有了控制流程,你就可以有條件地或者迴圈地執行某一</div><div class="t m0 x5 h4 y3d4 ff5 fs3 fc0 sc0 ls0 ws0">個程式碼塊,這些基本的程式碼塊可以組合成意想不到的複雜的程式!</div><div class="t m0 x4 h4 y3d5 ff5 fs3 fc0 sc0 ls0 ws0">這裡我們會介紹條件語句(包括<span class="_ _3"> </span><span class="ffa">if</span>、<span class="ffa">elif<span class="_ _a"> </span></span>和<span class="_ _3"> </span><span class="ffa">else</span>)以及迴圈語句(包括<span class="_ _3"> </span><span class="ffa">for<span class="_ _3"> </span></span>和<span class="_ _a"> </span><span class="ffa">while</span>,</div><div class="t m0 x5 h4 y3d6 ff5 fs3 fc0 sc0 ls0 ws0">還有<span class="_ _2a"> </span><span class="ffa">break</span>、<span class="ffa">continue<span class="_ _9"> </span></span>和<span class="_ _3"> </span><span class="ffa">pass</span>)<span class="_ _33"></span>。</div><div class="t m0 x5 h8 y3d7 ff1 fs1 fc0 sc0 ls0 ws0">8.1<span class="_ _39"> </span><span class="ff2">條件語句:<span class="ff11">if</span></span>-<span class="ff11">elif</span>-<span class="ff11">else</span></div><div class="t m0 x4 h4 y3d8 ff5 fs3 fc0 sc0 ls0 ws0">條件語句通常是<span class="_ _9"> </span><span class="ff8">if-then<span class="_ _a"> </span></span>語句,程式根據布林條件執行特定程式碼片段。<span class="_ _8"></span>下面是<span class="_ _3"> </span><span class="ff7">Python<span class="_ _9"> </span></span>條件</div><div class="t m0 x5 h4 y3d9 ff5 fs3 fc0 sc0 ls0 ws0">語句一個基本的例子:</div><div class="t m0 x11 hc y3da ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[1]:<span class="_ _2a"> </span><span class="fc0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>-15</span></span></div><div class="t m0 x24 hc y3db ff11 fs3 fc4 sc0 ls0 ws0">if<span class="_ _2a"> </span><span class="ffa fc0">x<span class="_ _2a"> </span><span class="fc6">==<span class="_ _2a"> </span>0</span>:</span></div><div class="t m0 x5d hc y3dc ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(x,<span class="_ _2a"> </span><span class="fcc">"is<span class="_ _2a"> </span>zero"</span>)</span></div><div class="t m0 x24 hc y3dd ff11 fs3 fc4 sc0 ls0 ws0">elif<span class="_ _2a"> </span><span class="ffa fc0">x<span class="_ _2a"> </span><span class="fc6">><span class="_ _2a"> </span>0</span>:</span></div><div class="t m0 x5d hc y3de ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(x,<span class="_ _2a"> </span><span class="fcc">"is<span class="_ _2a"> </span>positive"</span>)</span></div><div class="t m0 x24 hc y3df ff11 fs3 fc4 sc0 ls0 ws0">elif<span class="_ _2a"> </span><span class="ffa fc0">x<span class="_ _2a"> </span><span class="fc6"><<span class="_ _2a"> </span>0</span>:</span></div><div class="t m0 x5d hc y3e0 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(x,<span class="_ _2a"> </span><span class="fcc">"is<span class="_ _2a"> </span>negative"</span>)</span></div><div class="t m0 x24 hc y3e1 ff11 fs3 fc4 sc0 ls0 ws0">else<span class="ffa fc0">:</span></div><div class="t m0 x5d hc y3e2 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(x,<span class="_ _2a"> </span><span class="fcc">"is<span class="_ _2a"> </span>unlike<span class="_ _2a"> </span>anything<span class="_ _2a"> </span>I<span class="ff12">'</span>ve<span class="_ _2a"> </span>ever<span class="_ _2a"> </span>seen..."</span>)</span></div><div class="t m0 x11 hc y3e3 ffa fs3 fc0 sc0 ls0 ws0">-15<span class="_ _2a"> </span>is<span class="_ _2a"> </span>negative</div><div class="t m0 x4 h4 y3e4 ff5 fs3 fc0 sc0 ls0 ws0">特別要注意冒號(<span class="ffa">:</span>)的使用以及用來分隔程式碼的空白符。</div><div class="t m0 x4 h4 y3e5 ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _3"> </span><span class="ff5">採用的<span class="_ _3"> </span><span class="ffa">if<span class="_ _3"> </span></span>和<span class="_ _3"> </span><span class="ffa">else<span class="_ _3"> </span></span>關鍵字也經常被用在其它語言中,但是關鍵字<span class="_ _a"> </span><span class="ffa">elif<span class="_ _3"> </span></span>會比較獨</span></div><div class="t m0 x5 h4 y3e6 ff5 fs3 fc0 sc0 ls0 ws0">特,<span class="_ _4"></span>它是<span class="_ _9"> </span><span class="ffa">else<span class="_ _2a"> </span>if<span class="_ _9"> </span></span>的縮寫。在這些條件語句中,<span class="_ _8"></span><span class="ffa">elif<span class="_ _9"> </span><span class="ff5">和<span class="_ _3"> </span></span>else<span class="_ _9"> </span><span class="ff5">區塊是可選的。<span class="_ _8"></span>另外,你可以根據</span></span></div><div class="t m0 x5 h4 y3e7 ff5 fs3 fc0 sc0 ls0 ws0">需要使用任意數量的<span class="_ _9"> </span><span class="ffa">elif<span class="_ _3"> </span></span>語句。</div><div class="t m0 x5 h8 y3e8 ff1 fs1 fc0 sc0 ls0 ws0">8.2<span class="_ _39"> </span><span class="ff11">for<span class="_ _10"> </span><span class="ff2">迴圈</span></span></div><div class="t m0 x4 h4 y9a ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _9"> </span><span class="ff5">中的迴圈是一種重複執行某些程式碼語句的一種方式。<span class="_ _2"></span>舉個例子,<span class="_ _8"></span>如果我們想要列印</span></div><div class="t m0 x5 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">出一個列表中的每一個元素,我們可以使用<span class="_ _9"> </span><span class="ffa">for<span class="_ _3"> </span></span>迴圈:</div><div class="t m0 x2f h7 y20 ff7 fs3 fc0 sc0 ls0 ws0">43</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf33" class="pf w0 h0" data-page-no="33"><div class="pc pc33 w0 h0"><img class="bi x5 y40 w1 h11" alt="" src="images/bg33.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">44<span class="_ _77"> </span><span class="ffc">8.<span class="_ _16"> </span><span class="ff6">控制流程</span></span></div><div class="t m0 x11 hc yfa ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[2]:<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span><span class="fc0">N<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span>[<span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">3</span>,<span class="_ _2a"> </span><span class="fc6">5</span>,<span class="_ _2a"> </span><span class="fc6">7</span>]:</span></div><div class="t m0 x5d h6 yfb ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(N,<span class="_ _2a"> </span>end<span class="fc6">=<span class="ff12 fcc">'<span class="_ _2a"> </span>'</span></span>)<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">在同一行上列印</span></span></span></div><div class="t m0 x11 hc y21b ffa fs3 fc0 sc0 ls0 ws0">2<span class="_ _2a"> </span>3<span class="_ _2a"> </span>5<span class="_ _2a"> </span>7</div><div class="t m0 x4 h4 y3e9 ff5 fs3 fc0 sc0 ls0 ws0">注意到<span class="_ _34"> </span><span class="ffa">for<span class="_ _34"> </span></span>迴圈的簡潔性:<span class="_ _42"></span>我們可以定義我們想要用的迴圈變數和迴圈的序列,<span class="_ _40"></span>使用<span class="_ _34"> </span><span class="ffa">in<span class="_ _34"> </span></span>來</div><div class="t m0 x5 h4 y3ea ff5 fs3 fc0 sc0 ls0 ws0">連接它們,這是一種非常符合直覺且可讀的方式。更具體地,<span class="ffa">in<span class="_ _9"> </span></span>運算子右邊的物件可以使用任</div><div class="t m0 x5 h4 y3eb ff5 fs3 fc0 sc0 ls0 ws0">何<span class="_ _3"> </span><span class="ff7">Python<span class="_ _a"> </span></span>中的迭代器。一個迭代器可以被理解成一個一般的序列,我們將會在第<span class="_ _3"> </span><span class="ff7 fc1">11<span class="_ _a"> </span></span>章<span class="fc1">迭代</span></div><div class="t m0 x5 h4 y3ec ff5 fs3 fc1 sc0 ls0 ws0">器<span class="fc0">中討論它們。</span></div><div class="t m0 x4 h4 y3ed ff5 fs3 fc0 sc0 ls0 ws0">舉個例子,<span class="ff7">Python<span class="_ _9"> </span></span>中最常用的迭代器是<span class="_ _3"> </span><span class="ffa">range<span class="_ _9"> </span></span>物件,它可以產生數字的序列:</div><div class="t m0 x11 hc y3ee ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[3]:<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span><span class="fc0">i<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span><span class="fc4">range</span>(<span class="fc6">10</span>):</span></div><div class="t m0 x5d hc y3ef ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(i,<span class="_ _2a"> </span>end<span class="fc6">=<span class="ff12 fcc">'<span class="_ _2a"> </span>'</span></span>)</span></div><div class="t m0 x11 hc y3f0 ffa fs3 fc0 sc0 ls0 ws0">0<span class="_ _2a"> </span>1<span class="_ _2a"> </span>2<span class="_ _2a"> </span>3<span class="_ _2a"> </span>4<span class="_ _2a"> </span>5<span class="_ _2a"> </span>6<span class="_ _2a"> </span>7<span class="_ _2a"> </span>8<span class="_ _2a"> </span>9</div><div class="t m0 x4 h4 y3f1 ff5 fs3 fc0 sc0 ls0 ws0">序列預設從<span class="_ _3"> </span><span class="ff15">0<span class="_ _9"> </span></span>開始產生,按照慣例,域的上界並沒有包含在輸出中。<span class="ffa">range<span class="_ _3"> </span></span>物件也可以使</div><div class="t m0 x5 h4 y3f2 ff5 fs3 fc0 sc0 ls0 ws0">用更為複雜的參數來產生:</div><div class="t m0 x11 h6 y3f3 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[4]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">從<span class="_ _2a"> </span></span>5<span class="_ _2a"> </span><span class="ff6">到<span class="_ _2a"> </span></span>10<span class="_ _2a"> </span><span class="ff6">的範圍</span></span></div><div class="t m0 x24 hc y3f4 ffa fs3 fc4 sc0 ls0 ws0">list<span class="fc0">(</span>range<span class="fc0">(<span class="fc6">5</span>,<span class="_ _2a"> </span><span class="fc6">10</span>))</span></div><div class="t m0 x11 hc y3f5 ffa fs3 fcf sc0 ls0 ws0">Out[4]:<span class="_ _2a"> </span><span class="fc0">[5,<span class="_ _2a"> </span>6,<span class="_ _2a"> </span>7,<span class="_ _2a"> </span>8,<span class="_ _2a"> </span>9]</span></div><div class="t m0 x11 h6 y3f6 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[5]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">從<span class="_ _2a"> </span></span>0<span class="_ _2a"> </span><span class="ff6">到<span class="_ _2a"> </span></span>10<span class="_ _2a"> </span><span class="ff6">的範圍,步長為<span class="_ _2a"> </span></span>2</span></div><div class="t m0 x24 hc y3f7 ffa fs3 fc4 sc0 ls0 ws0">list<span class="fc0">(</span>range<span class="fc0">(<span class="fc6">0</span>,<span class="_ _2a"> </span><span class="fc6">10</span>,<span class="_ _2a"> </span><span class="fc6">2</span>))</span></div><div class="t m0 x11 hc y3f8 ffa fs3 fcf sc0 ls0 ws0">Out[5]:<span class="_ _2a"> </span><span class="fc0">[0,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>4,<span class="_ _2a"> </span>6,<span class="_ _2a"> </span>8]</span></div><div class="t m0 x4 h4 y3f9 ff5 fs3 fc0 sc0 ls0 ws0">你會發現<span class="_ _9"> </span><span class="ffa">range<span class="_ _3"> </span></span>參數的意義和我們在第<span class="_ _9"> </span><span class="ff7 fc1">7.1<span class="_ _9"> </span></span>節<span class="fc1">列表</span>中提到的切片的語法很像。</div><div class="t m0 x4 h4 y3fa ff5 fs3 fc0 sc0 ls0 ws0">同時要留意<span class="_ _34"> </span><span class="ffa">range()<span class="_ _36"> </span></span>也是<span class="_ _34"> </span><span class="ff7">Python<span class="_ _34"> </span>2<span class="_ _36"> </span></span>和<span class="_ _34"> </span><span class="ff7">Python<span class="_ _34"> </span>3<span class="_ _34"> </span></span>的區別之一:<span class="_ _43"></span>在<span class="_ _34"> </span><span class="ff7">Python<span class="_ _34"> </span>2<span class="_ _34"> </span></span>中,<span class="_ _43"></span><span class="ffa">range()<span class="_ _34"> </span><span class="ff5">生</span></span></div><div class="t m0 x5 h4 y3fb ff5 fs3 fc0 sc0 ls0 ws0">成一個列表,而在<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span>3<span class="_ _9"> </span></span>中,<span class="ffa">range()<span class="_ _9"> </span></span>產生一個可迭代的物件。</div><div class="t m0 x5 h8 y3fc ff1 fs1 fc0 sc0 ls0 ws0">8.3<span class="_ _39"> </span><span class="ff11">while<span class="_ _10"> </span><span class="ff2">迴圈</span></span></div><div class="t m0 x4 h4 y3fd ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _9"> </span><span class="ff5">中另外的一種迴圈型別是<span class="_ _3"> </span><span class="ffa">while<span class="_ _9"> </span></span>迴圈,它保持迴圈直到不再滿足某種條件:</span></div><div class="t m0 x11 hc y3fe ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[6]:<span class="_ _2a"> </span><span class="fc0">i<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>0</span></span></div><div class="t m0 x24 hc y329 ff11 fs3 fc4 sc0 ls0 ws0">while<span class="_ _2a"> </span><span class="ffa fc0">i<span class="_ _2a"> </span><span class="fc6"><<span class="_ _2a"> </span>10</span>:</span></div><div class="t m0 x5d hc y21a ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(i,<span class="_ _2a"> </span>end<span class="fc6">=<span class="ff12 fcc">'<span class="_ _2a"> </span>'</span></span>)</span></div><div class="t m0 x5d hc y117 ffa fs3 fc0 sc0 ls0 ws0">i<span class="_ _2a"> </span><span class="fc6">+=<span class="_ _2a"> </span>1</span></div><div class="t m0 x11 hc y119 ffa fs3 fc0 sc0 ls0 ws0">0<span class="_ _2a"> </span>1<span class="_ _2a"> </span>2<span class="_ _2a"> </span>3<span class="_ _2a"> </span>4<span class="_ _2a"> </span>5<span class="_ _2a"> </span>6<span class="_ _2a"> </span>7<span class="_ _2a"> </span>8<span class="_ _2a"> </span>9</div><a class="l" href="#pf44" data-dest-detail='[68,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:475.341000px;bottom:629.859000px;width:10.909000px;height:6.870000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf44" data-dest-detail='[68,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:501.181000px;bottom:626.749000px;width:22.095000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf44" data-dest-detail='[68,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:610.490000px;width:10.909000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf2a" data-dest-detail='[42,"XYZ",72,435.51,null]'><div class="d m1" style="border-style:none;position:absolute;left:286.091000px;bottom:284.416000px;width:13.939000px;height:6.869000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf2a" data-dest-detail='[42,"XYZ",72,435.51,null]'><div class="d m1" style="border-style:none;position:absolute;left:314.576000px;bottom:281.305000px;width:21.818000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf34" class="pf w0 h0" data-page-no="34"><div class="pc pc34 w0 h0"><img class="bi x5 y3ff w1 h30" alt="" src="images/bg34.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">8.4.<span class="_ _16"> </span><span class="ff6">調整迴圈執行:<span class="ff19">BREAK<span class="_ _9"> </span></span>和<span class="_ _9"> </span><span class="ff19">CONTINUE<span class="_ _78"> </span><span class="ff7">45</span></span></span></div><div class="t m0 x4 h4 y42 ffa fs3 fc0 sc0 ls0 ws0">while<span class="_ _9"> </span><span class="ff5">迴圈的參數用一個布林表示式來表示,迴圈一直被執行直到表示式的值為假。</span></div><div class="t m0 x5 h8 y400 ff1 fs1 fc0 sc0 ls0 ws0">8.4<span class="_ _39"> </span><span class="ff2">調整迴圈執行:<span class="ff11">break<span class="_ _10"> </span></span>和<span class="_ _10"> </span><span class="ff11">continue</span></span></div><div class="t m0 x4 h4 y401 ff5 fs3 fc0 sc0 ls0 ws0">迴圈中有兩種有用的語句來微調它們的執行:</div><div class="t m0 x4 h4 y402 ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ffa">break<span class="_ _9"> </span><span class="ff5">語句跳出整個迴圈體。</span></span></div><div class="t m0 x4 h4 y403 ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ffa">continue<span class="_ _9"> </span><span class="ff5">語句跳出當前迴圈,轉到下一次迭代中。</span></span></div><div class="t m0 x4 h4 y404 ff5 fs3 fc0 sc0 ls0 ws0">它們可以被用在<span class="_ _9"> </span><span class="ffa">for<span class="_ _3"> </span></span>和<span class="_ _9"> </span><span class="ffa">while<span class="_ _9"> </span></span>迴圈中。</div><div class="t m0 x4 h4 y405 ff5 fs3 fc0 sc0 ls0 ws0">這裡有一個使用<span class="_ _3"> </span><span class="ffa">continue<span class="_ _9"> </span></span>語句來列印奇數串的例子。在這個例子中,用<span class="_ _3"> </span><span class="ffa">if-else<span class="_ _9"> </span></span>也可以</div><div class="t m0 x5 h4 y406 ff5 fs3 fc0 sc0 ls0 ws0">完成任務,只是有時候<span class="_ _9"> </span><span class="ffa">continue<span class="_ _3"> </span></span>語句是一種表達你心中想法的更為方便的方式。</div><div class="t m0 x11 hc y407 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[7]:<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span><span class="fc0">n<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span><span class="fc4">range</span>(<span class="fc6">20</span>):</span></div><div class="t m0 x5d h6 y408 ff14 fs3 fca sc0 ls0 ws0">#<span class="_ _2a"> </span><span class="ff6">如果<span class="_ _2a"> </span></span>n/2<span class="_ _2a"> </span><span class="ff6">的餘數為<span class="_ _2a"> </span></span>0<span class="ff6">,跳過本次迴圈的迴圈部分</span></div><div class="t m0 x5d hc y409 ff11 fs3 fc4 sc0 ls0 ws0">if<span class="_ _2a"> </span><span class="ffa fc0">n<span class="_ _2a"> </span><span class="fc6">%<span class="_ _2a"> </span>2<span class="_ _2a"> </span>==<span class="_ _2a"> </span>0</span>:</span></div><div class="t m0 x25 h31 y40a ff11 fs3 fc4 sc0 ls0 ws0">continue</div><div class="t m0 x5d hc y40b ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(n,<span class="_ _2a"> </span>end<span class="fc6">=<span class="ff12 fcc">'<span class="_ _2a"> </span>'</span></span>)</span></div><div class="t m0 x11 hc y40c ffa fs3 fc0 sc0 ls0 ws0">1<span class="_ _2a"> </span>3<span class="_ _2a"> </span>5<span class="_ _2a"> </span>7<span class="_ _2a"> </span>9<span class="_ _2a"> </span>11<span class="_ _2a"> </span>13<span class="_ _2a"> </span>15<span class="_ _2a"> </span>17<span class="_ _2a"> </span>19</div><div class="t m0 x4 h4 y40d ff5 fs3 fc0 sc0 ls0 ws0">這裡用<span class="_ _a"> </span><span class="ffa">break<span class="_ _3"> </span></span>語句完成了一個不算簡單的任務。這個迴圈會用<span class="ff6">斐波那契數列</span>(<span class="ff7">Fibonacci</span></div><div class="t m0 x5 h4 y40e ff7 fs3 fc0 sc0 ls0 ws0">n<span class="_ _4"></span>um<span class="_ _4"></span>bers<span class="ff5">)填充一個列表,直到數字超過某一個值:</span></div><div class="t m0 x11 hc y40f ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[8]:<span class="_ _2a"> </span><span class="fc0">a,<span class="_ _2a"> </span>b<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>0</span>,<span class="_ _2a"> </span><span class="fc6">1</span></span></div><div class="t m0 x24 hc y410 ffa fs3 fc0 sc0 ls0 ws0">amax<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>100</span></div><div class="t m0 x24 hc y411 ffa fs3 fc0 sc0 ls0 ws0">L<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[]</div><div class="t m0 x24 hc y412 ff11 fs3 fc4 sc0 ls0 ws0">while<span class="_ _2a"> </span>True<span class="ffa fc0">:</span></div><div class="t m0 x5d hc y413 ffa fs3 fc0 sc0 ls0 ws0">(a,<span class="_ _2a"> </span>b)<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>(b,<span class="_ _2a"> </span>a<span class="_ _2a"> </span><span class="fc6">+<span class="_ _2a"> </span></span>b)</div><div class="t m0 x5d hc y414 ff11 fs3 fc4 sc0 ls0 ws0">if<span class="_ _2a"> </span><span class="ffa fc0">a<span class="_ _2a"> </span><span class="fc6">><span class="_ _2a"> </span></span>amax:</span></div><div class="t m0 x25 h31 y415 ff11 fs3 fc4 sc0 ls0 ws0">break</div><div class="t m0 x5d hc y416 ffa fs3 fc0 sc0 ls0 ws0">L<span class="fc6">.</span>append(a)</div><div class="t m0 x24 hc y417 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(L)</span></div><div class="t m0 x11 hc y418 ffa fs3 fc0 sc0 ls0 ws0">[1,<span class="_ _2a"> </span>1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>3,<span class="_ _2a"> </span>5,<span class="_ _2a"> </span>8,<span class="_ _2a"> </span>13,<span class="_ _2a"> </span>21,<span class="_ _2a"> </span>34,<span class="_ _2a"> </span>55,<span class="_ _2a"> </span>89]</div><div class="t m0 x4 h4 y419 ff5 fs3 fc0 sc0 ls0 ws0">注意到我們使用<span class="_ _9"> </span><span class="ffa">while<span class="_ _2a"> </span>True<span class="_ _3"> </span></span>迴圈,它會一直迴圈直到遇到一個跳出語句!</div><div class="t m0 x5 h8 y3c9 ff1 fs1 fc0 sc0 ls0 ws0">8.5<span class="_ _39"> </span><span class="ff2">帶有<span class="_ _10"> </span><span class="ff11">else<span class="_ _10"> </span></span>區塊的迴圈</span></div><div class="t m0 x4 h4 y99 ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _13"> </span><span class="ff5">中有一個很少用到的<span class="_ _13"> </span><span class="ffa">else<span class="_ _13"> </span></span>語句,它作為<span class="_ _13"> </span><span class="ffa">for<span class="_ _a"> </span></span>和<span class="_ _13"> </span><span class="ffa">while<span class="_ _13"> </span></span>迴圈的一部分。這個<span class="ff2">循</span></span></div><div class="t m0 x5 h4 y9a ff2 fs3 fc0 sc0 ls0 ws0">環<span class="ff9">-<span class="ff11">else<span class="_ _9"> </span><span class="ff5">或許是<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>中一個讓人困惑的名字,<span class="_ _8"></span>我更傾向於把它當作<span class="_ _9"> </span><span class="ffa">nobreak<span class="_ _3"> </span></span>語句:<span class="_ _8"></span><span class="ffa">else<span class="_ _9"> </span><span class="ff5">區塊</span></span></span></span></span></div><div class="t m0 x5 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">的程式碼只有當迴圈自然結束時才能執行,也就是說不能遇到<span class="_ _9"> </span><span class="ffa">break<span class="_ _3"> </span></span>語句。</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf35" class="pf w0 h0" data-page-no="35"><div class="pc pc35 w0 h0"><img class="bi x5 y41a w1 h2f" alt="" src="images/bg35.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">46<span class="_ _77"> </span><span class="ffc">8.<span class="_ _16"> </span><span class="ff6">控制流程</span></span></div><div class="t m0 x4 h4 y42 ff5 fs3 fc0 sc0 ls0 ws0">下面是一個可能用到這個語法的例子:未優化的<span class="ff6">愛拉託遜斯篩法</span>(<span class="ff7">Sieve<span class="_ _9"> </span>of<span class="_ _3"> </span>Eratosthenes</span>)<span class="_ _5f"></span>,</div><div class="t m0 x5 h4 y77 ff5 fs3 fc0 sc0 ls0 ws0">一個用來找出質數的著名演算法:</div><div class="t m0 x11 hc y41b ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[9]:<span class="_ _2a"> </span><span class="fc0">L<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[]</span></div><div class="t m0 x24 hc y41c ffa fs3 fc0 sc0 ls0 ws0">nmax<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>30</span></div><div class="t m0 x24 hc y41d ff11 fs3 fc4 sc0 ls0 ws0">for<span class="_ _2a"> </span><span class="ffa fc0">n<span class="_ _2a"> </span></span><span class="fcb">in<span class="_ _2a"> </span></span><span class="ffa">range<span class="fc0">(<span class="fc6">2</span>,<span class="_ _2a"> </span>nmax):</span></span></div><div class="t m0 x5d hc y41e ff11 fs3 fc4 sc0 ls0 ws0">for<span class="_ _2a"> </span><span class="ffa fc0">factor<span class="_ _2a"> </span></span><span class="fcb">in<span class="_ _2a"> </span><span class="ffa fc0">L:</span></span></div><div class="t m0 x25 hc y41f ff11 fs3 fc4 sc0 ls0 ws0">if<span class="_ _2a"> </span><span class="ffa fc0">n<span class="_ _2a"> </span><span class="fc6">%<span class="_ _2a"> </span></span>factor<span class="_ _2a"> </span><span class="fc6">==<span class="_ _2a"> </span>0</span>:</span></div><div class="t m0 xc h31 y420 ff11 fs3 fc4 sc0 ls0 ws0">break</div><div class="t m0 x5d h6 y421 ff11 fs3 fc4 sc0 ls0 ws0">else<span class="ffa fc0">:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">沒有<span class="_ _2a"> </span></span>break</span></span></div><div class="t m0 x25 hc y422 ffa fs3 fc0 sc0 ls0 ws0">L<span class="fc6">.</span>append(n)</div><div class="t m0 x24 hc y423 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(L)</span></div><div class="t m0 x11 hc y424 ffa fs3 fc0 sc0 ls0 ws0">[2,<span class="_ _2a"> </span>3,<span class="_ _2a"> </span>5,<span class="_ _2a"> </span>7,<span class="_ _2a"> </span>11,<span class="_ _2a"> </span>13,<span class="_ _2a"> </span>17,<span class="_ _2a"> </span>19,<span class="_ _2a"> </span>23,<span class="_ _2a"> </span>29]</div><div class="t m0 x4 h4 y425 ff5 fs3 fc0 sc0 ls0 ws0">只有當所有的因子都不能整除給定數字<span class="_ _9"> </span><span class="ffa">n<span class="_ _3"> </span></span>時,<span class="ffa">else<span class="_ _9"> </span></span>語句才被執行。</div><div class="t m0 x4 h4 y426 ffa fs3 fc0 sc0 ls0 ws0">while<span class="_ _9"> </span><span class="ff5">迴圈的<span class="_ _3"> </span></span>else<span class="_ _9"> </span><span class="ff5">語句也是同樣的工作機制。</span></div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf36" class="pf w0 h0" data-page-no="36"><div class="pc pc36 w0 h0"><img class="bi x5 y1bd w1 h32" alt="" src="images/bg36.png"/><div class="t m0 x5f h1 y3 ff2 fs0 fc0 sc0 ls0 ws0">第<span class="_ _0"> </span><span class="ff1">9<span class="_ _0"> </span></span>章<span class="_ _32"> </span>定義和使用函式</div><div class="t m0 x4 h4 ydd ff5 fs3 fc0 sc0 ls0 ws0">到現在為止,我們的腳本只是被使用一次的簡單程式碼區塊。但事實上,我們可以將程式碼中</div><div class="t m0 x5 h4 yde ff5 fs3 fc0 sc0 ls0 ws0">有用的區塊提取出來作為可復用的函數來重新組織我們的<span class="_ _3"> </span><span class="ff7">Python<span class="_ _13"> </span></span>程式碼,使得程序更具可讀性</div><div class="t m0 x5 h4 ydf ff5 fs3 fc0 sc0 ls0 ws0">和可復用性。這裡我們將介紹兩種建立函數的方法:<span class="ffa">def<span class="_ _a"> </span></span>語句,適用於任何類型的函數;以</div><div class="t m0 x5 h4 ye1 ff5 fs3 fc0 sc0 ls0 ws0">及</div><div class="t m0 x26 hc ye2 ffa fs3 fc0 sc0 ls0 ws0">lambda</div><div class="t m0 x32 h4 ye1 ff5 fs3 fc0 sc0 ls0 ws0">語句,適用於建立較短的匿名函式。</div><div class="t m0 x5 h8 y427 ff1 fs1 fc0 sc0 ls0 ws0">9.1<span class="_ _39"> </span><span class="ff2">使用函式</span></div><div class="t m0 x4 h4 y428 ff5 fs3 fc0 sc0 ls0 ws0">函式是一組有名字的程式碼,它可以通過圓括號來進行調用。我們已經接觸過一些函數,例</div><div class="t m0 x5 h4 y429 ff5 fs3 fc0 sc0 ls0 ws0">如<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span>3<span class="_ _9"> </span></span>中的<span class="_ _9"> </span><span class="ffa">print<span class="_ _3"> </span></span>就是一個函式:</div><div class="t m0 x11 hc y42a ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[1]:<span class="_ _2a"> </span><span class="fc4">print<span class="fc0">(<span class="ff12 fcc">'<span class="ffa">abc</span>'</span>)</span></span></div><div class="t m0 x11 hc y42b ffa fs3 fc0 sc0 ls0 ws0">abc</div><div class="t m0 x4 h4 y42c ff5 fs3 fc0 sc0 ls0 ws0">這裡<span class="_ _9"> </span><span class="ffa">print<span class="_ _3"> </span></span>是函式名,而<span class="_ _9"> </span><span class="ff12">'<span class="ffa">abc</span>'<span class="_ _9"> </span></span>是函式的參數。</div><div class="t m0 x4 h4 y42d ff5 fs3 fc0 sc0 ls0 ws0">除了普通參數,<span class="_ _35"></span>還有用名字指定的<span class="ff6">關鍵字形式參數<span class="_ _8"></span><span class="ff5">(<span class="ff7">keyw<span class="_ _8"></span>ord<span class="_ _34"> </span>argument<span class="ff5">)<span class="_ _33"></span>。<span class="_ _3e"></span><span class="ffa">sep<span class="_ _9"> </span><span class="ff5">是<span class="_ _34"> </span></span>print<span class="_ _9"> </span><span class="ff5">函</span></span></span></span></span></span></div><div class="t m0 x5 h4 y42e ff5 fs3 fc0 sc0 ls0 ws0">數的一個可用的關鍵字形式參數(<span class="ff7">Python<span class="_ _9"> </span>3</span>)<span class="_ _33"></span>,它表示用來間隔各個項之間的字元:</div><div class="t m0 x11 hc y42f ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[2]:<span class="_ _2a"> </span><span class="fc4">print<span class="fc0">(<span class="fc6">1</span>,<span class="_ _2a"> </span><span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">3</span>)</span></span></div><div class="t m0 x11 hc y430 ffa fs3 fc0 sc0 ls0 ws0">1<span class="_ _2a"> </span>2<span class="_ _2a"> </span>3</div><div class="t m0 x11 hc y431 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[3]:<span class="_ _2a"> </span><span class="fc4">print<span class="fc0">(<span class="fc6">1</span>,<span class="_ _2a"> </span><span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">3</span>,<span class="_ _2a"> </span>sep<span class="fc6">=<span class="ff12 fcc">'<span class="ffa">--</span>'</span></span>)</span></span></div><div class="t m0 x11 hc y432 ffa fs3 fc0 sc0 ls0 ws0">1--2--3</div><div class="t m0 x4 h4 y433 ff5 fs3 fc0 sc0 ls0 ws0">當無關鍵字的參數和有關鍵字的參數共同使用時,有關鍵字的參數必須被放到後面。</div><div class="t m0 x5 h8 y434 ff1 fs1 fc0 sc0 ls0 ws0">9.2<span class="_ _39"> </span><span class="ff2">定義函式</span></div><div class="t m0 x4 h4 y435 ff5 fs3 fc0 sc0 ls0 ws0">自定義函式使得函式更加實用,<span class="_ _3e"></span>這樣程式碼就可以被多個不同的地方呼叫。<span class="_ _3e"></span>在<span class="_ _9"> </span><span class="ff7">Python<span class="_ _34"> </span></span>中,<span class="_ _3e"></span>函</div><div class="t m0 x5 h4 y436 ff5 fs3 fc0 sc0 ls0 ws0">數通過<span class="_ _9"> </span><span class="ffa">def<span class="_ _3"> </span></span>語句來定義。舉個例子,我們可以將前一節產生斐波那契數列的程式碼進行封裝:</div><div class="t m0 x11 hc y9a ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[4]:<span class="_ _2a"> </span><span class="ff11 fc4">def<span class="_ _2a"> </span></span><span class="fc5">fibonacci<span class="fc0">(N):</span></span></div><div class="t m0 x5d hc y40 ffa fs3 fc0 sc0 ls0 ws0">L<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[]</div><div class="t m0 x2f h7 y20 ff7 fs3 fc0 sc0 ls0 ws0">47</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf37" class="pf w0 h0" data-page-no="37"><div class="pc pc37 w0 h0"><img class="bi x5 y40 w1 h33" alt="" src="images/bg37.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">48<span class="_ _79"> </span><span class="ffc">9.<span class="_ _16"> </span><span class="ff6">定義和使用函式</span></span></div><div class="t m0 x5d hc y1d6 ffa fs3 fc0 sc0 ls0 ws0">a,<span class="_ _2a"> </span>b<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>0</span>,<span class="_ _2a"> </span><span class="fc6">1</span></div><div class="t m0 x5d hc y1d7 ff11 fs3 fc4 sc0 ls0 ws0">while<span class="_ _2a"> </span><span class="ffa">len<span class="fc0">(L)<span class="_ _2a"> </span><span class="fc6"><<span class="_ _2a"> </span></span>N:</span></span></div><div class="t m0 x25 hc y348 ffa fs3 fc0 sc0 ls0 ws0">a,<span class="_ _2a"> </span>b<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>b,<span class="_ _2a"> </span>a<span class="_ _2a"> </span><span class="fc6">+<span class="_ _2a"> </span></span>b</div><div class="t m0 x25 hc y349 ffa fs3 fc0 sc0 ls0 ws0">L<span class="fc6">.</span>append(a)</div><div class="t m0 x5d hc y34a ff11 fs3 fc4 sc0 ls0 ws0">return<span class="_ _2a"> </span><span class="ffa fc0">L</span></div><div class="t m0 x4 h4 y437 ff5 fs3 fc0 sc0 ls0 ws0">現在我們有了一個叫做<span class="_ _9"> </span><span class="ffa">fibonacci<span class="_ _9"> </span></span>的函式,<span class="_ _2"></span>它接受一個參數<span class="_ _9"> </span><span class="ffa">N</span>,<span class="_ _8"></span>完成對應這個參數的工作,</div><div class="t m0 x5 h4 y438 ff5 fs3 fc0 sc0 ls0 ws0">並且回傳一個值。下面是一個前<span class="_ _9"> </span><span class="ffa">N<span class="_ _3"> </span></span>項斐波那契數的列表:</div><div class="t m0 x11 hc y439 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[5]:<span class="_ _2a"> </span><span class="fc0">fibonacci(<span class="fc6">10</span>)</span></div><div class="t m0 x11 hc y43a ffa fs3 fcf sc0 ls0 ws0">Out[5]:<span class="_ _2a"> </span><span class="fc0">[1,<span class="_ _2a"> </span>1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>3,<span class="_ _2a"> </span>5,<span class="_ _2a"> </span>8,<span class="_ _2a"> </span>13,<span class="_ _2a"> </span>21,<span class="_ _2a"> </span>34,<span class="_ _2a"> </span>55]</span></div><div class="t m0 x4 h4 y43b ff5 fs3 fc0 sc0 ls0 ws0">如果你對類別似<span class="_ _3"> </span><span class="ff7">C<span class="_ _3"> </span></span>這樣的強類型語言比較熟悉的話,你會立即發現這裡沒有關於函數輸入、</div><div class="t m0 x5 h4 y43c ff5 fs3 fc0 sc0 ls0 ws0">輸出的類型信息。<span class="ff7">Python<span class="_ _a"> </span></span>的函數可以返回任何<span class="_ _a"> </span><span class="ff7">Python<span class="_ _a"> </span></span>的物件,無論是簡單物件還是複合物</div><div class="t m0 x5 h4 y43d ff5 fs3 fc0 sc0 ls0 ws0">件。這就意味著對其他語言來說比較難以組成的函式在<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>裡面卻可以直接實現。</div><div class="t m0 x4 h4 y43e ff5 fs3 fc0 sc0 ls0 ws0">舉個例子:多個回傳值會被簡單地放入一個用逗號間隔的Tuple中:</div><div class="t m0 x11 hc y43f ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[6]:<span class="_ _2a"> </span><span class="ff11 fc4">def<span class="_ _2a"> </span></span><span class="fc5">real_imag_conj<span class="fc0">(val):</span></span></div><div class="t m0 x5d hc y440 ff11 fs3 fc4 sc0 ls0 ws0">return<span class="_ _2a"> </span><span class="ffa fc0">val<span class="fc6">.</span>real,<span class="_ _2a"> </span>val<span class="fc6">.</span>imag,<span class="_ _2a"> </span>val<span class="fc6">.</span>conjugate()</span></div><div class="t m0 x24 hc y441 ffa fs3 fc0 sc0 ls0 ws0">r,<span class="_ _2a"> </span>i,<span class="_ _2a"> </span>c<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>real_imag_conj(<span class="fc6">3<span class="_ _2a"> </span>+<span class="_ _2a"> </span>4</span>j)</div><div class="t m0 x24 hc y442 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(r,<span class="_ _2a"> </span>i,<span class="_ _2a"> </span>c)</span></div><div class="t m0 x11 hc y443 ffa fs3 fc0 sc0 ls0 ws0">3.0<span class="_ _2a"> </span>4.0<span class="_ _2a"> </span>(3-4j)</div><div class="t m0 x5 h8 y444 ff1 fs1 fc0 sc0 ls0 ws0">9.3<span class="_ _39"> </span><span class="ff2">預設參數</span></div><div class="t m0 x4 h4 y445 ff5 fs3 fc0 sc0 ls0 ws0">在定義函式時,通常會有某些參數在<span class="ff2">大多數情況</span>下是同一個特定值,但同時我們又希望用</div><div class="t m0 x5 h4 y446 ff5 fs3 fc0 sc0 ls0 ws0">戶具有調整它的能力,因而我們給這些參數加上了預設值。思考一下之前的<span class="_ _3"> </span><span class="ffa">fibonacci<span class="_ _3"> </span></span>函數,</div><div class="t m0 x5 h4 y447 ff5 fs3 fc0 sc0 ls0 ws0">假如我們希望使用者可以自己設定起始數值,我們可以採用下面的方法:</div><div class="t m0 x11 hc y448 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[7]:<span class="_ _2a"> </span><span class="ff11 fc4">def<span class="_ _2a"> </span></span><span class="fc5">fibonacci<span class="fc0">(N,<span class="_ _2a"> </span>a<span class="fc6">=0</span>,<span class="_ _2a"> </span>b<span class="fc6">=1</span>):</span></span></div><div class="t m0 x5d hc y449 ffa fs3 fc0 sc0 ls0 ws0">L<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[]</div><div class="t m0 x5d hc y44a ff11 fs3 fc4 sc0 ls0 ws0">while<span class="_ _2a"> </span><span class="ffa">len<span class="fc0">(L)<span class="_ _2a"> </span><span class="fc6"><<span class="_ _2a"> </span></span>N:</span></span></div><div class="t m0 x25 hc y44b ffa fs3 fc0 sc0 ls0 ws0">a,<span class="_ _2a"> </span>b<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>b,<span class="_ _2a"> </span>a<span class="_ _2a"> </span><span class="fc6">+<span class="_ _2a"> </span></span>b</div><div class="t m0 x25 hc y44c ffa fs3 fc0 sc0 ls0 ws0">L<span class="fc6">.</span>append(a)</div><div class="t m0 x5d hc y44d ff11 fs3 fc4 sc0 ls0 ws0">return<span class="_ _2a"> </span><span class="ffa fc0">L</span></div><div class="t m0 x4 h4 y44e ff5 fs3 fc0 sc0 ls0 ws0">如果只給一個參數,那麼函式的呼叫結果和之前完全一樣:</div><div class="t m0 x11 hc y117 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[8]:<span class="_ _2a"> </span><span class="fc0">fibonacci(<span class="fc6">10</span>)</span></div><div class="t m0 x11 hc y119 ffa fs3 fcf sc0 ls0 ws0">Out[8]:<span class="_ _2a"> </span><span class="fc0">[1,<span class="_ _2a"> </span>1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>3,<span class="_ _2a"> </span>5,<span class="_ _2a"> </span>8,<span class="_ _2a"> </span>13,<span class="_ _2a"> </span>21,<span class="_ _2a"> </span>34,<span class="_ _2a"> </span>55]</span></div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf38" class="pf w0 h0" data-page-no="38"><div class="pc pc38 w0 h0"><img class="bi x5 y40 w1 h2c" alt="" src="images/bg38.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">9.4.<span class="_ _16"> </span><span class="ff6">可變參數:<span class="ff19">*ARGS<span class="_ _9"> </span></span>和<span class="_ _9"> </span><span class="ff19">**KWARGS<span class="_ _7a"> </span><span class="ff7">49</span></span></span></div><div class="t m0 x4 h4 y42 ff5 fs3 fc0 sc0 ls0 ws0">但是現在,我們可以用這個函式來探索新事物,比如設定新的初始值:</div><div class="t m0 x11 hc y44f ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[9]:<span class="_ _2a"> </span><span class="fc0">fibonacci(<span class="fc6">10</span>,<span class="_ _2a"> </span><span class="fc6">0</span>,<span class="_ _2a"> </span><span class="fc6">2</span>)</span></div><div class="t m0 x11 hc y450 ffa fs3 fcf sc0 ls0 ws0">Out[9]:<span class="_ _2a"> </span><span class="fc0">[2,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>4,<span class="_ _2a"> </span>6,<span class="_ _2a"> </span>10,<span class="_ _2a"> </span>16,<span class="_ _2a"> </span>26,<span class="_ _2a"> </span>42,<span class="_ _2a"> </span>68,<span class="_ _2a"> </span>110]</span></div><div class="t m0 x4 h4 y451 ff5 fs3 fc0 sc0 ls0 ws0">我們也可以按照意願用名字來指定參數的值,這樣傳入參數的順序就無所謂先後了:</div><div class="t m0 x11 hc y452 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[10]:<span class="_ _2a"> </span><span class="fc0">fibonacci(<span class="fc6">10</span>,<span class="_ _2a"> </span>b<span class="fc6">=3</span>,<span class="_ _2a"> </span>a<span class="fc6">=1</span>)</span></div><div class="t m0 x11 hc y453 ffa fs3 fcf sc0 ls0 ws0">Out[10]:<span class="_ _2a"> </span><span class="fc0">[3,<span class="_ _2a"> </span>4,<span class="_ _2a"> </span>7,<span class="_ _2a"> </span>11,<span class="_ _2a"> </span>18,<span class="_ _2a"> </span>29,<span class="_ _2a"> </span>47,<span class="_ _2a"> </span>76,<span class="_ _2a"> </span>123,<span class="_ _2a"> </span>199]</span></div><div class="t m0 x5 h8 y454 ff1 fs1 fc0 sc0 ls0 ws0">9.4<span class="_ _39"> </span><span class="ff2">可變參數:<span class="ff11">*args<span class="_ _10"> </span></span>和<span class="_ _10"> </span><span class="ff11">**kwargs</span></span></div><div class="t m0 x4 h4 y455 ff5 fs3 fc0 sc0 ls0 ws0">有時候,你可能會希望有這樣一個函式:你一開始並不知道使用者會傳多少參數給這個函式。</div><div class="t m0 x5 h4 y456 ff5 fs3 fc0 sc0 ls0 ws0">在這種情況下,你可以使用<span class="_ _9"> </span><span class="ffa">*args<span class="_ _3"> </span></span>和<span class="_ _9"> </span><span class="ffa">**kwargs<span class="_ _9"> </span></span>的特殊形式來捕捉傳進來的參數。就像這樣:</div><div class="t m0 x11 hc y457 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[11]:<span class="_ _2a"> </span><span class="ff11 fc4">def<span class="_ _2a"> </span></span><span class="fc5">catch_all<span class="fc0">(<span class="fc6">*</span>args,<span class="_ _2a"> </span><span class="fc6">**</span>kwargs):</span></span></div><div class="t m0 x28 hc y458 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fcc">"args<span class="_ _2a"> </span>="</span>,<span class="_ _2a"> </span>args)</span></div><div class="t m0 x28 hc y459 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fcc">"kwargs<span class="_ _2a"> </span>=<span class="_ _2a"> </span>"</span>,<span class="_ _2a"> </span>kwargs)</span></div><div class="t m0 x11 hc y45a ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[12]:<span class="_ _2a"> </span><span class="fc0">catch_all(<span class="fc6">1</span>,<span class="_ _2a"> </span><span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">3</span>,<span class="_ _2a"> </span>a<span class="fc6">=4</span>,<span class="_ _2a"> </span>b<span class="fc6">=5</span>)</span></div><div class="t m0 x11 hc y45b ffa fs3 fc0 sc0 ls0 ws0">args<span class="_ _2a"> </span>=<span class="_ _2a"> </span>(1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>3)</div><div class="t m0 x11 hc y45c ffa fs3 fc0 sc0 ls0 ws0">kwargs<span class="_ _2a"> </span>=<span class="_ _27"> </span>{</div><div class="t m0 x3e h34 y45d ff12 fs3 fc0 sc0 ls0 ws0">'</div><div class="t m0 x5d hc y45c ffa fs3 fc0 sc0 ls0 ws0">a</div><div class="t m0 x28 h34 y45d ff12 fs3 fc0 sc0 ls0 ws0">'</div><div class="t m0 x60 hc y45c ffa fs3 fc0 sc0 ls0 ws0">:<span class="_ _2a"> </span>4,</div><div class="t m0 x61 h34 y45d ff12 fs3 fc0 sc0 ls0 ws0">'</div><div class="t m0 xc hc y45c ffa fs3 fc0 sc0 ls0 ws0">b</div><div class="t m0 x3d h34 y45d ff12 fs3 fc0 sc0 ls0 ws0">'</div><div class="t m0 x62 hc y45c ffa fs3 fc0 sc0 ls0 ws0">:<span class="_ _2a"> </span>5}</div><div class="t m0 x11 hc y45e ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[13]:<span class="_ _2a"> </span><span class="fc0">catch_all(<span class="ff12 fcc">'<span class="ffa">a</span>'</span>,<span class="_ _2a"> </span>keyword<span class="fc6">=2</span>)</span></div><div class="t m0 x11 hc y45f ffa fs3 fc0 sc0 ls0 ws0">args<span class="_ _2a"> </span>=<span class="_ _2a"> </span>(<span class="ff12">'</span>a<span class="ff12">'</span>,)</div><div class="t m0 x11 hc y460 ffa fs3 fc0 sc0 ls0 ws0">kwargs<span class="_ _2a"> </span>=<span class="_ _27"> </span>{<span class="ff12">'</span>keyword<span class="ff12">'</span>:<span class="_ _2a"> </span>2}</div><div class="t m0 x4 h4 y461 ff5 fs3 fc0 sc0 ls0 ws0">這<span class="_ _36"> </span>裡<span class="_ _0"> </span><span class="ffa">*args<span class="_ _0"> </span></span>和<span class="_ _0"> </span><span class="ffa">**kwargs<span class="_ _0"> </span></span>的<span class="_ _36"> </span>名<span class="_ _34"> </span>字<span class="_ _36"> </span>並<span class="_ _36"> </span>不<span class="_ _34"> </span>重<span class="_ _36"> </span>要,<span class="_ _36"> </span>重<span class="_ _34"> </span>要<span class="_ _36"> </span>的<span class="_ _34"> </span>是<span class="_ _36"> </span>在<span class="_ _36"> </span>它<span class="_ _34"> </span>們<span class="_ _36"> </span>前<span class="_ _36"> </span>面<span class="_ _34"> </span>的<span class="_ _36"> </span>字<span class="_ _36"> </span>符<span class="_ _0"> </span><span class="ffa">*</span>。</div><div class="t m0 x5 h4 y462 ffa fs3 fc0 sc0 ls0 ws0">args<span class="_ _13"> </span><span class="ff5">和<span class="_ _72"> </span></span>kwargs<span class="_ _72"> </span><span class="ff5">只不過是慣用的變<span class="_ _4a"></span>量名,是<span class="_ _13"> </span><span class="ff7">“arguments”<span class="_ _13"> </span></span>和<span class="_ _72"> </span><span class="ff7">“k<span class="_ _4"></span>eyw<span class="_ _4"></span>ord<span class="_ _13"> </span>arguments”<span class="_ _13"> </span><span class="ff5">的縮寫。</span></span></span></div><div class="t m0 x5 h4 y463 ff5 fs3 fc0 sc0 ls0 ws0">起作用的還是<span class="_ _4a"></span>星號字元(<span class="ffa">*</span>)<span class="_ _42"></span>:變數前<span class="_ _4a"></span>面單獨的<span class="_ _72"> </span><span class="ffa">*<span class="_ _13"> </span></span>意味<span class="_ _4a"></span>著“將其展開為列表”<span class="_ _42"></span>,<span class="_ _4a"></span>而變數前面</div><div class="t m0 x5 h4 y464 ff5 fs3 fc0 sc0 ls0 ws0">的<span class="_ _9"> </span><span class="ffa">**<span class="_ _3"> </span></span>則意味著“將其展開為字典”<span class="_ _5f"></span>。事實上,這種語法不僅僅被用在函式定義中,函式的呼叫</div><div class="t m0 x5 h4 y465 ff5 fs3 fc0 sc0 ls0 ws0">同樣也是如此:</div><div class="t m0 x11 hc y328 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[14]:<span class="_ _2a"> </span><span class="fc0">inputs<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>(<span class="fc6">1</span>,<span class="_ _2a"> </span><span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">3</span>)</span></div><div class="t m0 x31 hc y3fe ffa fs3 fc0 sc0 ls0 ws0">keywords<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>{<span class="ff12 fcc">'<span class="ffa">pi</span>'</span>:<span class="_ _2a"> </span><span class="fc6">3.14</span>}</div><div class="t m0 x31 hc y21a ffa fs3 fc0 sc0 ls0 ws0">catch_all(<span class="fc6">*</span>inputs,<span class="_ _2a"> </span><span class="fc6">**</span>keywords)</div><div class="t m0 x11 hc y118 ffa fs3 fc0 sc0 ls0 ws0">args<span class="_ _2a"> </span>=<span class="_ _2a"> </span>(1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>3)</div><div class="t m0 x11 hc y119 ffa fs3 fc0 sc0 ls0 ws0">kwargs<span class="_ _2a"> </span>=<span class="_ _27"> </span>{<span class="ff12">'</span>pi<span class="ff12">'</span>:<span class="_ _2a"> </span>3.14}</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf39" class="pf w0 h0" data-page-no="39"><div class="pc pc39 w0 h0"><img class="bi x5 ydc w1 h35" alt="" src="images/bg39.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">50<span class="_ _79"> </span><span class="ffc">9.<span class="_ _16"> </span><span class="ff6">定義和使用函式</span></span></div><div class="t m0 x5 h8 ybb ff1 fs1 fc0 sc0 ls0 ws0">9.5<span class="_ _39"> </span><span class="ff2">匿名(</span>Lam<span class="_ _8"></span>bda<span class="ff2">)函式</span></div><div class="t m0 x4 h4 y466 ff5 fs3 fc0 sc0 ls0 ws0">之前我們已經快速了解了定義函式最常用的方式,也就是通過<span class="_ _3"> </span><span class="ffa">def<span class="_ _9"> </span></span>語句。這裡我們將介紹</div><div class="t m0 x5 h4 y467 ff5 fs3 fc0 sc0 ls0 ws0">另一種用<span class="_ _9"> </span><span class="ffa">lambda<span class="_ _3"> </span></span>語句定義的更為短小的一次性函式。它看起來長成這樣:</div><div class="t m0 x11 hc y468 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[15]:<span class="_ _2a"> </span><span class="fc0">add<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="ff11 fc4">lambda<span class="_ _2a"> </span></span></span>x,<span class="_ _2a"> </span>y:<span class="_ _2a"> </span>x<span class="_ _2a"> </span><span class="fc6">+<span class="_ _2a"> </span></span>y</span></div><div class="t m0 x31 hc y469 ffa fs3 fc0 sc0 ls0 ws0">add(<span class="fc6">1</span>,<span class="_ _2a"> </span><span class="fc6">2</span>)</div><div class="t m0 x11 hc y46a ffa fs3 fcf sc0 ls0 ws0">Out[15]:<span class="_ _2a"> </span><span class="fc0">3</span></div><div class="t m0 x4 h4 y46b ff5 fs3 fc0 sc0 ls0 ws0">這個<span class="_ _9"> </span><span class="ff7">Lambda<span class="_ _3"> </span></span>函式粗略地等價於:</div><div class="t m0 x11 hc y46c ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[16]:<span class="_ _2a"> </span><span class="ff11 fc4">def<span class="_ _2a"> </span></span><span class="fc5">add<span class="fc0">(x,<span class="_ _2a"> </span>y):</span></span></div><div class="t m0 x28 hc y46d ff11 fs3 fc4 sc0 ls0 ws0">return<span class="_ _2a"> </span><span class="ffa fc0">x<span class="_ _2a"> </span><span class="fc6">+<span class="_ _2a"> </span></span>y</span></div><div class="t m0 x4 h4 y46e ff5 fs3 fc0 sc0 ls0 ws0">那麼為什麼我們會想要用這種語法呢?首先,在<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>中有這樣一個事實:<span class="ff2">一切都是對</span></div><div class="t m0 x5 h4 y46f ff2 fs3 fc0 sc0 ls0 ws0">象<span class="ff5">,甚至是函式本身!這就意味著函式可以被當作參數傳入另一個函式。</span></div><div class="t m0 x63 hb y470 ffe fs4 fc1 sc0 ls0 ws0">1</div><div class="t m0 x4 h4 y471 ff5 fs3 fc0 sc0 ls0 ws0">下面是對應的例子:假設我們有一些資料,它們存放在一個由字典組成的列表中。</div><div class="t m0 x11 hc y472 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[17]:<span class="_ _2a"> </span><span class="fc0">data<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[{<span class="ff12 fcc">'<span class="ffa">first</span>'</span>:<span class="ff12 fcc">'<span class="ffa">Guido</span>'</span>,<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">last</span>'</span>:<span class="ff12 fcc">'<span class="ffa">Van<span class="_ _2a"> </span>Rossum</span>'</span>,<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">YOB</span>'</span>:<span class="fc6">1956</span>},</span></div><div class="t m0 x3a hc y473 ffa fs3 fc0 sc0 ls0 ws0">{<span class="ff12 fcc">'<span class="ffa">first</span>'</span>:<span class="ff12 fcc">'<span class="ffa">Grace</span>'</span>,<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">last</span>'</span>:<span class="ff12 fcc">'<span class="ffa">Hopper</span>'</span>,<span class="_ _4f"> </span><span class="ff12 fcc">'<span class="ffa">YOB</span>'</span>:<span class="fc6">1906</span>},</div><div class="t m0 x3a hc y474 ffa fs3 fc0 sc0 ls0 ws0">{<span class="ff12 fcc">'<span class="ffa">first</span>'</span>:<span class="ff12 fcc">'<span class="ffa">Alan</span>'</span>,<span class="_ _27"> </span><span class="ff12 fcc">'<span class="ffa">last</span>'</span>:<span class="ff12 fcc">'<span class="ffa">Turing</span>'</span>,<span class="_ _4f"> </span><span class="ff12 fcc">'<span class="ffa">YOB</span>'</span>:<span class="fc6">1912</span>}]</div><div class="t m0 x4 h4 y475 ff5 fs3 fc0 sc0 ls0 ws0">現在我們想要對資料進行排序。<span class="ff7">Python<span class="_ _9"> </span></span>已經有一個<span class="_ _3"> </span><span class="ffa">sorted<span class="_ _9"> </span></span>函式來完成這項工作:</div><div class="t m0 x11 hc y476 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[18]:<span class="_ _2a"> </span><span class="fc4">sorted<span class="fc0">([<span class="fc6">2</span>,<span class="fc6">4</span>,<span class="fc6">3</span>,<span class="fc6">5</span>,<span class="fc6">1</span>,<span class="fc6">6</span>])</span></span></div><div class="t m0 x11 hc y477 ffa fs3 fcf sc0 ls0 ws0">Out[18]:<span class="_ _2a"> </span><span class="fc0">[1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>3,<span class="_ _2a"> </span>4,<span class="_ _2a"> </span>5,<span class="_ _2a"> </span>6]</span></div><div class="t m0 x4 h4 y478 ff5 fs3 fc0 sc0 ls0 ws0">然而字典並不可排序:我們需要一種方法來告訴函式按照<span class="ff2">什麼順序</span>排列我們的資料。我們</div><div class="t m0 x5 h4 y479 ff5 fs3 fc0 sc0 ls0 ws0">可以定義一個<span class="_ _9"> </span><span class="ffa">key<span class="_ _3"> </span></span>函式,只要給它一個項,它就可以回傳排序所依據的鍵:</div><div class="t m0 x11 h6 y47a ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[19]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">根據名字按字典序排序</span></span></div><div class="t m0 x31 hc y47b ffa fs3 fc4 sc0 ls0 ws0">sorted<span class="fc0">(data,<span class="_ _2a"> </span>key<span class="fc6">=</span></span><span class="ff11">lambda<span class="_ _2a"> </span></span><span class="fc0">item:<span class="_ _2a"> </span>item[<span class="ff12 fcc">'<span class="ffa">first</span>'</span>])</span></div><div class="t m0 x11 hc y47c ffa fs3 fcf sc0 ls0 ws0">Out[19]:<span class="_ _2a"> </span><span class="fc0">[{<span class="ff12">'</span>YOB<span class="ff12">'</span>:<span class="_ _2a"> </span>1912,<span class="_ _2a"> </span><span class="ff12">'</span>first<span class="ff12">'</span>:<span class="_ _2a"> </span><span class="ff12">'</span>Alan<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>last<span class="ff12">'</span>:<span class="_ _2a"> </span><span class="ff12">'</span>Turing<span class="ff12">'</span>},</span></div><div class="t m0 x64 hc y47d ffa fs3 fc0 sc0 ls0 ws0">{<span class="ff12">'</span>YOB<span class="ff12">'</span>:<span class="_ _2a"> </span>1906,<span class="_ _2a"> </span><span class="ff12">'</span>first<span class="ff12">'</span>:<span class="_ _2a"> </span><span class="ff12">'</span>Grace<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>last<span class="ff12">'</span>:<span class="_ _2a"> </span><span class="ff12">'</span>Hopper<span class="ff12">'</span>},</div><div class="t m0 x64 hc y47e ffa fs3 fc0 sc0 ls0 ws0">{<span class="ff12">'</span>YOB<span class="ff12">'</span>:<span class="_ _2a"> </span>1956,<span class="_ _2a"> </span><span class="ff12">'</span>first<span class="ff12">'</span>:<span class="_ _2a"> </span><span class="ff12">'</span>Guido<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>last<span class="ff12">'</span>:<span class="_ _2a"> </span><span class="ff12">'</span>Van<span class="_ _2a"> </span>Rossum<span class="ff12">'</span>}]</div><div class="t m0 x11 h6 y47f ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[20]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">根據出生日期排序</span></span></div><div class="t m0 x31 hc y480 ffa fs3 fc4 sc0 ls0 ws0">sorted<span class="fc0">(data,<span class="_ _2a"> </span>key<span class="fc6">=</span></span><span class="ff11">lambda<span class="_ _2a"> </span></span><span class="fc0">item:<span class="_ _2a"> </span>item[<span class="ff12 fcc">'<span class="ffa">YOB</span>'</span>])</span></div><div class="t m0 x12 hd yf9 fff fs5 fc0 sc0 ls0 ws0">1</div><div class="t m0 xa he y40 ff5 fs6 fc0 sc0 ls0 ws0">譯者注:這就是所謂<span class="ff6">一等函式</span>(<span class="ff10">first-order<span class="_ _34"> </span>function</span>)的概念:將函式像值一樣傳遞。</div><a class="l" href="#pf39" data-dest-detail='[57,"XYZ",88.59,85.15,null]'><div class="d m1" style="border-style:none;position:absolute;left:421.091000px;bottom:503.583000px;width:4.234000px;height:5.136000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf3a" class="pf w0 h0" data-page-no="3a"><div class="pc pc3a w0 h0"><img class="bi x5 y481 w1 h36" alt="" src="images/bg3a.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">9.5.<span class="_ _16"> </span><span class="ff6">匿名(</span>LAMBD<span class="_ _8"></span>A<span class="ff6">)函式<span class="_ _7b"> </span><span class="ff7">51</span></span></div><div class="t m0 x11 hc y1d6 ffa fs3 fcf sc0 ls0 ws0">Out[20]:<span class="_ _2a"> </span><span class="fc0">[{<span class="ff12">'</span>YOB<span class="ff12">'</span>:<span class="_ _2a"> </span>1906,<span class="_ _2a"> </span><span class="ff12">'</span>first<span class="ff12">'</span>:<span class="_ _2a"> </span><span class="ff12">'</span>Grace<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>last<span class="ff12">'</span>:<span class="_ _2a"> </span><span class="ff12">'</span>Hopper<span class="ff12">'</span>},</span></div><div class="t m0 x64 hc y1d7 ffa fs3 fc0 sc0 ls0 ws0">{<span class="ff12">'</span>YOB<span class="ff12">'</span>:<span class="_ _2a"> </span>1912,<span class="_ _2a"> </span><span class="ff12">'</span>first<span class="ff12">'</span>:<span class="_ _2a"> </span><span class="ff12">'</span>Alan<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>last<span class="ff12">'</span>:<span class="_ _2a"> </span><span class="ff12">'</span>Turing<span class="ff12">'</span>},</div><div class="t m0 x64 hc y348 ffa fs3 fc0 sc0 ls0 ws0">{<span class="ff12">'</span>YOB<span class="ff12">'</span>:<span class="_ _2a"> </span>1956,<span class="_ _2a"> </span><span class="ff12">'</span>first<span class="ff12">'</span>:<span class="_ _2a"> </span><span class="ff12">'</span>Guido<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>last<span class="ff12">'</span>:<span class="_ _2a"> </span><span class="ff12">'</span>Van<span class="_ _2a"> </span>Rossum<span class="ff12">'</span>}]</div><div class="t m0 x4 h4 y482 ff5 fs3 fc0 sc0 ls0 ws0">儘管這些函式一定可以用正常的<span class="_ _3"> </span><span class="ffa">def<span class="_ _3"> </span></span>語法來定義,但<span class="_ _3"> </span><span class="ffa">lambda<span class="_ _3"> </span></span>的語法則讓這些短小的一次</div><div class="t m0 x5 h4 y483 ff5 fs3 fc0 sc0 ls0 ws0">性函式使用起來更加方便。</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf3b" class="pf w0 h0" data-page-no="3b"><div class="pc pc3b w0 h0"></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf3c" class="pf w0 h0" data-page-no="3c"><div class="pc pc3c w0 h0"><img class="bi x5 y1bd w1 h37" alt="" src="images/bg3c.png"/><div class="t m0 x65 h1 y3 ff2 fs0 fc0 sc0 ls0 ws0">第<span class="_ _0"> </span><span class="ff1">10<span class="_ _0"> </span></span>章<span class="_ _32"> </span>錯誤和異常</div><div class="t m0 x4 h4 y204 ff5 fs3 fc0 sc0 ls0 ws0">不論你的程式碼水平怎麼樣,你終究會在程式碼中遇到一些錯誤。可能遇到的錯誤一般有下列</div><div class="t m0 x5 h4 y205 ff5 fs3 fc0 sc0 ls0 ws0">三種:</div><div class="t m0 x4 h4 y484 ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff6">語法錯誤<span class="ff5">(<span class="ff7">syn<span class="_ _4"></span>tax<span class="_ _3"> </span>error<span class="ff5">)<span class="_ _7"></span>:由於你寫出了不合法的<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>程式碼導致的錯誤(一般很好</span></span></span></span></div><div class="t m0 xd h4 y485 ff5 fs3 fc0 sc0 ls0 ws0">修改)<span class="_ _33"></span>。</div><div class="t m0 x4 h4 y486 ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff6">執行時錯誤<span class="_ _8"></span><span class="ff5">(<span class="ff7">runtime<span class="_ _34"> </span>error</span>)<span class="_ _33"></span>:<span class="_ _2"></span>由於語法上有效程式碼無法得到執行導致的錯誤。<span class="_ _35"></span>也許是由</span></span></div><div class="t m0 xd h4 y487 ff5 fs3 fc0 sc0 ls0 ws0">於不合法的使用者輸入導致的(有時候很好修改)<span class="_ _33"></span>。</div><div class="t m0 x4 h4 y488 ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff6">語意錯誤<span class="ff5">(<span class="ff7">semantic<span class="_ _9"> </span>error</span>)<span class="_ _7"></span>:那些邏輯上的錯誤。程式碼執行時並沒有出現問題,但是結</span></span></div><div class="t m0 xd h4 y489 ff5 fs3 fc0 sc0 ls0 ws0">果卻不是你想要的那樣(一般很難追蹤和修改)<span class="_ _33"></span>。</div><div class="t m0 x4 h4 y48a ff5 fs3 fc0 sc0 ls0 ws0">這裡我們主要來看一下如何乾淨地處理執行時錯誤。<span class="_ _2"></span>我們可以看到,<span class="_ _8"></span><span class="ff7">Python<span class="_ _34"> </span><span class="ff5">使用它的<span class="ff6">異常</span></span></span></div><div class="t m0 x5 h4 y48b ff6 fs3 fc0 sc0 ls0 ws0">處理<span class="ff5">(<span class="ff7">exception<span class="_ _9"> </span>handling</span>)框架來處理執行時錯誤。</span></div><div class="t m0 x5 h8 y48c ff1 fs1 fc0 sc0 ls0 ws0">10.1<span class="_ _39"> </span><span class="ff2">執行時錯誤</span></div><div class="t m0 x4 h4 y48d ff5 fs3 fc0 sc0 ls0 ws0">如果你寫過一些<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>程式碼,你應該會遇到一些執行時錯誤,它們可能發生在各種情況</div><div class="t m0 x5 h4 y48e ff5 fs3 fc0 sc0 ls0 ws0">下。</div><div class="t m0 x4 h4 y48f ff5 fs3 fc0 sc0 ls0 ws0">比如,如果你嘗試使用一個未定義的變數:</div><div class="t m0 x11 hc y490 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[1]:<span class="_ _2a"> </span><span class="fc4">print<span class="fc0">(Q)</span></span></div><div class="t m0 x24 hc y491 ffa fs3 fc0 sc0 ls0 ws0">----------------------------------------------------------------------</div><div class="t m0 x24 hc y492 ffa fs3 fc0 sc0 ls0 ws0">NameError<span class="_ _73"> </span>Traceback<span class="_ _2a"> </span>(most<span class="_ _2a"> </span>recent<span class="_ _2a"> </span>call<span class="_ _2a"> </span>last)</div><div class="t m0 x24 hc y493 ffa fs3 fc0 sc0 ls0 ws0"><ipython-input-3-e796bdcf24ff><span class="_ _2a"> </span>in<span class="_ _2a"> </span><module>()</div><div class="t m0 x29 hc y494 ffa fs3 fc0 sc0 ls0 ws0">----><span class="_ _2a"> </span>1<span class="_ _2a"> </span>print(Q)</div><div class="t m0 x24 hc y495 ffa fs3 fc0 sc0 ls0 ws0">NameError:<span class="_ _2a"> </span>name<span class="_ _2a"> </span><span class="ff12">'</span>Q<span class="ff12">'<span class="_ _2a"> </span></span>is<span class="_ _2a"> </span>not<span class="_ _2a"> </span>defined</div><div class="t m0 x4 h4 y496 ff5 fs3 fc0 sc0 ls0 ws0">或者你使用一個未定義的操作:</div><div class="t m0 x11 hc y99 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[2]:<span class="_ _2a"> </span><span class="fc6">1<span class="_ _2a"> </span>+<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">abc</span>'</span></span></div><div class="t m0 x24 hc y40 ffa fs3 fc0 sc0 ls0 ws0">----------------------------------------------------------------------</div><div class="t m0 x2f h7 y20 ff7 fs3 fc0 sc0 ls0 ws0">53</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf3d" class="pf w0 h0" data-page-no="3d"><div class="pc pc3d w0 h0"><img class="bi x5 y497 w1 h38" alt="" src="images/bg3d.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">54<span class="_ _7c"> </span><span class="ffc">10.<span class="_ _16"> </span><span class="ff6">錯誤和異常</span></span></div><div class="t m0 x24 hc y1d7 ffa fs3 fc0 sc0 ls0 ws0">TypeError<span class="_ _73"> </span>Traceback<span class="_ _2a"> </span>(most<span class="_ _2a"> </span>recent<span class="_ _2a"> </span>call<span class="_ _2a"> </span>last)</div><div class="t m0 x24 hc y349 ffa fs3 fc0 sc0 ls0 ws0"><ipython-input-4-aab9e8ede4f7><span class="_ _2a"> </span>in<span class="_ _2a"> </span><module>()</div><div class="t m0 x29 hc y34a ffa fs3 fc0 sc0 ls0 ws0">----><span class="_ _2a"> </span>1<span class="_ _2a"> </span>1<span class="_ _2a"> </span>+<span class="_ _2a"> </span><span class="ff12">'</span>abc<span class="ff12">'</span></div><div class="t m0 x24 hc y34c ffa fs3 fc0 sc0 ls0 ws0">TypeError:<span class="_ _2a"> </span>unsupported<span class="_ _2a"> </span>operand<span class="_ _2a"> </span>type(s)<span class="_ _2a"> </span>for<span class="_ _2a"> </span>+:<span class="_ _2a"> </span><span class="ff12">'</span>int<span class="ff12">'<span class="_ _2a"> </span></span>and<span class="_ _2a"> </span><span class="ff12">'</span>str<span class="ff12">'</span></div><div class="t m0 x4 h4 y498 ff5 fs3 fc0 sc0 ls0 ws0">或者你在嘗試計算一個數學上非法的結果:</div><div class="t m0 x11 hc y499 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[3]:<span class="_ _2a"> </span><span class="fc6">2<span class="_ _2a"> </span>/<span class="_ _2a"> </span>0</span></div><div class="t m0 x24 hc y49a ffa fs3 fc0 sc0 ls0 ws0">----------------------------------------------------------------------</div><div class="t m0 x24 hc y49b ffa fs3 fc0 sc0 ls0 ws0">ZeroDivisionError<span class="_ _7d"> </span>Traceback<span class="_ _2a"> </span>(most<span class="_ _2a"> </span>recent<span class="_ _2a"> </span>call<span class="_ _2a"> </span>last)</div><div class="t m0 x24 hc y49c ffa fs3 fc0 sc0 ls0 ws0"><ipython-input-5-ae0c5d243292><span class="_ _2a"> </span>in<span class="_ _2a"> </span><module>()</div><div class="t m0 x29 hc y49d ffa fs3 fc0 sc0 ls0 ws0">----><span class="_ _2a"> </span>1<span class="_ _2a"> </span>2<span class="_ _2a"> </span>/<span class="_ _2a"> </span>0</div><div class="t m0 x24 hc y49e ffa fs3 fc0 sc0 ls0 ws0">ZeroDivisionError:<span class="_ _2a"> </span>division<span class="_ _2a"> </span>by<span class="_ _2a"> </span>zero</div><div class="t m0 x4 h4 y49f ff5 fs3 fc0 sc0 ls0 ws0">或者你在嘗試訪問一個在列表中不存在的元素:</div><div class="t m0 x11 hc y4a0 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[4]:<span class="_ _2a"> </span><span class="fc0">L<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[<span class="fc6">1</span>,<span class="_ _2a"> </span><span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">3</span>]</span></div><div class="t m0 x24 hc y4a1 ffa fs3 fc0 sc0 ls0 ws0">L[<span class="fc6">1000</span>]</div><div class="t m0 x24 hc y4a2 ffa fs3 fc0 sc0 ls0 ws0">----------------------------------------------------------------------</div><div class="t m0 x24 hc y4a3 ffa fs3 fc0 sc0 ls0 ws0">IndexError<span class="_ _7e"> </span>Traceback<span class="_ _2a"> </span>(most<span class="_ _2a"> </span>recent<span class="_ _2a"> </span>call<span class="_ _2a"> </span>last)</div><div class="t m0 x24 hc y4a4 ffa fs3 fc0 sc0 ls0 ws0"><ipython-input-6-06b6eb1b8957><span class="_ _2a"> </span>in<span class="_ _2a"> </span><module>()</div><div class="t m0 x64 hc y4a5 ffa fs3 fc0 sc0 ls0 ws0">1<span class="_ _2a"> </span>L<span class="_ _2a"> </span>=<span class="_ _2a"> </span>[1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>3]</div><div class="t m0 x29 hc y4a6 ffa fs3 fc0 sc0 ls0 ws0">----><span class="_ _2a"> </span>2<span class="_ _2a"> </span>L[1000]</div><div class="t m0 x24 hc y4a7 ffa fs3 fc0 sc0 ls0 ws0">IndexError:<span class="_ _2a"> </span>list<span class="_ _2a"> </span>index<span class="_ _2a"> </span>out<span class="_ _2a"> </span>of<span class="_ _2a"> </span>range</div><div class="t m0 x4 h4 y4a8 ff5 fs3 fc0 sc0 ls0 ws0">注意到在每個例子中,<span class="ff7">Python<span class="_ _a"> </span></span>不僅簡單地指出了哪裡有錯誤發生,還拋出了一個<span class="ff2">有意義</span></div><div class="t m0 x5 h4 y4a9 ff5 fs3 fc0 sc0 ls0 ws0">的異常說明:包括了什麼程式碼出錯,以及具體哪一行程式碼出錯。</div><div class="t m0 x4 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">在你試圖追蹤程式碼中的問題根源時,遇到這樣有意義的報錯通常很有幫助。</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf3e" class="pf w0 h0" data-page-no="3e"><div class="pc pc3e w0 h0"><img class="bi x5 y40 w1 h39" alt="" src="images/bg3e.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">10.2.<span class="_ _16"> </span><span class="ff6">捕獲異常:<span class="ff19">TRY<span class="_ _9"> </span></span>和<span class="_ _9"> </span><span class="ff19">EXCEPT<span class="_ _7f"> </span><span class="ff7">55</span></span></span></div><div class="t m0 x5 h8 ybb ff1 fs1 fc0 sc0 ls0 ws0">10.2<span class="_ _39"> </span><span class="ff2">捕獲異常:<span class="ff11">try<span class="_ _10"> </span></span>和<span class="_ _10"> </span><span class="ff11">except</span></span></div><div class="t m0 x4 h4 y4aa ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _3"> </span><span class="ff5">給你提供的捕獲運行時異常的主要工具就是<span class="_ _3"> </span><span class="ffa">try</span></span>...<span class="ffa">except<span class="_ _3"> </span><span class="ff5">語句,它的基本結構就</span></span></div><div class="t m0 x5 h4 y4ab ff5 fs3 fc0 sc0 ls0 ws0">像這樣:</div><div class="t m0 x11 hc y4ac ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[5]:<span class="_ _2a"> </span><span class="ff11 fc4">try</span><span class="fc0">:</span></div><div class="t m0 x5d hc y4ad ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fcc">"this<span class="_ _2a"> </span>gets<span class="_ _2a"> </span>executed<span class="_ _2a"> </span>first"</span>)</span></div><div class="t m0 x24 hc y4ae ff11 fs3 fc4 sc0 ls0 ws0">except<span class="ffa fc0">:</span></div><div class="t m0 x5d hc y4af ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fcc">"this<span class="_ _2a"> </span>gets<span class="_ _2a"> </span>executed<span class="_ _2a"> </span>only<span class="_ _2a"> </span>if<span class="_ _2a"> </span>there<span class="_ _2a"> </span>is<span class="_ _2a"> </span>an<span class="_ _2a"> </span>error"</span>)</span></div><div class="t m0 x11 hc y4b0 ffa fs3 fc0 sc0 ls0 ws0">this<span class="_ _2a"> </span>gets<span class="_ _2a"> </span>executed<span class="_ _2a"> </span>first</div><div class="t m0 x4 h4 y4b1 ff5 fs3 fc0 sc0 ls0 ws0">注意到第二個程式碼區塊沒有被執行:這是因為第一個程式碼區塊並沒有返回錯誤。讓我們看看</div><div class="t m0 x5 h4 y4b2 ff5 fs3 fc0 sc0 ls0 ws0">在<span class="_ _9"> </span><span class="ffa">try<span class="_ _3"> </span></span>程式碼區塊中放一個有問題的語句會發生什麼:</div><div class="t m0 x11 hc y4b3 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[6]:<span class="_ _2a"> </span><span class="ff11 fc4">try</span><span class="fc0">:</span></div><div class="t m0 x5d hc y4b4 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fcc">"let<span class="ff12">'</span>s<span class="_ _2a"> </span>try<span class="_ _2a"> </span>something:"</span>)</span></div><div class="t m0 x5d hc y4b5 ffa fs3 fc0 sc0 ls0 ws0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>1<span class="_ _2a"> </span>/<span class="_ _2a"> </span>0<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span>ZeroDivisionError</span></span></div><div class="t m0 x24 hc y4b6 ff11 fs3 fc4 sc0 ls0 ws0">except<span class="ffa fc0">:</span></div><div class="t m0 x5d hc y4b7 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fcc">"something<span class="_ _2a"> </span>bad<span class="_ _2a"> </span>happened!"</span>)</span></div><div class="t m0 x11 hc y4b8 ffa fs3 fc0 sc0 ls0 ws0">let<span class="ff12">'</span>s<span class="_ _2a"> </span>try<span class="_ _2a"> </span>something:</div><div class="t m0 x11 hc y4b9 ffa fs3 fc0 sc0 ls0 ws0">something<span class="_ _2a"> </span>bad<span class="_ _2a"> </span>happened!</div><div class="t m0 x4 h4 y4ba ff5 fs3 fc0 sc0 ls0 ws0">這裡我們可以看到,當一個錯誤(這裡是一個<span class="_ _3"> </span><span class="ffa">ZeroDivisionError</span>)在<span class="_ _3"> </span><span class="ffa">try<span class="_ _a"> </span></span>語句中發生</div><div class="t m0 x5 h4 y4bb ff5 fs3 fc0 sc0 ls0 ws0">時,這個錯誤被捕獲,然後<span class="_ _9"> </span><span class="ffa">except<span class="_ _3"> </span></span>語句會被執行。</div><div class="t m0 x4 h4 y4bc ff5 fs3 fc0 sc0 ls0 ws0">這是我們在一個函數或者其他程式碼中檢測用戶輸入的一種方法。比如我們可能期望有一</div><div class="t m0 x5 h4 y4bd ff5 fs3 fc0 sc0 ls0 ws0">個函式可以捕獲一個被除零異常並回傳一個其他值,也許是一個像<span class="_ _9"> </span><span class="ff15">10</span></div><div class="t m0 x66 h1e y4be ff17 fs4 fc0 sc0 ls0 ws0">100</div><div class="t m0 x1b h4 y4bd ff5 fs3 fc0 sc0 ls0 ws0">一樣很大的數字<span class="ff7">:</span></div><div class="t m0 x11 hc y4bf ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[7]:<span class="_ _2a"> </span><span class="ff11 fc4">def<span class="_ _2a"> </span></span><span class="fc5">safe_divide<span class="fc0">(a,<span class="_ _2a"> </span>b):</span></span></div><div class="t m0 x5d hc y4c0 ff11 fs3 fc4 sc0 ls0 ws0">try<span class="ffa fc0">:</span></div><div class="t m0 x25 hc y4c1 ff11 fs3 fc4 sc0 ls0 ws0">return<span class="_ _2a"> </span><span class="ffa fc0">a<span class="_ _2a"> </span><span class="fc6">/<span class="_ _2a"> </span></span>b</span></div><div class="t m0 x5d hc y4c2 ff11 fs3 fc4 sc0 ls0 ws0">except<span class="ffa fc0">:</span></div><div class="t m0 x25 hc y4c3 ff11 fs3 fc4 sc0 ls0 ws0">return<span class="_ _2a"> </span><span class="ffa fc6">1E100</span></div><div class="t m0 x11 hc y328 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[8]:<span class="_ _2a"> </span><span class="fc0">safe_divide(<span class="fc6">1</span>,<span class="_ _2a"> </span><span class="fc6">2</span>)</span></div><div class="t m0 x11 hc y329 ffa fs3 fcf sc0 ls0 ws0">Out[10]:<span class="_ _2a"> </span><span class="fc0">0.5</span></div><div class="t m0 x11 hc y117 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[9]:<span class="_ _2a"> </span><span class="fc0">safe_divide(<span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">0</span>)</span></div><div class="t m0 x11 hc y119 ffa fs3 fcf sc0 ls0 ws0">Out[9]:<span class="_ _2a"> </span><span class="fc0">1e+100</span></div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf3f" class="pf w0 h0" data-page-no="3f"><div class="pc pc3f w0 h0"><img class="bi x5 y4c4 w1 h3a" alt="" src="images/bg3f.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">56<span class="_ _7c"> </span><span class="ffc">10.<span class="_ _16"> </span><span class="ff6">錯誤和異常</span></span></div><div class="t m0 x4 h4 y42 ff5 fs3 fc0 sc0 ls0 ws0">這段程式碼存在一個微妙的問題,如果發生了其他的異常會出現什麼狀況呢?比如這裡有一</div><div class="t m0 x5 h4 y77 ff5 fs3 fc0 sc0 ls0 ws0">個不符合我們預期的例子:</div><div class="t m0 x11 hc y4c5 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[10]:<span class="_ _2a"> </span><span class="fc0">safe_divide<span class="_ _2a"> </span>(<span class="fc6">1</span>,<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">2</span>'</span>)</span></div><div class="t m0 x11 hc y4c6 ffa fs3 fcf sc0 ls0 ws0">Out[10]:<span class="_ _2a"> </span><span class="fc0">1e+100</span></div><div class="t m0 x4 h4 y4c7 ff5 fs3 fc0 sc0 ls0 ws0">在整數和字串之間進行除法會觸發一個<span class="_ _9"> </span><span class="ffa">TypeError<span class="_ _9"> </span></span>異常,但是我們上面寫的那段過於熱</div><div class="t m0 x5 h4 y4c8 ff5 fs3 fc0 sc0 ls0 ws0">情的程式碼把它當做<span class="_ _9"> </span><span class="ffa">ZeroDivisionError<span class="_ _34"> </span></span>來處理了。<span class="_ _8"></span>因此,<span class="_ _35"></span>最好要明確指出我們要捕獲的異常類別</div><div class="t m0 x5 h4 y4c9 ff5 fs3 fc0 sc0 ls0 ws0">型:</div><div class="t m0 x11 hc y4ca ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[11]:<span class="_ _2a"> </span><span class="ff11 fc4">def<span class="_ _2a"> </span></span><span class="fc5">safe_divide<span class="fc0">(a,<span class="_ _2a"> </span>b):</span></span></div><div class="t m0 x28 hc y4cb ff11 fs3 fc4 sc0 ls0 ws0">try<span class="ffa fc0">:</span></div><div class="t m0 x3a hc y4cc ff11 fs3 fc4 sc0 ls0 ws0">return<span class="_ _2a"> </span><span class="ffa fc0">a<span class="_ _2a"> </span><span class="fc6">/<span class="_ _2a"> </span></span>b</span></div><div class="t m0 x28 hc y4cd ff11 fs3 fc4 sc0 ls0 ws0">except<span class="_ _2a"> </span><span class="fc11">ZeroDivisionError<span class="ffa fc0">:</span></span></div><div class="t m0 x3a hc y4ce ff11 fs3 fc4 sc0 ls0 ws0">return<span class="_ _2a"> </span><span class="ffa fc6">1E100</span></div><div class="t m0 x11 hc y4cf ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[12]:<span class="_ _2a"> </span><span class="fc0">safe_divide(<span class="fc6">1</span>,<span class="_ _2a"> </span><span class="fc6">0</span>)</span></div><div class="t m0 x11 hc y4d0 ffa fs3 fcf sc0 ls0 ws0">Out[12]:<span class="_ _2a"> </span><span class="fc0">1e+100</span></div><div class="t m0 x11 hc y4d1 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[13]:<span class="_ _2a"> </span><span class="fc0">safe_divide(<span class="fc6">1</span>,<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">2</span>'</span>)</span></div><div class="t m0 x24 hc y4d2 ffa fs3 fc0 sc0 ls0 ws0">----------------------------------------------------------------------</div><div class="t m0 x24 hc y4d3 ffa fs3 fc0 sc0 ls0 ws0">TypeError<span class="_ _73"> </span>Traceback<span class="_ _2a"> </span>(most<span class="_ _2a"> </span>recent<span class="_ _2a"> </span>call<span class="_ _2a"> </span>last)</div><div class="t m0 x24 hc y4d4 ffa fs3 fc0 sc0 ls0 ws0"><ipython-input-15-2331af6a0acf><span class="_ _2a"> </span>in<span class="_ _2a"> </span><module>()</div><div class="t m0 x29 hc y4d5 ffa fs3 fc0 sc0 ls0 ws0">----><span class="_ _2a"> </span>1<span class="_ _2a"> </span>safe_divide(1,<span class="_ _2a"> </span><span class="ff12">'</span>2<span class="ff12">'</span>)</div><div class="t m0 x24 hc y4d6 ffa fs3 fc0 sc0 ls0 ws0"><ipython-input-13-10b5f0163af8><span class="_ _2a"> </span>in<span class="_ _2a"> </span>safe_divide(a,<span class="_ _2a"> </span>b)</div><div class="t m0 x64 hc y4d7 ffa fs3 fc0 sc0 ls0 ws0">1<span class="_ _2a"> </span>def<span class="_ _2a"> </span>safe_divide(a,<span class="_ _2a"> </span>b):</div><div class="t m0 x64 hc y4d8 ffa fs3 fc0 sc0 ls0 ws0">2<span class="_ _4f"> </span>try:</div><div class="t m0 x29 hc y4d9 ffa fs3 fc0 sc0 ls0 ws0">----><span class="_ _2a"> </span>3<span class="_ _47"> </span>return<span class="_ _2a"> </span>a<span class="_ _2a"> </span>/<span class="_ _2a"> </span>b</div><div class="t m0 x64 hc y4da ffa fs3 fc0 sc0 ls0 ws0">4<span class="_ _4f"> </span>except<span class="_ _2a"> </span>ZeroDivisionError:</div><div class="t m0 x64 hc y4db ffa fs3 fc0 sc0 ls0 ws0">5<span class="_ _47"> </span>return<span class="_ _2a"> </span>1E100</div><div class="t m0 x24 hc y4dc ffa fs3 fc0 sc0 ls0 ws0">TypeError:<span class="_ _2a"> </span>unsupported<span class="_ _2a"> </span>operand<span class="_ _2a"> </span>type(s)<span class="_ _2a"> </span>for<span class="_ _2a"> </span>/:<span class="_ _2a"> </span><span class="ff12">'</span>int<span class="ff12">'<span class="_ _2a"> </span></span>and<span class="_ _2a"> </span><span class="ff12">'</span>str<span class="ff12">'</span></div><div class="t m0 x4 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">現在我們只能捕獲被除零異常,讓其他型別的異常都原封不動地傳出去。</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf40" class="pf w0 h0" data-page-no="40"><div class="pc pc40 w0 h0"><img class="bi x5 y1bd w1 h3b" alt="" src="images/bg40.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">10.3.<span class="_ _16"> </span><span class="ff6">丟擲異常:<span class="ff19">RAISE<span class="_ _80"> </span><span class="ff7">57</span></span></span></div><div class="t m0 x5 h8 ybb ff1 fs1 fc0 sc0 ls0 ws0">10.3<span class="_ _39"> </span><span class="ff2">丟擲異常:<span class="ff11">raise</span></span></div><div class="t m0 x4 h4 ybc ff5 fs3 fc0 sc0 ls0 ws0">我們已經看到了在使用<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>進行程式設計時獲得異常資訊是多麼寶貴。在你編寫的程式碼中</div><div class="t m0 x5 h4 ybd ff5 fs3 fc0 sc0 ls0 ws0">利用異常資訊同樣非常有幫助,<span class="_ _8"></span>因為這樣使用你程式碼的使用者<span class="_ _8"></span>(最重要的是你自己!<span class="_ _33"></span>)<span class="_ _4"></span>能弄清導致</div><div class="t m0 x5 h4 ybe ff5 fs3 fc0 sc0 ls0 ws0">錯誤的根因。</div><div class="t m0 x4 h4 ybf ff5 fs3 fc0 sc0 ls0 ws0">你可以通過使用<span class="_ _9"> </span><span class="ffa">raise<span class="_ _3"> </span></span>語句來自己丟擲異常,比如:</div><div class="t m0 x11 hc y4dd ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[14]:<span class="_ _2a"> </span><span class="ff11 fc4">raise<span class="_ _2a"> </span><span class="fc11">RuntimeError</span></span><span class="fc0">(<span class="fcc">"my<span class="_ _2a"> </span>error<span class="_ _2a"> </span>message"</span>)</span></div><div class="t m0 x24 hc y4de ffa fs3 fc0 sc0 ls0 ws0">----------------------------------------------------------------------</div><div class="t m0 x24 hc y4df ffa fs3 fc0 sc0 ls0 ws0">RuntimeError<span class="_ _81"> </span>Traceback<span class="_ _2a"> </span>(most<span class="_ _2a"> </span>recent<span class="_ _2a"> </span>call<span class="_ _2a"> </span>last)</div><div class="t m0 x24 hc y4e0 ffa fs3 fc0 sc0 ls0 ws0"><ipython-input-16-c6a4c1ed2f34><span class="_ _2a"> </span>in<span class="_ _2a"> </span><module>()</div><div class="t m0 x29 hc y4e1 ffa fs3 fc0 sc0 ls0 ws0">----><span class="_ _2a"> </span>1<span class="_ _2a"> </span>raise<span class="_ _2a"> </span>RuntimeError("my<span class="_ _2a"> </span>error<span class="_ _2a"> </span>message")</div><div class="t m0 x24 hc y4e2 ffa fs3 fc0 sc0 ls0 ws0">RuntimeError:<span class="_ _2a"> </span>my<span class="_ _2a"> </span>error<span class="_ _2a"> </span>message</div><div class="t m0 x4 h4 y4e3 ff5 fs3 fc0 sc0 ls0 ws0">這裡使用這個例子很合適,讓我們回到我們前面定義的<span class="_ _9"> </span><span class="ffa">fibonacci<span class="_ _3"> </span></span>函式:</div><div class="t m0 x11 hc y4e4 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[15]:<span class="_ _2a"> </span><span class="ff11 fc4">def<span class="_ _2a"> </span></span><span class="fc5">fibonacci<span class="fc0">(N):</span></span></div><div class="t m0 x28 hc y4e5 ffa fs3 fc0 sc0 ls0 ws0">L<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[]</div><div class="t m0 x28 hc y4e6 ffa fs3 fc0 sc0 ls0 ws0">a,<span class="_ _2a"> </span>b<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>0</span>,<span class="_ _2a"> </span><span class="fc6">1</span></div><div class="t m0 x28 hc y4e7 ff11 fs3 fc4 sc0 ls0 ws0">while<span class="_ _2a"> </span><span class="ffa">len<span class="fc0">(L)<span class="_ _2a"> </span><span class="fc6"><<span class="_ _2a"> </span></span>N:</span></span></div><div class="t m0 x3a hc y4e8 ffa fs3 fc0 sc0 ls0 ws0">a,<span class="_ _2a"> </span>b<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>b,<span class="_ _2a"> </span>a<span class="_ _2a"> </span><span class="fc6">+<span class="_ _2a"> </span></span>b</div><div class="t m0 x3a hc y4e9 ffa fs3 fc0 sc0 ls0 ws0">L<span class="fc6">.</span>append(a)</div><div class="t m0 x28 hc y4ea ff11 fs3 fc4 sc0 ls0 ws0">return<span class="_ _2a"> </span><span class="ffa fc0">L</span></div><div class="t m0 x4 h4 y4eb ff5 fs3 fc0 sc0 ls0 ws0">這裡有一個潛在的問題是用戶輸入的值可能為負值。這並不會讓我們的函式現在出現什麼</div><div class="t m0 x5 h4 y4ec ff5 fs3 fc0 sc0 ls0 ws0">錯誤,但是我們可能希望讓使用者知道一個負的<span class="_ _9"> </span><span class="ffa">N<span class="_ _3"> </span></span>是不受支援的。</div><div class="t m0 x4 h4 y4ed ff5 fs3 fc0 sc0 ls0 ws0">根據慣例,由於無效參數值導致的錯誤導致<span class="_ _9"> </span><span class="ffa">ValueError<span class="_ _3"> </span></span>錯誤:</div><div class="t m0 x11 hc y346 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[16]:<span class="_ _2a"> </span><span class="ff11 fc4">def<span class="_ _2a"> </span></span><span class="fc5">fibonacci<span class="fc0">(N):</span></span></div><div class="t m0 x28 hc y4ee ff11 fs3 fc4 sc0 ls0 ws0">if<span class="_ _2a"> </span><span class="ffa fc0">N<span class="_ _2a"> </span><span class="fc6"><<span class="_ _2a"> </span>0</span>:</span></div><div class="t m0 x3a hc y172 ff11 fs3 fc4 sc0 ls0 ws0">raise<span class="_ _2a"> </span><span class="fc11">ValueError<span class="ffa fc0">(<span class="fcc">"N<span class="_ _2a"> </span>must<span class="_ _2a"> </span>be<span class="_ _2a"> </span>non-negative"</span>)</span></span></div><div class="t m0 x28 hc y173 ffa fs3 fc0 sc0 ls0 ws0">L<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[]</div><div class="t m0 x28 hc y174 ffa fs3 fc0 sc0 ls0 ws0">a,<span class="_ _2a"> </span>b<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>0</span>,<span class="_ _2a"> </span><span class="fc6">1</span></div><div class="t m0 x28 hc y175 ff11 fs3 fc4 sc0 ls0 ws0">while<span class="_ _2a"> </span><span class="ffa">len<span class="fc0">(L)<span class="_ _2a"> </span><span class="fc6"><<span class="_ _2a"> </span></span>N:</span></span></div><div class="t m0 x3a hc y176 ffa fs3 fc0 sc0 ls0 ws0">a,<span class="_ _2a"> </span>b<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>b,<span class="_ _2a"> </span>a<span class="_ _2a"> </span><span class="fc6">+<span class="_ _2a"> </span></span>b</div><div class="t m0 x3a hc y98 ffa fs3 fc0 sc0 ls0 ws0">L<span class="fc6">.</span>append(a)</div><div class="t m0 x28 hc y99 ff11 fs3 fc4 sc0 ls0 ws0">return<span class="_ _2a"> </span><span class="ffa fc0">L</span></div><div class="t m0 x11 hc y40 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[17]:<span class="_ _2a"> </span><span class="fc0">fibonacci(<span class="fc6">10</span>)</span></div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf41" class="pf w0 h0" data-page-no="41"><div class="pc pc41 w0 h0"><img class="bi x5 y4ef w1 h3c" alt="" src="images/bg41.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">58<span class="_ _7c"> </span><span class="ffc">10.<span class="_ _16"> </span><span class="ff6">錯誤和異常</span></span></div><div class="t m0 x11 hc y1d7 ffa fs3 fcf sc0 ls0 ws0">Out[17]:<span class="_ _2a"> </span><span class="fc0">[1,<span class="_ _2a"> </span>1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>3,<span class="_ _2a"> </span>5,<span class="_ _2a"> </span>8,<span class="_ _2a"> </span>13,<span class="_ _2a"> </span>21,<span class="_ _2a"> </span>34,<span class="_ _2a"> </span>55]</span></div><div class="t m0 x11 hc y349 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[18]:<span class="_ _2a"> </span><span class="fc0">fibonacci(<span class="fc6">-10</span>)</span></div><div class="t m0 x24 hc y34b ffa fs3 fc0 sc0 ls0 ws0">----------------------------------------------------------------------</div><div class="t m0 x24 hc y34c ffa fs3 fc0 sc0 ls0 ws0">ValueError<span class="_ _7e"> </span>Traceback<span class="_ _2a"> </span>(most<span class="_ _2a"> </span>recent<span class="_ _2a"> </span>call<span class="_ _2a"> </span>last)</div><div class="t m0 x24 hc y4f0 ffa fs3 fc0 sc0 ls0 ws0"><ipython-input-20-3d291499cfa7><span class="_ _2a"> </span>in<span class="_ _2a"> </span><module>()</div><div class="t m0 x29 hc y4f1 ffa fs3 fc0 sc0 ls0 ws0">----><span class="_ _2a"> </span>1<span class="_ _2a"> </span>fibonacci(-10)</div><div class="t m0 x24 hc y4f2 ffa fs3 fc0 sc0 ls0 ws0"><ipython-input-18-01d0cf168d63><span class="_ _2a"> </span>in<span class="_ _2a"> </span>fibonacci(N)</div><div class="t m0 x64 hc y4f3 ffa fs3 fc0 sc0 ls0 ws0">1<span class="_ _2a"> </span>def<span class="_ _2a"> </span>fibonacci(N):</div><div class="t m0 x64 hc y4f4 ffa fs3 fc0 sc0 ls0 ws0">2<span class="_ _4f"> </span>if<span class="_ _2a"> </span>N<span class="_ _2a"> </span><<span class="_ _2a"> </span>0:</div><div class="t m0 x29 hc y4f5 ffa fs3 fc0 sc0 ls0 ws0">----><span class="_ _2a"> </span>3<span class="_ _47"> </span>raise<span class="_ _2a"> </span>ValueError("N<span class="_ _2a"> </span>must<span class="_ _2a"> </span>be<span class="_ _2a"> </span>non-negative")</div><div class="t m0 x64 hc y4f6 ffa fs3 fc0 sc0 ls0 ws0">4<span class="_ _4f"> </span>L<span class="_ _2a"> </span>=<span class="_ _2a"> </span>[]</div><div class="t m0 x64 hc y4f7 ffa fs3 fc0 sc0 ls0 ws0">5<span class="_ _4f"> </span>a,<span class="_ _2a"> </span>b<span class="_ _2a"> </span>=<span class="_ _2a"> </span>0,<span class="_ _2a"> </span>1</div><div class="t m0 x24 hc y4f8 ffa fs3 fc0 sc0 ls0 ws0">ValueError:<span class="_ _2a"> </span>N<span class="_ _2a"> </span>must<span class="_ _2a"> </span>be<span class="_ _2a"> </span>non-negative</div><div class="t m0 x4 h4 y4f9 ff5 fs3 fc0 sc0 ls0 ws0">現在使用者能夠確切地知道為什麼輸入是無效的了,然後可以用<span class="_ _9"> </span><span class="ffa">try<span class="ff7">...</span>except<span class="_ _3"> </span></span>來處理它。</div><div class="t m0 x11 hc y4fa ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[19]:<span class="_ _2a"> </span><span class="fc0">N<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>-10</span></span></div><div class="t m0 x31 hc y4fb ff11 fs3 fc4 sc0 ls0 ws0">try<span class="ffa fc0">:</span></div><div class="t m0 x28 hc y4fc ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fcc">"trying<span class="_ _2a"> </span>this..."</span>)</span></div><div class="t m0 x28 hc y4fd ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(fibonacci(N))</span></div><div class="t m0 x31 hc y4fe ff11 fs3 fc4 sc0 ls0 ws0">except<span class="_ _2a"> </span><span class="fc11">ValueError<span class="ffa fc0">:</span></span></div><div class="t m0 x28 hc y4ff ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fcc">"Bad<span class="_ _2a"> </span>value:<span class="_ _2a"> </span>need<span class="_ _2a"> </span>to<span class="_ _2a"> </span>do<span class="_ _2a"> </span>something<span class="_ _2a"> </span>else"</span>)</span></div><div class="t m0 x11 hc y500 ffa fs3 fc0 sc0 ls0 ws0">trying<span class="_ _2a"> </span>this...</div><div class="t m0 x11 hc y501 ffa fs3 fc0 sc0 ls0 ws0">Bad<span class="_ _2a"> </span>value:<span class="_ _2a"> </span>need<span class="_ _2a"> </span>to<span class="_ _2a"> </span>do<span class="_ _2a"> </span>something<span class="_ _2a"> </span>else</div><div class="t m0 x5 h8 y502 ff1 fs1 fc0 sc0 ls0 ws0">10.4<span class="_ _39"> </span><span class="ff2">深入探究異常</span></div><div class="t m0 x4 h4 y503 ff5 fs3 fc0 sc0 ls0 ws0">簡而言之,我想在這裡提及一些其他可能遇到的概念。我不會詳細介紹這些概念,以及如</div><div class="t m0 x5 h4 y504 ff5 fs3 fc0 sc0 ls0 ws0">何和為什麼使用它們,而只是向你展示語法,之後你可以自己探索更多關於它們的內容。</div><div class="t m0 x5 ha y296 ff1 fs2 fc0 sc0 ls0 ws0">10.4.1<span class="_ _3c"> </span><span class="ff2">訪問錯誤資訊</span></div><div class="t m0 x4 h4 y9a ff5 fs3 fc0 sc0 ls0 ws0">有時在<span class="_ _3"> </span><span class="ffa">try<span class="ff7">...</span>except<span class="_ _3"> </span></span>語句中,你可能會想能夠處理錯誤信息本身。這時你可以使用<span class="_ _3"> </span><span class="ffa">as<span class="_ _3"> </span></span>關</div><div class="t m0 x5 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">鍵字:</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf42" class="pf w0 h0" data-page-no="42"><div class="pc pc42 w0 h0"><img class="bi x5 y40 w1 h11" alt="" src="images/bg42.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">10.4.<span class="_ _16"> </span><span class="ff6">深入探究異常<span class="_ _82"> </span><span class="ff7">59</span></span></div><div class="t m0 x11 hc yfa ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[20]:<span class="_ _2a"> </span><span class="ff11 fc4">try</span><span class="fc0">:</span></div><div class="t m0 x28 hc yfb ffa fs3 fc0 sc0 ls0 ws0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>1<span class="_ _2a"> </span>/<span class="_ _2a"> </span>0</span></div><div class="t m0 x31 hc yfc ff11 fs3 fc4 sc0 ls0 ws0">except<span class="_ _2a"> </span><span class="fc11">ZeroDivisionError<span class="_ _2a"> </span></span>as<span class="_ _2a"> </span><span class="ffa fc0">err:</span></div><div class="t m0 x28 hc y21b ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fcc">"Error<span class="_ _2a"> </span>class<span class="_ _2a"> </span>is:<span class="_ _27"> </span>"</span>,<span class="_ _2a"> </span></span>type<span class="fc0">(err))</span></div><div class="t m0 x28 hc yfd ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fcc">"Error<span class="_ _2a"> </span>message<span class="_ _2a"> </span>is:"</span>,<span class="_ _2a"> </span>err)</span></div><div class="t m0 x11 hc yff ffa fs3 fc0 sc0 ls0 ws0">Error<span class="_ _2a"> </span>class<span class="_ _2a"> </span>is:<span class="_ _49"> </span><class<span class="_ _2a"> </span><span class="ff12">'</span>ZeroDivisionError<span class="ff12">'</span>></div><div class="t m0 x11 hc y100 ffa fs3 fc0 sc0 ls0 ws0">Error<span class="_ _2a"> </span>message<span class="_ _2a"> </span>is:<span class="_ _2a"> </span>division<span class="_ _2a"> </span>by<span class="_ _2a"> </span>zero</div><div class="t m0 x4 h4 y505 ff5 fs3 fc0 sc0 ls0 ws0">通過這種方法,你可以進一步自定義函式的異常處理。</div><div class="t m0 x5 ha y506 ff1 fs2 fc0 sc0 ls0 ws0">10.4.2<span class="_ _3c"> </span><span class="ff2">定義自定義異常</span></div><div class="t m0 x4 h4 y507 ff5 fs3 fc0 sc0 ls0 ws0">除了內建異常型別之外,還可以通過類別繼承來定義自定義異常。例如,如果你想要一個特</div><div class="t m0 x5 h4 y508 ff5 fs3 fc0 sc0 ls0 ws0">殊的<span class="_ _9"> </span><span class="ffa">ValueError</span>,你可以這樣做:</div><div class="t m0 x11 hc y509 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[21]:<span class="_ _2a"> </span><span class="ff11 fc4">class<span class="_ _2a"> </span><span class="fc5">MySpecialError</span></span><span class="fc0">(<span class="ff11 fc11">ValueError</span>):</span></div><div class="t m0 x28 h31 y50a ff11 fs3 fc4 sc0 ls0 ws0">pass</div><div class="t m0 x31 hc y50b ff11 fs3 fc4 sc0 ls0 ws0">raise<span class="_ _2a"> </span><span class="ffa fc0">MySpecialError(<span class="fcc">"here<span class="ff12">'</span>s<span class="_ _2a"> </span>the<span class="_ _2a"> </span>message"</span>)</span></div><div class="t m0 x24 hc y50c ffa fs3 fc0 sc0 ls0 ws0">----------------------------------------------------------------------</div><div class="t m0 x24 hc y50d ffa fs3 fc0 sc0 ls0 ws0">MySpecialError<span class="_ _74"> </span>Traceback<span class="_ _2a"> </span>(most<span class="_ _2a"> </span>recent<span class="_ _2a"> </span>call<span class="_ _2a"> </span>last)</div><div class="t m0 x24 hc y50e ffa fs3 fc0 sc0 ls0 ws0"><ipython-input-23-92c36e04a9d0><span class="_ _2a"> </span>in<span class="_ _2a"> </span><module>()</div><div class="t m0 x64 hc y50f ffa fs3 fc0 sc0 ls0 ws0">2<span class="_ _4f"> </span>pass</div><div class="t m0 x64 hc y510 ffa fs3 fc0 sc0 ls0 ws0">3</div><div class="t m0 x29 hc y511 ffa fs3 fc0 sc0 ls0 ws0">----><span class="_ _2a"> </span>4<span class="_ _2a"> </span>raise<span class="_ _2a"> </span>MySpecialError("here<span class="ff12">'</span>s<span class="_ _2a"> </span>the<span class="_ _2a"> </span>message")</div><div class="t m0 x24 hc y512 ffa fs3 fc0 sc0 ls0 ws0">MySpecialError:<span class="_ _2a"> </span>here<span class="ff12">'</span>s<span class="_ _2a"> </span>the<span class="_ _2a"> </span>message</div><div class="t m0 x4 h4 y513 ff5 fs3 fc0 sc0 ls0 ws0">這將允許你使用只能捕獲此類別錯誤的<span class="_ _9"> </span><span class="ffa">try<span class="ff7">...</span>except<span class="_ _3"> </span></span>區塊。</div><div class="t m0 x11 hc y514 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[22]:<span class="_ _2a"> </span><span class="ff11 fc4">try</span><span class="fc0">:</span></div><div class="t m0 x28 hc y328 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fcc">"do<span class="_ _2a"> </span>something"</span>)</span></div><div class="t m0 x28 hc y3fe ff11 fs3 fc4 sc0 ls0 ws0">raise<span class="_ _2a"> </span><span class="ffa fc0">MySpecialError(<span class="fcc">"[informative<span class="_ _2a"> </span>error<span class="_ _2a"> </span>message<span class="_ _2a"> </span>here]"</span>)</span></div><div class="t m0 x31 hc y329 ff11 fs3 fc4 sc0 ls0 ws0">except<span class="_ _2a"> </span><span class="ffa fc0">MySpecialError:</span></div><div class="t m0 x28 hc y21a ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fcc">"do<span class="_ _2a"> </span>something<span class="_ _2a"> </span>else"</span>)</span></div><div class="t m0 x11 hc y118 ffa fs3 fc0 sc0 ls0 ws0">do<span class="_ _2a"> </span>something</div><div class="t m0 x11 hc y119 ffa fs3 fc0 sc0 ls0 ws0">do<span class="_ _2a"> </span>something<span class="_ _2a"> </span>else</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf43" class="pf w0 h0" data-page-no="43"><div class="pc pc43 w0 h0"><img class="bi x5 y515 w1 h2f" alt="" src="images/bg43.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">60<span class="_ _7c"> </span><span class="ffc">10.<span class="_ _16"> </span><span class="ff6">錯誤和異常</span></span></div><div class="t m0 x4 h4 y42 ff5 fs3 fc0 sc0 ls0 ws0">在編寫過更多自定義程式碼時,你可能會發現這很有用。</div><div class="t m0 x5 h13 y11c ff1 fs1 fc0 sc0 ls0 ws0">10.5<span class="_ _39"> </span><span class="ff11">try</span>...<span class="ff11">except</span>...<span class="ff11">else</span>...<span class="ff11">finally</span></div><div class="t m0 x4 h4 y11d ff5 fs3 fc0 sc0 ls0 ws0">除了<span class="_ _3"> </span><span class="ffa">try<span class="_ _9"> </span></span>和<span class="_ _3"> </span><span class="ffa">except<span class="_ _3"> </span></span>之外,你可以使用<span class="_ _3"> </span><span class="ffa">else<span class="_ _9"> </span></span>和<span class="_ _3"> </span><span class="ffa">finally<span class="_ _3"> </span></span>進一步調整程式碼的異常處理。其</div><div class="t m0 x5 h4 y11e ff5 fs3 fc0 sc0 ls0 ws0">基本結構是這樣的:</div><div class="t m0 x11 hc y516 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[23]:<span class="_ _2a"> </span><span class="ff11 fc4">try</span><span class="fc0">:</span></div><div class="t m0 x5d hc y517 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fcc">"try<span class="_ _2a"> </span>something<span class="_ _2a"> </span>here"</span>)</span></div><div class="t m0 x24 hc y518 ff11 fs3 fc4 sc0 ls0 ws0">except<span class="ffa fc0">:</span></div><div class="t m0 x5d hc y519 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fcc">"this<span class="_ _2a"> </span>happens<span class="_ _2a"> </span>only<span class="_ _2a"> </span>if<span class="_ _2a"> </span>it<span class="_ _2a"> </span>fails"</span>)</span></div><div class="t m0 x24 hc y51a ff11 fs3 fc4 sc0 ls0 ws0">else<span class="ffa fc0">:</span></div><div class="t m0 x5d hc y51b ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fcc">"this<span class="_ _2a"> </span>happens<span class="_ _2a"> </span>only<span class="_ _2a"> </span>if<span class="_ _2a"> </span>it<span class="_ _2a"> </span>succeeds"</span>)</span></div><div class="t m0 x24 hc y51c ff11 fs3 fc4 sc0 ls0 ws0">finally<span class="ffa fc0">:</span></div><div class="t m0 x5d hc y51d ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fcc">"this<span class="_ _2a"> </span>happens<span class="_ _2a"> </span>no<span class="_ _2a"> </span>matter<span class="_ _2a"> </span>what"</span>)</span></div><div class="t m0 x11 hc y51e ffa fs3 fc0 sc0 ls0 ws0">try<span class="_ _2a"> </span>something<span class="_ _2a"> </span>here</div><div class="t m0 x11 hc y51f ffa fs3 fc0 sc0 ls0 ws0">this<span class="_ _2a"> </span>happens<span class="_ _2a"> </span>only<span class="_ _2a"> </span>if<span class="_ _2a"> </span>it<span class="_ _2a"> </span>succeeds</div><div class="t m0 x11 hc y520 ffa fs3 fc0 sc0 ls0 ws0">this<span class="_ _2a"> </span>happens<span class="_ _2a"> </span>no<span class="_ _2a"> </span>matter<span class="_ _2a"> </span>what</div><div class="t m0 x4 h4 y521 ff5 fs3 fc0 sc0 ls0 ws0">這裡的<span class="_ _34"> </span><span class="ffa">else<span class="_ _34"> </span></span>的功能很清楚,<span class="_ _40"></span>但是<span class="_ _34"> </span><span class="ffa">finally<span class="_ _34"> </span></span>的意思是什麼?<span class="_ _3e"></span>其實無論發生什麼,<span class="_ _40"></span><span class="ffa">finally<span class="_ _34"> </span><span class="ff5">子</span></span></div><div class="t m0 x5 h4 y522 ff5 fs3 fc0 sc0 ls0 ws0">句始終會被執行,我通常認為它是在操作完成後用來做某種清理工作的。</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf44" class="pf w0 h0" data-page-no="44"><div class="pc pc44 w0 h0"><img class="bi x5 y523 w1 h3d" alt="" src="images/bg44.png"/><div class="t m0 x0 h1 y3 ff2 fs0 fc0 sc0 ls0 ws0">第<span class="_ _0"> </span><span class="ff1">11<span class="_ _0"> </span></span>章<span class="_ _32"> </span>迭代器</div><div class="t m0 x4 h4 y4 ff5 fs3 fc0 sc0 ls0 ws0">在資料分析中一個很重要的應用是將資料以一種統一的方式重復地進行資料處理。比如</div><div class="t m0 x5 h4 y5 ff5 fs3 fc0 sc0 ls0 ws0">對於一個包含人名的表格,將所有名字分割為姓和名;或者將一個包含日期的表格轉換為某</div><div class="t m0 x5 h4 y6 ff5 fs3 fc0 sc0 ls0 ws0">種標準的格式。對於這類別問題<span class="_ _3"> </span><span class="ff7">Python<span class="_ _9"> </span></span>中的解決方法是採用<span class="ff6">迭代器</span>(<span class="ff7">iterator</span>)語法。我們已經</div><div class="t m0 x5 h4 y7 ff5 fs3 fc0 sc0 ls0 ws0">在<span class="_ _9"> </span><span class="ffa">range<span class="_ _3"> </span></span>迭代器中見過它了:</div><div class="t m0 x11 hc y524 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[1]:<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span><span class="fc0">i<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span><span class="fc4">range</span>(<span class="fc6">10</span>):</span></div><div class="t m0 x5d hc y525 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(i,<span class="_ _2a"> </span>end<span class="fc6">=<span class="ff12 fcc">'<span class="_ _2a"> </span>'</span></span>)</span></div><div class="t m0 x11 hc y526 ffa fs3 fc0 sc0 ls0 ws0">0<span class="_ _2a"> </span>1<span class="_ _2a"> </span>2<span class="_ _2a"> </span>3<span class="_ _2a"> </span>4<span class="_ _2a"> </span>5<span class="_ _2a"> </span>6<span class="_ _2a"> </span>7<span class="_ _2a"> </span>8<span class="_ _2a"> </span>9</div><div class="t m0 x4 h4 y527 ff5 fs3 fc0 sc0 ls0 ws0">在本章中我們將深入討論迭代器。<span class="_ _2"></span>在<span class="_ _9"> </span><span class="ff7">Python<span class="_ _9"> </span>3<span class="_ _9"> </span></span>中,<span class="_ _8"></span><span class="ffa">range<span class="_ _34"> </span><span class="ff5">產生的不是一個列表,<span class="_ _8"></span>而是一個</span></span></div><div class="t m0 x5 h4 y528 ff5 fs3 fc0 sc0 ls0 ws0">稱為迭代器的物件。掌握它的工作原理將幫助你理解許多非常有用的<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>功能與特性。</div><div class="t m0 x5 h8 y529 ff1 fs1 fc0 sc0 ls0 ws0">11.1<span class="_ _39"> </span><span class="ff2">遍歷列表</span></div><div class="t m0 x4 h4 y52a ff5 fs3 fc0 sc0 ls0 ws0">使用迭代器最簡單的場景莫過於對列表進行迭代。考慮下列程式碼:</div><div class="t m0 x11 hc y52b ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[2]:<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span><span class="fc0">value<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span>[<span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">4</span>,<span class="_ _2a"> </span><span class="fc6">6</span>,<span class="_ _2a"> </span><span class="fc6">8</span>,<span class="_ _2a"> </span><span class="fc6">10</span>]:</span></div><div class="t m0 x5d h6 y52c ff14 fs3 fca sc0 ls0 ws0">#<span class="_ _2a"> </span><span class="ff6">做其他一些操作</span></div><div class="t m0 x5d hc y52d ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(value<span class="_ _2a"> </span><span class="fc6">+<span class="_ _2a"> </span>1</span>,<span class="_ _2a"> </span>end<span class="fc6">=<span class="ff12 fcc">'<span class="_ _2a"> </span>'</span></span>)</span></div><div class="t m0 x11 hc y52e ffa fs3 fc0 sc0 ls0 ws0">3<span class="_ _2a"> </span>5<span class="_ _2a"> </span>7<span class="_ _2a"> </span>9<span class="_ _2a"> </span>11</div><div class="t m0 x4 h4 y52f ff5 fs3 fc0 sc0 ls0 ws0">熟<span class="_ _4a"></span>悉<span class="_ _55"></span>的<span class="_ _10"> </span><span class="ff7">“<span class="ffa">for<span class="_ _10"> </span>x<span class="_ _2a"> </span>in<span class="_ _2a"> </span>y</span>”<span class="_ _10"> </span></span>語<span class="_ _4a"></span>法<span class="_ _4a"></span>可<span class="_ _55"> </span>以<span class="_ _4a"></span>讓<span class="_ _55"></span>我<span class="_ _4a"></span>們<span class="_ _55"></span>我<span class="_ _4a"></span>們<span class="_ _4a"></span>對列<span class="_ _55"> </span>表<span class="_ _4a"></span>中<span class="_ _55"> </span>的<span class="_ _4a"></span>每<span class="_ _55"> </span>個<span class="_ _4a"></span>值<span class="_ _55"> </span>進<span class="_ _4a"></span>行<span class="_ _55"></span>某<span class="_ _4a"></span>種<span class="_ _4a"></span>重<span class="_ _55"> </span>復<span class="_ _4a"></span>的<span class="_ _55"> </span>操<span class="_ _4a"></span>作。</div><div class="t m0 x5 h4 y530 ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _a"> </span><span class="ff5">語法與其英文描述(</span>“<span class="ff8">for<span class="_ _a"> </span>[e<span class="_ _8"></span>ach]<span class="_ _13"> </span>value<span class="_ _13"> </span>in<span class="_ _13"> </span>[the]<span class="_ _13"> </span>list<span class="ff7">’’<span class="ff5">)如此接近的特點使得<span class="_ _a"> </span></span>Python<span class="_ _a"> </span><span class="ff5">語</span></span></span></div><div class="t m0 x5 h4 y531 ff5 fs3 fc0 sc0 ls0 ws0">法非常符合直覺並且易學、易用。但是,在這裡表面上的行為並不是<span class="ff2">真正</span>發生的情況。當你</div><div class="t m0 x5 h4 y532 ff5 fs3 fc0 sc0 ls0 ws0">寫下類別似<span class="_ _a"> </span><span class="ff7">“<span class="ffa">for<span class="_ _2a"> </span>val<span class="_ _2a"> </span>in<span class="_ _2a"> </span>L</span>’’<span class="_ _a"> </span></span>這樣的程式碼時,<span class="ff7">Python<span class="_ _a"> </span></span>直譯器會檢查它是否包含一個<span class="ff6">迭代器接口</span></div><div class="t m0 x30 h4 y533 ff5 fs3 fc0 sc0 ls0 ws0">(<span class="ff7">iterator<span class="_ _9"> </span>interface</span>)<span class="_ _41"></span>。你可以通過內建的<span class="_ _3"> </span><span class="ffa">iter<span class="_ _9"> </span></span>函式進行檢查:</div><div class="t m0 x11 hc y534 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[3]:<span class="_ _2a"> </span><span class="fc4">iter<span class="fc0">([<span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">4</span>,<span class="_ _2a"> </span><span class="fc6">6</span>,<span class="_ _2a"> </span><span class="fc6">8</span>,<span class="_ _2a"> </span><span class="fc6">10</span>])</span></span></div><div class="t m0 x11 hc y535 ffa fs3 fcf sc0 ls0 ws0">Out[3]:<span class="_ _2a"> </span><span class="fc0"><list_iterator<span class="_ _2a"> </span>at<span class="_ _2a"> </span>0x104722400></span></div><div class="t m0 x4 h4 y9a ff5 fs3 fc0 sc0 ls0 ws0">正是這個迭代器物件提供了<span class="_ _3"> </span><span class="ffa">for<span class="_ _3"> </span></span>迴圈需要的功能。這個<span class="_ _3"> </span><span class="ffa">iter<span class="_ _3"> </span></span>物件是一個容器,使你能訪</div><div class="t m0 x5 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">問下一個物件(只要它是合法的)<span class="_ _33"></span>。你可以通過內建的<span class="_ _9"> </span><span class="ffa">next<span class="_ _3"> </span></span>函式檢視:</div><div class="t m0 x2f h7 y20 ff7 fs3 fc0 sc0 ls0 ws0">61</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf45" class="pf w0 h0" data-page-no="45"><div class="pc pc45 w0 h0"><img class="bi x5 y536 w1 h3e" alt="" src="images/bg45.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">62<span class="_ _83"> </span><span class="ffc">11.<span class="_ _16"> </span><span class="ff6">迭代器</span></span></div><div class="t m0 x11 hc yfa ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[4]:<span class="_ _2a"> </span><span class="fc0">I<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="fc4">iter</span></span>([<span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">4</span>,<span class="_ _2a"> </span><span class="fc6">6</span>,<span class="_ _2a"> </span><span class="fc6">8</span>,<span class="_ _2a"> </span><span class="fc6">10</span>])</span></div><div class="t m0 x11 hc yfc ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[5]:<span class="_ _2a"> </span><span class="fc4">print<span class="fc0">(</span>next<span class="fc0">(I))</span></span></div><div class="t m0 x11 hc yfd ffa fs3 fc0 sc0 ls0 ws0">2</div><div class="t m0 x11 hc yff ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[6]:<span class="_ _2a"> </span><span class="fc4">print<span class="fc0">(</span>next<span class="fc0">(I))</span></span></div><div class="t m0 x11 hc y101 ffa fs3 fc0 sc0 ls0 ws0">4</div><div class="t m0 x11 hc y102 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[7]:<span class="_ _2a"> </span><span class="fc4">print<span class="fc0">(</span>next<span class="fc0">(I))</span></span></div><div class="t m0 x11 hc y537 ffa fs3 fc0 sc0 ls0 ws0">6</div><div class="t m0 x4 h4 y538 ff5 fs3 fc0 sc0 ls0 ws0">你可能覺得這樣“迂迴”的操作過於複雜,但事實證明這是非常有用的。因為這樣的操作</div><div class="t m0 x5 h4 y539 ff5 fs3 fc0 sc0 ls0 ws0">允許<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>像列表一樣對待<span class="ff2">實際上不是列表的物件</span>。下面我們將會看到這樣的例子。</div><div class="t m0 x5 h8 y53a ff1 fs1 fc0 sc0 ls0 ws0">11.2<span class="_ _39"> </span><span class="ff2">不總是列表的列表:<span class="ff11">range()</span></span></div><div class="t m0 x4 h4 y53b ff5 fs3 fc0 sc0 ls0 ws0">可能這種<span class="_ _2"></span>“迂迴”<span class="_ _2"></span>迭代最常見的例子就是<span class="_ _9"> </span><span class="ff7">Python<span class="_ _9"> </span>3<span class="_ _34"> </span></span>中的<span class="_ _9"> </span><span class="ffa">range()<span class="_ _9"> </span></span>函式<span class="_ _8"></span>(在<span class="_ _9"> </span><span class="ff7">Python<span class="_ _9"> </span>2<span class="_ _9"> </span></span>中這</div><div class="t m0 x5 h4 y53c ff5 fs3 fc0 sc0 ls0 ws0">個函式命名為<span class="_ _9"> </span><span class="ffa">xrange()</span>)<span class="_ _33"></span>。這個函式並不回傳一個列表,而是一個特殊的<span class="_ _3"> </span><span class="ffa">range()<span class="_ _9"> </span></span>物件:</div><div class="t m0 x11 hc y53d ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[8]:<span class="_ _2a"> </span><span class="fc4">range<span class="fc0">(<span class="fc6">10</span>)</span></span></div><div class="t m0 x11 hc y53e ffa fs3 fcf sc0 ls0 ws0">Out[8]:<span class="_ _2a"> </span><span class="fc0">range(0,<span class="_ _2a"> </span>10)</span></div><div class="t m0 x4 h4 y53f ffa fs3 fc0 sc0 ls0 ws0">range<span class="_ _9"> </span><span class="ff5">像一個列表一樣暴露了一個迭代器介面:</span></div><div class="t m0 x11 hc y540 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[9]:<span class="_ _2a"> </span><span class="fc4">iter<span class="fc0">(</span>range<span class="fc0">(<span class="fc6">10</span>))</span></span></div><div class="t m0 x11 hc y541 ffa fs3 fcf sc0 ls0 ws0">Out[9]:<span class="_ _2a"> </span><span class="fc0"><range_iterator<span class="_ _2a"> </span>at<span class="_ _2a"> </span>0x1045a1810></span></div><div class="t m0 x4 h4 y542 ff5 fs3 fc0 sc0 ls0 ws0">所以<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span><span class="ff2">假裝按照</span></span>它是一個列表一樣對待它:</div><div class="t m0 x11 hc y543 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[10]:<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span><span class="fc0">i<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span><span class="fc4">range</span>(<span class="fc6">10</span>):</span></div><div class="t m0 x28 hc y544 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(i,<span class="_ _2a"> </span>end<span class="fc6">=<span class="ff12 fcc">'<span class="_ _2a"> </span>'</span></span>)</span></div><div class="t m0 x11 hc y545 ffa fs3 fc0 sc0 ls0 ws0">0<span class="_ _2a"> </span>1<span class="_ _2a"> </span>2<span class="_ _2a"> </span>3<span class="_ _2a"> </span>4<span class="_ _2a"> </span>5<span class="_ _2a"> </span>6<span class="_ _2a"> </span>7<span class="_ _2a"> </span>8<span class="_ _2a"> </span>9</div><div class="t m0 x4 h4 y98 ff5 fs3 fc0 sc0 ls0 ws0">這樣迭代的好處是,<span class="ff2">整個列表並沒有(明確地)得到完全建立</span>。我們可以通過一個範圍計</div><div class="t m0 x5 h4 y99 ff5 fs3 fc0 sc0 ls0 ws0">算的例子看到這一點。在下面這個例子中,如果我們把列表中的每一個元素都建立出來(完全</div><div class="t m0 x5 h4 y9a ff5 fs3 fc0 sc0 ls0 ws0">實體化這個列表)<span class="_ _33"></span>,<span class="_ _8"></span>系統記憶體將被全部吞噬(注意到在<span class="_ _9"> </span><span class="ff7">Python<span class="_ _9"> </span>2<span class="_ _9"> </span></span>中<span class="_ _3"> </span><span class="ffa">range<span class="_ _34"> </span></span>建立了一個列表,因</div><div class="t m0 x5 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">此在<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span>2<span class="_ _9"> </span></span>中執行下面的程式碼可能導致不好的事情發生)<span class="_ _33"></span>:</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf46" class="pf w0 h0" data-page-no="46"><div class="pc pc46 w0 h0"><img class="bi x5 y40 w1 h11" alt="" src="images/bg46.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">11.3.<span class="_ _16"> </span><span class="ff6">實用迭代器介紹<span class="_ _84"> </span><span class="ff7">63</span></span></div><div class="t m0 x11 hc yfa ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[11]:<span class="_ _2a"> </span><span class="fc0">N<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>10<span class="_ _2a"> </span>**<span class="_ _2a"> </span>12</span></span></div><div class="t m0 x31 hc yfb ff11 fs3 fc4 sc0 ls0 ws0">for<span class="_ _2a"> </span><span class="ffa fc0">i<span class="_ _2a"> </span></span><span class="fcb">in<span class="_ _2a"> </span></span><span class="ffa">range<span class="fc0">(N):</span></span></div><div class="t m0 x28 hc yfc ff11 fs3 fc4 sc0 ls0 ws0">if<span class="_ _2a"> </span><span class="ffa fc0">i<span class="_ _2a"> </span><span class="fc6">>=<span class="_ _2a"> </span>10</span>:<span class="_ _2a"> </span></span>break</div><div class="t m0 x28 hc y21b ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(i,<span class="_ _2a"> </span>end<span class="fc6">=<span class="ff12 fcc">'<span class="ffa">,<span class="_ _2a"> </span></span>'</span></span>)</span></div><div class="t m0 x11 hc yfe ffa fs3 fc0 sc0 ls0 ws0">0,<span class="_ _2a"> </span>1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>3,<span class="_ _2a"> </span>4,<span class="_ _2a"> </span>5,<span class="_ _2a"> </span>6,<span class="_ _2a"> </span>7,<span class="_ _2a"> </span>8,<span class="_ _2a"> </span>9,</div><div class="t m0 x4 h4 y546 ff5 fs3 fc0 sc0 ls0 ws0">如果<span class="_ _9"> </span><span class="ffa">range<span class="_ _9"> </span></span>真的建立了一個包含<span class="_ _9"> </span><span class="ff15">1<span class="_ _9"> </span></span>萬億個值的列表,<span class="_ _8"></span>它將佔用幾十<span class="_ _9"> </span><span class="ff7">TB<span class="_ _9"> </span></span>的機器記憶體!<span class="_ _8"></span>這是</div><div class="t m0 x5 h4 y547 ff5 fs3 fc0 sc0 ls0 ws0">一個巨大的浪費,因為我們實際上並沒有用到前<span class="_ _9"> </span><span class="ff15">10<span class="_ _3"> </span></span>個值之外的所有東西!</div><div class="t m0 x4 h4 y548 ff5 fs3 fc0 sc0 ls0 ws0">事實上,這裡根本沒有理由讓我們對<span class="ff2">整個列表</span>進行迭代。<span class="ff7">Python<span class="_ _3"> </span></span>的<span class="_ _3"> </span><span class="ffa">itertools<span class="_ _3"> </span></span>函式庫包含了</div><div class="t m0 x5 h4 y549 ff5 fs3 fc0 sc0 ls0 ws0">一個<span class="_ _9"> </span><span class="ffa">count<span class="_ _3"> </span></span>函式,這個函式起到了一個無窮<span class="_ _9"> </span><span class="ffa">range<span class="_ _9"> </span></span>的作用:</div><div class="t m0 x11 hc y54a ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[12]:<span class="_ _2a"> </span><span class="ff11 fc4">from<span class="_ _2a"> </span><span class="fc5">itertools<span class="_ _2a"> </span></span>import<span class="_ _2a"> </span></span><span class="fc0">count</span></div><div class="t m0 x31 hc y54b ff11 fs3 fc4 sc0 ls0 ws0">for<span class="_ _2a"> </span><span class="ffa fc0">i<span class="_ _2a"> </span></span><span class="fcb">in<span class="_ _2a"> </span><span class="ffa fc0">count():</span></span></div><div class="t m0 x28 hc y54c ff11 fs3 fc4 sc0 ls0 ws0">if<span class="_ _2a"> </span><span class="ffa fc0">i<span class="_ _2a"> </span><span class="fc6">>=<span class="_ _2a"> </span>10</span>:</span></div><div class="t m0 x3a h31 y54d ff11 fs3 fc4 sc0 ls0 ws0">break</div><div class="t m0 x28 hc y54e ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(i,<span class="_ _2a"> </span>end<span class="fc6">=<span class="ff12 fcc">'<span class="ffa">,<span class="_ _2a"> </span></span>'</span></span>)</span></div><div class="t m0 x11 hc y54f ffa fs3 fc0 sc0 ls0 ws0">0,<span class="_ _2a"> </span>1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>3,<span class="_ _2a"> </span>4,<span class="_ _2a"> </span>5,<span class="_ _2a"> </span>6,<span class="_ _2a"> </span>7,<span class="_ _2a"> </span>8,<span class="_ _2a"> </span>9,</div><div class="t m0 x4 h4 y550 ff5 fs3 fc0 sc0 ls0 ws0">如<span class="_ _4a"></span>果<span class="_ _4a"></span>我<span class="_ _4a"></span>們<span class="_ _55"></span>沒<span class="_ _4a"></span>有<span class="_ _4a"></span>在<span class="_ _4a"></span>中<span class="_ _4a"></span>途<span class="_ _55"></span>跳<span class="_ _4a"></span>出<span class="_ _4a"></span>循<span class="_ _4a"></span>環,<span class="_ _55"></span>它<span class="_ _4a"></span>會<span class="_ _4a"></span>一<span class="_ _4a"></span>直<span class="_ _4a"></span>計<span class="_ _55"></span>數<span class="_ _4a"></span>直<span class="_ _4a"></span>到<span class="_ _4a"></span>進<span class="_ _55"></span>程<span class="_ _4a"></span>被<span class="_ _4a"></span>手<span class="_ _4a"></span>動<span class="_ _4a"></span>中<span class="_ _55"></span>斷<span class="_ _4a"></span>或<span class="_ _4a"></span>結<span class="_ _4a"></span>束<span class="_ _55"></span>(比<span class="_ _4a"></span>如<span class="_ _4a"></span>通</div><div class="t m0 x5 h4 y551 ff5 fs3 fc0 sc0 ls0 ws0">過<span class="_ _9"> </span><span class="ffa">Ctrl-C</span>)<span class="_ _33"></span>。</div><div class="t m0 x5 h8 y552 ff1 fs1 fc0 sc0 ls0 ws0">11.3<span class="_ _39"> </span><span class="ff2">實用迭代器介紹</span></div><div class="t m0 x4 h4 y553 ff5 fs3 fc0 sc0 ls0 ws0">迭代器語法幾乎普遍用於<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>的內建類型以及資料科學中的一些特定物件,我們將在</div><div class="t m0 x5 h4 y554 ff5 fs3 fc0 sc0 ls0 ws0">後面的部分中討論。這裡我們介紹一些<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>中更有實際價值的迭代器。</div><div class="t m0 x5 h3f y555 ff1 fs2 fc0 sc0 ls0 ws0">11.3.1<span class="_ _3c"> </span><span class="ff11">enumerate</span></div><div class="t m0 x4 h4 y556 ff5 fs3 fc0 sc0 ls0 ws0">通常你不僅需要迭代陣列中的值,還需要同步對索引進行追蹤。你可能會用下面這種方式</div><div class="t m0 x5 h4 y557 ff5 fs3 fc0 sc0 ls0 ws0">去實現:</div><div class="t m0 x11 hc y4c3 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[13]:<span class="_ _2a"> </span><span class="fc0">L<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[<span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">4</span>,<span class="_ _2a"> </span><span class="fc6">6</span>,<span class="_ _2a"> </span><span class="fc6">8</span>,<span class="_ _2a"> </span><span class="fc6">10</span>]</span></div><div class="t m0 x31 hc y514 ff11 fs3 fc4 sc0 ls0 ws0">for<span class="_ _2a"> </span><span class="ffa fc0">i<span class="_ _2a"> </span></span><span class="fcb">in<span class="_ _2a"> </span></span><span class="ffa">range<span class="fc0">(</span>len<span class="fc0">(L)):</span></span></div><div class="t m0 x28 hc y328 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(i,<span class="_ _2a"> </span>L[i])</span></div><div class="t m0 x11 hc y329 ffa fs3 fc0 sc0 ls0 ws0">0<span class="_ _2a"> </span>2</div><div class="t m0 x11 hc y21a ffa fs3 fc0 sc0 ls0 ws0">1<span class="_ _2a"> </span>4</div><div class="t m0 x11 hc y117 ffa fs3 fc0 sc0 ls0 ws0">2<span class="_ _2a"> </span>6</div><div class="t m0 x11 hc y118 ffa fs3 fc0 sc0 ls0 ws0">3<span class="_ _2a"> </span>8</div><div class="t m0 x11 hc y119 ffa fs3 fc0 sc0 ls0 ws0">4<span class="_ _2a"> </span>10</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf47" class="pf w0 h0" data-page-no="47"><div class="pc pc47 w0 h0"><img class="bi x5 y11a w1 h39" alt="" src="images/bg47.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">64<span class="_ _83"> </span><span class="ffc">11.<span class="_ _16"> </span><span class="ff6">迭代器</span></span></div><div class="t m0 x4 h4 y42 ff5 fs3 fc0 sc0 ls0 ws0">儘管上述例子是可行的,但是<span class="_ _3"> </span><span class="ff7">Python<span class="_ _9"> </span></span>提供了一個更簡潔的語法,即通過<span class="_ _3"> </span><span class="ffa">enumerate<span class="_ _9"> </span></span>迭代</div><div class="t m0 x5 h4 y77 ff5 fs3 fc0 sc0 ls0 ws0">器:</div><div class="t m0 x11 hc y558 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[14]:<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span><span class="fc0">i,<span class="_ _2a"> </span>val<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span><span class="fc4">enumerate</span>(L):</span></div><div class="t m0 x28 hc y559 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(i,<span class="_ _2a"> </span>val)</span></div><div class="t m0 x11 hc y55a ffa fs3 fc0 sc0 ls0 ws0">0<span class="_ _2a"> </span>2</div><div class="t m0 x11 hc y55b ffa fs3 fc0 sc0 ls0 ws0">1<span class="_ _2a"> </span>4</div><div class="t m0 x11 hc y55c ffa fs3 fc0 sc0 ls0 ws0">2<span class="_ _2a"> </span>6</div><div class="t m0 x11 hc y55d ffa fs3 fc0 sc0 ls0 ws0">3<span class="_ _2a"> </span>8</div><div class="t m0 x11 hc y55e ffa fs3 fc0 sc0 ls0 ws0">4<span class="_ _2a"> </span>10</div><div class="t m0 x4 h4 y55f ff5 fs3 fc0 sc0 ls0 ws0">在對一個列表中的索引和值進行迭代時,這是一種更加<span class="_ _9"> </span><span class="ff7">Pythonic</span></div><div class="t m0 x66 hb y560 ffe fs4 fc1 sc0 ls0 ws0">1</div><div class="t m0 x67 h4 y55f ff5 fs3 fc0 sc0 ls0 ws0">的方法。</div><div class="t m0 x5 h3f y561 ff1 fs2 fc0 sc0 ls0 ws0">11.3.2<span class="_ _3c"> </span><span class="ff11">zip</span></div><div class="t m0 x4 h4 y562 ff5 fs3 fc0 sc0 ls0 ws0">另外一些情況下,你可能需要對多個列表同時進行迭代。你完全可以像之前我們看到的</div><div class="t m0 x5 h4 y563 ff5 fs3 fc0 sc0 ls0 ws0">不<span class="_ _3"> </span><span class="ff7">Pythonic<span class="_ _3"> </span></span>的方法那樣對列表索引進行迭代,但是更好的方法是使用<span class="_ _3"> </span><span class="ffa">zip<span class="_ _3"> </span></span>迭代器,它將可迭</div><div class="t m0 x5 h4 y564 ff5 fs3 fc0 sc0 ls0 ws0">代物件鏈在一起:</div><div class="t m0 x11 hc y565 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[15]:<span class="_ _2a"> </span><span class="fc0">L<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[<span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">4</span>,<span class="_ _2a"> </span><span class="fc6">6</span>,<span class="_ _2a"> </span><span class="fc6">8</span>,<span class="_ _2a"> </span><span class="fc6">10</span>]</span></div><div class="t m0 x31 hc y566 ffa fs3 fc0 sc0 ls0 ws0">R<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[<span class="fc6">3</span>,<span class="_ _2a"> </span><span class="fc6">6</span>,<span class="_ _2a"> </span><span class="fc6">9</span>,<span class="_ _2a"> </span><span class="fc6">12</span>,<span class="_ _2a"> </span><span class="fc6">15</span>]</div><div class="t m0 x31 hc y567 ff11 fs3 fc4 sc0 ls0 ws0">for<span class="_ _2a"> </span><span class="ffa fc0">lval,<span class="_ _2a"> </span>rval<span class="_ _2a"> </span></span><span class="fcb">in<span class="_ _2a"> </span></span><span class="ffa">zip<span class="fc0">(L,<span class="_ _2a"> </span>R):</span></span></div><div class="t m0 x28 hc y568 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(lval,<span class="_ _2a"> </span>rval)</span></div><div class="t m0 x11 hc y569 ffa fs3 fc0 sc0 ls0 ws0">2<span class="_ _2a"> </span>3</div><div class="t m0 x11 hc y56a ffa fs3 fc0 sc0 ls0 ws0">4<span class="_ _2a"> </span>6</div><div class="t m0 x11 hc y56b ffa fs3 fc0 sc0 ls0 ws0">6<span class="_ _2a"> </span>9</div><div class="t m0 x11 hc y56c ffa fs3 fc0 sc0 ls0 ws0">8<span class="_ _2a"> </span>12</div><div class="t m0 x11 hc y56d ffa fs3 fc0 sc0 ls0 ws0">10<span class="_ _2a"> </span>15</div><div class="t m0 x4 h4 y56e ff5 fs3 fc0 sc0 ls0 ws0">任意數量的可迭代物件都可以被鏈在一起,<span class="_ _8"></span>其中最短的那個列表將決定整個<span class="_ _2a"> </span><span class="ffa">zip<span class="_ _9"> </span></span>迭代器的</div><div class="t m0 x5 h4 y56f ff5 fs3 fc0 sc0 ls0 ws0">長度。</div><div class="t m0 x5 ha y570 ff1 fs2 fc0 sc0 ls0 ws0">11.3.3<span class="_ _3c"> </span><span class="ff11">map<span class="_ _13"> </span><span class="ff2">和<span class="_ _13"> </span></span>filter</span></div><div class="t m0 x4 h4 y571 ffa fs3 fc0 sc0 ls0 ws0">map<span class="_ _9"> </span><span class="ff5">迭代器接受一個函式,並且將它套用到迭代器中每一個值:</span></div><div class="t m0 x11 h6 y572 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[16]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">尋找前<span class="_ _2a"> </span></span>10<span class="_ _2a"> </span><span class="ff6">個平方數</span></span></div><div class="t m0 x31 hc y573 ffa fs3 fc0 sc0 ls0 ws0">square<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="ff11 fc4">lambda<span class="_ _2a"> </span></span></span>x:<span class="_ _2a"> </span>x<span class="_ _2a"> </span><span class="fc6">**<span class="_ _2a"> </span>2</span></div><div class="t m0 x31 hc y574 ff11 fs3 fc4 sc0 ls0 ws0">for<span class="_ _2a"> </span><span class="ffa fc0">val<span class="_ _2a"> </span></span><span class="fcb">in<span class="_ _2a"> </span></span><span class="ffa">map<span class="fc0">(square,<span class="_ _2a"> </span></span>range<span class="fc0">(<span class="fc6">10</span>)):</span></span></div><div class="t m0 x28 hc y575 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(val,<span class="_ _2a"> </span>end<span class="fc6">=<span class="ff12 fcc">'<span class="_ _2a"> </span>'</span></span>)</span></div><div class="t m0 x12 hd y123 fff fs5 fc0 sc0 ls0 ws0">1</div><div class="t m0 xa he ydb ff5 fs6 fc0 sc0 ls0 ws0">譯者注:<span class="_ _43"></span>所謂<span class="_ _60"> </span><span class="ff10">Pythonic<span class="_ _60"> </span></span>的程式碼就是以<span class="_ _60"> </span><span class="ff10">Python<span class="_ _60"> </span></span>的方式寫出簡潔優美的程式碼<span class="_ _2"></span>(在第<span class="_ _60"> </span><span class="ff10 fc1">1<span class="_ _60"> </span></span>章中我們介紹了<span class="_ _35"></span>《<span class="ff10">Python<span class="_ _60"> </span></span>之</div><div class="t m0 x5 he yba ff5 fs6 fc0 sc0 ls0 ws0">禪》<span class="_ _8"></span>,它<span class="_ _4a"> </span>用<span class="_ _13"> </span><span class="ff10">19<span class="_ _13"> </span></span>句<span class="_ _4a"></span>話精<span class="_ _4a"></span>煉地<span class="_ _4a"> </span>介<span class="_ _4a"></span>紹了<span class="_ _13"> </span><span class="ff10">Python<span class="_ _13"> </span></span>的<span class="_ _4a"></span>設計<span class="_ _4a"> </span>思<span class="_ _4a"></span>想)<span class="_ _8"></span>。閱<span class="_ _4a"> </span>讀<span class="_ _4a"></span>並盡<span class="_ _4a"> </span>量<span class="_ _4a"></span>遵守<span class="_ _13"> </span><span class="ff10">PEP<span class="_ _13"> </span>8</span>:<span class="ff10">Python<span class="_ _13"> </span></span>代<span class="_ _4a"></span>碼風<span class="_ _4a"></span>格<span class="_ _4a"></span>指南:</div><div class="t m0 x5 he y40 ff13 fs6 fc2 sc0 ls0 ws0">https://www.python.org/dev/peps/pep-0008/<span class="ff5 fc0">,你的程式碼就足夠<span class="_ _34"> </span><span class="ff10">Pythonic<span class="_ _34"> </span></span>了!</span></div><a class="l" href="#pf47" data-dest-detail='[71,"XYZ",88.59,111.45,null]'><div class="d m1" style="border-style:none;position:absolute;left:402.152000px;bottom:570.134000px;width:4.234000px;height:5.136000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf8" data-dest-detail='[8,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:404.902000px;bottom:98.301000px;width:4.607000px;height:5.674000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="https://www.python.org/dev/peps/pep-0008/"><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:70.007000px;width:193.500000px;height:8.220000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf48" class="pf w0 h0" data-page-no="48"><div class="pc pc48 w0 h0"><img class="bi x5 y1bd w1 h40" alt="" src="images/bg48.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">11.3.<span class="_ _16"> </span><span class="ff6">實用迭代器介紹<span class="_ _84"> </span><span class="ff7">65</span></span></div><div class="t m0 x11 hc y1d7 ffa fs3 fc0 sc0 ls0 ws0">0<span class="_ _2a"> </span>1<span class="_ _2a"> </span>4<span class="_ _2a"> </span>9<span class="_ _2a"> </span>16<span class="_ _2a"> </span>25<span class="_ _2a"> </span>36<span class="_ _2a"> </span>49<span class="_ _2a"> </span>64<span class="_ _2a"> </span>81</div><div class="t m0 x4 h4 y576 ffa fs3 fc0 sc0 ls0 ws0">filter<span class="_ _9"> </span><span class="ff5">迭代器看上去類似,但是它只允許使得過濾函式為<span class="ff2">真</span>的值保留下來:</span></div><div class="t m0 x11 h6 y577 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[17]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">尋找當<span class="_ _2a"> </span></span>x<span class="_ _2a"> </span>%<span class="_ _2a"> </span>2<span class="_ _2a"> </span><span class="ff6">為<span class="_ _2a"> </span></span>0<span class="_ _2a"> </span><span class="ff6">且小於<span class="_ _2a"> </span></span>10<span class="_ _2a"> </span><span class="ff6">的值</span></span></div><div class="t m0 x31 hc y578 ffa fs3 fc0 sc0 ls0 ws0">is_even<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="ff11 fc4">lambda<span class="_ _2a"> </span></span></span>x:<span class="_ _2a"> </span>x<span class="_ _2a"> </span><span class="fc6">%<span class="_ _2a"> </span>2<span class="_ _2a"> </span>==<span class="_ _2a"> </span>0</span></div><div class="t m0 x31 hc y579 ff11 fs3 fc4 sc0 ls0 ws0">for<span class="_ _2a"> </span><span class="ffa fc0">val<span class="_ _2a"> </span></span><span class="fcb">in<span class="_ _2a"> </span></span><span class="ffa">filter<span class="fc0">(is_even,<span class="_ _2a"> </span></span>range<span class="fc0">(<span class="fc6">10</span>)):</span></span></div><div class="t m0 x28 hc y57a ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(val,<span class="_ _2a"> </span>end<span class="fc6">=<span class="ff12 fcc">'<span class="_ _2a"> </span>'</span></span>)</span></div><div class="t m0 x11 hc y57b ffa fs3 fc0 sc0 ls0 ws0">0<span class="_ _2a"> </span>2<span class="_ _2a"> </span>4<span class="_ _2a"> </span>6<span class="_ _2a"> </span>8</div><div class="t m0 x4 h4 y57c ffa fs3 fc0 sc0 ls0 ws0">map<span class="ff5">、</span>filter<span class="_ _10"> </span><span class="ff5">以<span class="_ _4a"></span>及<span class="_ _1"> </span></span>reduce<span class="_ _10"> </span><span class="ff5">函<span class="_ _4a"></span>數<span class="_ _55"></span>(位<span class="_ _4a"></span>於<span class="_ _10"> </span><span class="ff7">Python<span class="_ _1"> </span></span>的<span class="_ _10"> </span></span>functools<span class="_ _1"> </span><span class="ff5">模<span class="_ _55"> </span>組<span class="_ _4a"></span>中)<span class="_ _55"></span>是<span class="ff6">函<span class="_ _4a"></span>數<span class="_ _4a"></span>式<span class="_ _55"> </span>編<span class="_ _4a"></span>程</span></span></div><div class="t m0 x30 h4 y57d ff5 fs3 fc0 sc0 ls0 ws0">(<span class="ff7">functional<span class="_ _3"> </span>programming</span>)風格的基本組成模組。目前在<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>世界中,這種方式暫時還不</div><div class="t m0 x5 h4 y57e ff5 fs3 fc0 sc0 ls0 ws0">是主流,但是仍然有著堅定的支持者(比如<span class="_ _9"> </span><span class="ff7 fc2">pytoolz<span class="_ _3"> </span><span class="ff5">函式庫</span></span>)<span class="_ _33"></span>。</div><div class="t m0 x5 ha y57f ff1 fs2 fc0 sc0 ls0 ws0">11.3.4<span class="_ _3c"> </span><span class="ff2">迭代器作為函式參數</span></div><div class="t m0 x4 h4 y580 ff5 fs3 fc0 sc0 ls0 ws0">我們在第<span class="_ _3"> </span><span class="ff7 fc1">9.4<span class="_ _3"> </span></span>節<span class="fc1">可變參數:<span class="ffa">*args<span class="_ _3"> </span></span>和<span class="_ _3"> </span><span class="ffa">**kwargs<span class="_ _3"> </span></span></span>中看見<span class="_ _2a"> </span><span class="ffa">*args<span class="_ _3"> </span></span>和<span class="_ _3"> </span><span class="ffa">**kwargs<span class="_ _3"> </span></span>可以用來向函</div><div class="t m0 x5 h4 y581 ff5 fs3 fc0 sc0 ls0 ws0">數傳遞序列和字典。事實上,<span class="ffa">*args<span class="_ _9"> </span></span>語法不僅僅適用於序列,同樣適用於任意迭代器:</div><div class="t m0 x11 hc y582 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[18]:<span class="_ _2a"> </span><span class="fc4">print<span class="fc0">(<span class="fc6">*</span></span>range<span class="fc0">(<span class="fc6">10</span>))</span></span></div><div class="t m0 x11 hc y583 ffa fs3 fc0 sc0 ls0 ws0">0<span class="_ _2a"> </span>1<span class="_ _2a"> </span>2<span class="_ _2a"> </span>3<span class="_ _2a"> </span>4<span class="_ _2a"> </span>5<span class="_ _2a"> </span>6<span class="_ _2a"> </span>7<span class="_ _2a"> </span>8<span class="_ _2a"> </span>9</div><div class="t m0 x4 h4 y584 ff5 fs3 fc0 sc0 ls0 ws0">那麼,舉一個例子,我們可以將之前的<span class="_ _9"> </span><span class="ffa">map<span class="_ _3"> </span></span>例子用一個技巧整合進下面的函式呼叫中:</div><div class="t m0 x11 hc y585 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[19]:<span class="_ _2a"> </span><span class="fc4">print<span class="fc0">(<span class="fc6">*</span></span>map<span class="fc0">(</span><span class="ff11">lambda<span class="_ _2a"> </span></span><span class="fc0">x:<span class="_ _2a"> </span>x<span class="_ _2a"> </span><span class="fc6">**<span class="_ _2a"> </span>2</span>,<span class="_ _2a"> </span></span>range<span class="fc0">(<span class="fc6">10</span>)))</span></span></div><div class="t m0 x11 hc y586 ffa fs3 fc0 sc0 ls0 ws0">0<span class="_ _2a"> </span>1<span class="_ _2a"> </span>4<span class="_ _2a"> </span>9<span class="_ _2a"> </span>16<span class="_ _2a"> </span>25<span class="_ _2a"> </span>36<span class="_ _2a"> </span>49<span class="_ _2a"> </span>64<span class="_ _2a"> </span>81</div><div class="t m0 x4 h4 y587 ff5 fs3 fc0 sc0 ls0 ws0">使<span class="_ _4a"></span>用<span class="_ _55"> </span>這<span class="_ _55"></span>個<span class="_ _4a"></span>技<span class="_ _55"> </span>巧,<span class="_ _55"></span>我<span class="_ _4a"></span>們<span class="_ _55"> </span>可<span class="_ _4a"></span>以<span class="_ _55"> </span>回<span class="_ _55"> </span>答<span class="_ _4a"></span>在<span class="_ _10"> </span><span class="ff7">Python<span class="_ _10"> </span></span>初<span class="_ _4a"></span>學<span class="_ _55"> </span>者<span class="_ _55"> </span>論<span class="_ _4a"></span>壇<span class="_ _55"> </span>上<span class="_ _55"></span>一<span class="_ _4a"></span>個<span class="_ _55"> </span>老<span class="_ _55"></span>生<span class="_ _4a"></span>常<span class="_ _55"> </span>談<span class="_ _4a"></span>的<span class="_ _55"> </span>問<span class="_ _55"> </span>題:<span class="_ _4a"></span>為<span class="_ _55"> </span>什</div><div class="t m0 x5 h4 y588 ff5 fs3 fc0 sc0 ls0 ws0">麼<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>中沒有一個<span class="_ _2a"> </span><span class="ffa">unzip()<span class="_ _3"> </span></span>函數恰好執行<span class="_ _3"> </span><span class="ffa">zip()<span class="_ _3"> </span></span>函數相反的操作呢?如果你把自己鎖在</div><div class="t m0 x5 h4 y589 ff5 fs3 fc0 sc0 ls0 ws0">一個漆黑的地方然後思考一會兒這個問題,你可能會意識到與<span class="_ _a"> </span><span class="ffa">zip()<span class="_ _3"> </span></span>相反的函數還是<span class="_ _3"> </span><span class="ffa">zip()</span>!</div><div class="t m0 x5 h4 y58a ff5 fs3 fc0 sc0 ls0 ws0">理解這個問題的關鍵在於<span class="_ _9"> </span><span class="ffa">zip()<span class="_ _3"> </span></span>可以連結任意數量的迭代器或者序列。觀察下列程式碼:</div><div class="t m0 x11 hc y173 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[20]:<span class="_ _2a"> </span><span class="fc0">L1<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>(<span class="fc6">1</span>,<span class="_ _2a"> </span><span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">3</span>,<span class="_ _2a"> </span><span class="fc6">4</span>)</span></div><div class="t m0 x31 hc y174 ffa fs3 fc0 sc0 ls0 ws0">L2<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>(<span class="ff12 fcc">'<span class="ffa">a</span>'</span>,<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">b</span>'</span>,<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">c</span>'</span>,<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">d</span>'</span>)</div><div class="t m0 x11 hc y176 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[21]:<span class="_ _2a"> </span><span class="fc0">z<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="fc4">zip</span></span>(L1,<span class="_ _2a"> </span>L2)</span></div><div class="t m0 x31 hc y98 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fc6">*</span>z)</span></div><div class="t m0 x11 hc y9a ffa fs3 fc0 sc0 ls0 ws0">(1,<span class="_ _2a"> </span><span class="ff12">'</span>a<span class="ff12">'</span>)<span class="_ _2a"> </span>(2,<span class="_ _2a"> </span><span class="ff12">'</span>b<span class="ff12">'</span>)<span class="_ _2a"> </span>(3,<span class="_ _2a"> </span><span class="ff12">'</span>c<span class="ff12">'</span>)<span class="_ _2a"> </span>(4,<span class="_ _2a"> </span><span class="ff12">'</span>d<span class="ff12">'</span>)</div><a class="l" href="https://toolz.readthedocs.org/en/latest/"><div class="d m1" style="border-style:none;position:absolute;left:282.909000px;bottom:525.807000px;width:49.394000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf38" data-dest-detail='[56,"XYZ",72,586.65,null]'><div class="d m1" style="border-style:none;position:absolute;left:141.603000px;bottom:473.671000px;width:13.940000px;height:6.869000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf38" data-dest-detail='[56,"XYZ",72,586.65,null]'><div class="d m1" style="border-style:none;position:absolute;left:170.251000px;bottom:470.560000px;width:147.857000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf49" class="pf w0 h0" data-page-no="49"><div class="pc pc49 w0 h0"><img class="bi x5 ydc w1 h11" alt="" src="images/bg49.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">66<span class="_ _83"> </span><span class="ffc">11.<span class="_ _16"> </span><span class="ff6">迭代器</span></span></div><div class="t m0 x11 hc y1d6 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[22]:<span class="_ _2a"> </span><span class="fc0">z<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="fc4">zip</span></span>(L1,<span class="_ _2a"> </span>L2)</span></div><div class="t m0 x31 hc y1d7 ffa fs3 fc0 sc0 ls0 ws0">new_L1,<span class="_ _2a"> </span>new_L2<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="fc4">zip</span></span>(<span class="fc6">*</span>z)</div><div class="t m0 x31 hc y348 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(new_L1,<span class="_ _2a"> </span>new_L2)</span></div><div class="t m0 x11 hc y34a ffa fs3 fc0 sc0 ls0 ws0">(1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>3,<span class="_ _2a"> </span>4)<span class="_ _2a"> </span>(<span class="ff12">'</span>a<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>b<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>c<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>d<span class="ff12">'</span>)</div><div class="t m0 x4 h4 y58b ff5 fs3 fc0 sc0 ls0 ws0">好<span class="_ _4a"></span>好<span class="_ _4a"></span>思<span class="_ _4a"></span>考<span class="_ _55"></span>一<span class="_ _4a"></span>下<span class="_ _4a"></span>這<span class="_ _4a"></span>個<span class="_ _4a"></span>例<span class="_ _55"></span>子。<span class="_ _4a"></span>如<span class="_ _4a"></span>果<span class="_ _4a"></span>你<span class="_ _55"></span>已<span class="_ _4a"></span>經<span class="_ _4a"></span>理<span class="_ _4a"></span>解<span class="_ _4a"></span>了<span class="_ _55"></span>它<span class="_ _4a"></span>是<span class="_ _4a"></span>如<span class="_ _4a"></span>何<span class="_ _55"></span>工<span class="_ _4a"></span>作<span class="_ _4a"></span>的,<span class="_ _4a"></span>說<span class="_ _4a"></span>明<span class="_ _55"></span>你<span class="_ _4a"></span>已<span class="_ _4a"></span>經<span class="_ _4a"></span>深<span class="_ _55"></span>入<span class="_ _4a"></span>理<span class="_ _4a"></span>解</div><div class="t m0 x5 h4 y58c ff5 fs3 fc0 sc0 ls0 ws0">了<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>的迭代器!</div><div class="t m0 x5 h8 y58d ff1 fs1 fc0 sc0 ls0 ws0">11.4<span class="_ _39"> </span><span class="ff2">專用迭代器:<span class="ff11">itertools</span></span></div><div class="t m0 x4 h4 y58e ffa fs3 fc0 sc0 ls0 ws0">itertools<span class="_ _2a"> </span><span class="ff5">模<span class="_ _76"> </span>組<span class="_ _4b"> </span>包<span class="_ _76"> </span>含<span class="_ _76"> </span>了<span class="_ _76"> </span>一<span class="_ _76"> </span>系<span class="_ _76"> </span>列<span class="_ _76"> </span>有<span class="_ _76"> </span>用<span class="_ _76"> </span>的迭<span class="_ _76"> </span>代<span class="_ _76"> </span>器,<span class="_ _4b"> </span>我<span class="_ _76"> </span>們<span class="_ _76"> </span>之<span class="_ _76"> </span>前<span class="_ _76"> </span>已<span class="_ _76"> </span>經<span class="_ _76"> </span>簡<span class="_ _76"> </span>略<span class="_ _76"> </span>地<span class="_ _76"> </span>介<span class="_ _76"> </span>紹<span class="_ _4b"> </span>了<span class="_ _76"> </span>其<span class="_ _76"> </span>中<span class="_ _76"> </span>無</span></div><div class="t m0 x5 h4 y58f ff5 fs3 fc0 sc0 ls0 ws0">窮<span class="_ _72"> </span><span class="ffa">range<span class="_ _1"> </span></span>迭<span class="_ _4a"></span>代器:<span class="ffa">itertools.count</span>。<span class="_ _4a"></span>探<span class="_ _4a"></span>索這<span class="_ _4a"></span>個<span class="_ _4a"></span>模塊<span class="_ _4a"></span>來<span class="_ _4a"></span>找到<span class="_ _4a"></span>對<span class="_ _4a"></span>你的<span class="_ _4a"></span>程<span class="_ _4a"></span>序有<span class="_ _4a"></span>用<span class="_ _4a"></span>的迭代<span class="_ _4a"></span>器<span class="_ _4a"></span>是非</div><div class="t m0 x5 h4 y590 ff5 fs3 fc0 sc0 ls0 ws0">常值得的。作為一個例子,考慮<span class="_ _3"> </span><span class="ffa">itertools.permutations<span class="_ _3"> </span></span>函式,這個函式在一個序列的<span class="ff6">全排</span></div><div class="t m0 x5 h4 y591 ff6 fs3 fc0 sc0 ls0 ws0">列<span class="ff5">(<span class="ff7">full<span class="_ _9"> </span>permutation</span>)中進行迭代:</span></div><div class="t m0 x11 hc y592 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[23]:<span class="_ _2a"> </span><span class="ff11 fc4">from<span class="_ _2a"> </span><span class="fc5">itertools<span class="_ _2a"> </span></span>import<span class="_ _2a"> </span></span><span class="fc0">permutations</span></div><div class="t m0 x31 hc y593 ffa fs3 fc0 sc0 ls0 ws0">p<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>permutations(<span class="fc4">range</span>(<span class="fc6">3</span>))</div><div class="t m0 x31 hc y594 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fc6">*</span>p)</span></div><div class="t m0 x11 hc y595 ffa fs3 fc0 sc0 ls0 ws0">(0,<span class="_ _2a"> </span>1,<span class="_ _2a"> </span>2)<span class="_ _2a"> </span>(0,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>1)<span class="_ _2a"> </span>(1,<span class="_ _2a"> </span>0,<span class="_ _2a"> </span>2)<span class="_ _2a"> </span>(1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>0)<span class="_ _2a"> </span>(2,<span class="_ _2a"> </span>0,<span class="_ _2a"> </span>1)<span class="_ _2a"> </span>(2,<span class="_ _2a"> </span>1,<span class="_ _2a"> </span>0)</div><div class="t m0 x4 h4 y596 ff5 fs3 fc0 sc0 ls0 ws0">類似地,<span class="ffa">itertools.combinations<span class="_ _9"> </span></span>函式在一個列表中所有不同的<span class="_ _3"> </span><span class="ffd">N<span class="_ _72"> </span></span>組合中進行迭代:</div><div class="t m0 x11 hc y597 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[24]:<span class="_ _2a"> </span><span class="ff11 fc4">from<span class="_ _2a"> </span><span class="fc5">itertools<span class="_ _2a"> </span></span>import<span class="_ _2a"> </span></span><span class="fc0">combinations</span></div><div class="t m0 x31 hc y598 ffa fs3 fc0 sc0 ls0 ws0">c<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>combinations(<span class="fc4">range</span>(<span class="fc6">4</span>),<span class="_ _2a"> </span><span class="fc6">2</span>)</div><div class="t m0 x31 hc y599 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fc6">*</span>c)</span></div><div class="t m0 x11 hc y59a ffa fs3 fc0 sc0 ls0 ws0">(0,<span class="_ _2a"> </span>1)<span class="_ _2a"> </span>(0,<span class="_ _2a"> </span>2)<span class="_ _2a"> </span>(0,<span class="_ _2a"> </span>3)<span class="_ _2a"> </span>(1,<span class="_ _2a"> </span>2)<span class="_ _2a"> </span>(1,<span class="_ _2a"> </span>3)<span class="_ _2a"> </span>(2,<span class="_ _2a"> </span>3)</div><div class="t m0 x4 h4 y59b ff5 fs3 fc0 sc0 ls0 ws0">與之有一點兒關聯的是<span class="_ _9"> </span><span class="ffa">product<span class="_ _3"> </span></span>迭代器,這個迭代器對兩個或多個可迭代物件進行兩兩配</div><div class="t m0 x5 h4 y59c ff5 fs3 fc0 sc0 ls0 ws0">對</div><div class="t m0 x68 hb y59d ffe fs4 fc1 sc0 ls0 ws0">2</div><div class="t m0 xa h4 y59c ff5 fs3 fc0 sc0 ls0 ws0">,最終產生一個集合進行迭代:</div><div class="t m0 x11 hc y59e ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[25]:<span class="_ _2a"> </span><span class="ff11 fc4">from<span class="_ _2a"> </span><span class="fc5">itertools<span class="_ _2a"> </span></span>import<span class="_ _2a"> </span></span><span class="fc0">product</span></div><div class="t m0 x31 hc y59f ffa fs3 fc0 sc0 ls0 ws0">p<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>product(<span class="ff12 fcc">'<span class="ffa">ab</span>'</span>,<span class="_ _2a"> </span><span class="fc4">range</span>(<span class="fc6">3</span>))</div><div class="t m0 x31 hc y5a0 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fc6">*</span>p)</span></div><div class="t m0 x11 hc y5a1 ffa fs3 fc0 sc0 ls0 ws0">(<span class="ff12">'</span>a<span class="ff12">'</span>,<span class="_ _2a"> </span>0)<span class="_ _2a"> </span>(<span class="ff12">'</span>a<span class="ff12">'</span>,<span class="_ _2a"> </span>1)<span class="_ _2a"> </span>(<span class="ff12">'</span>a<span class="ff12">'</span>,<span class="_ _2a"> </span>2)<span class="_ _2a"> </span>(<span class="ff12">'</span>b<span class="ff12">'</span>,<span class="_ _2a"> </span>0)<span class="_ _2a"> </span>(<span class="ff12">'</span>b<span class="ff12">'</span>,<span class="_ _2a"> </span>1)<span class="_ _2a"> </span>(<span class="ff12">'</span>b<span class="ff12">'</span>,<span class="_ _2a"> </span>2)</div><div class="t m0 x4 h4 y5a2 ffa fs3 fc0 sc0 ls0 ws0">itertools<span class="_ _3"> </span><span class="ff5">中還有更多有用的工具:你可以查閱<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>官方文件找到全部的列表以及一</span></div><div class="t m0 x5 h4 y5a3 ff5 fs3 fc0 sc0 ls0 ws0">些例子:<span class="ffa fc2">https://docs.python.org/3/library/itertools.html</span>。</div><div class="t m0 x12 hd yf9 fff fs5 fc0 sc0 ls0 ws0">2</div><div class="t m0 xa he y40 ff5 fs6 fc0 sc0 ls0 ws0">譯者注:實際上是求兩個或多個集合的<span class="ff6">笛卡爾積</span>(<span class="ff10">Cartesian<span class="_ _34"> </span>Product</span>)<span class="_ _44"></span>。</div><a class="l" href="#pf49" data-dest-detail='[73,"XYZ",88.59,85.15,null]'><div class="d m1" style="border-style:none;position:absolute;left:82.909000px;bottom:254.603000px;width:4.234000px;height:5.137000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="https://docs.python.org/3/library/itertools.html"><div class="d m1" style="border-style:none;position:absolute;left:115.636000px;bottom:102.316000px;width:274.909000px;height:10.000000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf4a" class="pf w0 h0" data-page-no="4a"><div class="pc pc4a w0 h0"><img class="bi x5 y9b w1 h41" alt="" src="images/bg4a.png"/><div class="t m0 x69 h1 y3 ff2 fs0 fc0 sc0 ls0 ws0">第<span class="_ _0"> </span><span class="ff1">12<span class="_ _0"> </span></span>章<span class="_ _32"> </span>列表推導</div><div class="t m0 x4 h4 y5a4 ff5 fs3 fc0 sc0 ls0 ws0">如果你讀過足夠多的<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>程式碼的話,你最終一定會遇到著名的<span class="ff6">列表推導</span>(<span class="ff7">list<span class="_ _3"> </span>compre-</span></div><div class="t m0 x5 h4 y5a5 ff7 fs3 fc0 sc0 ls0 ws0">hension<span class="ff5">)</span></div><div class="t m0 x6a hb y5a6 ffe fs4 fc1 sc0 ls0 ws0">1</div><div class="t m0 x32 h4 y5a5 ff5 fs3 fc0 sc0 ls0 ws0">這種簡短、高效的程式碼建立方式。如果你之前沒有使用過這種特性,我希望你最終</div><div class="t m0 x5 h4 y5a7 ff5 fs3 fc0 sc0 ls0 ws0">會愛上它。它的格式如下所示:</div><div class="t m0 x11 hc y5a8 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[1]:<span class="_ _2a"> </span><span class="fc0">[i<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span>i<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span><span class="fc4">range</span>(<span class="fc6">20</span>)<span class="_ _2a"> </span><span class="ff11 fc4">if<span class="_ _2a"> </span></span>i<span class="_ _2a"> </span><span class="fc6">%<span class="_ _2a"> </span>3<span class="_ _2a"> </span>><span class="_ _2a"> </span>0</span>]</span></div><div class="t m0 x11 hc y5a9 ffa fs3 fcf sc0 ls0 ws0">Out[1]:<span class="_ _2a"> </span><span class="fc0">[1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>4,<span class="_ _2a"> </span>5,<span class="_ _2a"> </span>7,<span class="_ _2a"> </span>8,<span class="_ _2a"> </span>10,<span class="_ _2a"> </span>11,<span class="_ _2a"> </span>13,<span class="_ _2a"> </span>14,<span class="_ _2a"> </span>16,<span class="_ _2a"> </span>17,<span class="_ _2a"> </span>19]</span></div><div class="t m0 x4 h4 y5aa ff5 fs3 fc0 sc0 ls0 ws0">這行程式碼的結果是一個排除了數字<span class="_ _3"> </span><span class="ff15">3<span class="_ _9"> </span></span>及其倍數的列表。第一眼看上去這段程式碼也許有些令</div><div class="t m0 x5 h4 y5ab ff5 fs3 fc0 sc0 ls0 ws0">人困惑,但是隨著你對<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>程式碼的熟悉,閱讀和書寫列表推導程式碼將變得越來越自然。</div><div class="t m0 x5 h8 y5ac ff1 fs1 fc0 sc0 ls0 ws0">12.1<span class="_ _39"> </span><span class="ff2">基礎列表推導</span></div><div class="t m0 x4 h4 y5ad ff5 fs3 fc0 sc0 ls0 ws0">列表推導是將一個冗長的<span class="_ _3"> </span><span class="ffa">for<span class="_ _9"> </span></span>迴圈列表組成語句壓縮為一行簡短易讀程式碼的簡單方法。比</div><div class="t m0 x5 h4 y5ae ff5 fs3 fc0 sc0 ls0 ws0">如,下面就是一個組成包含前<span class="_ _9"> </span><span class="ff15">12<span class="_ _3"> </span></span>個平方數的列表的迴圈語句:</div><div class="t m0 x11 hc y5af ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[2]:<span class="_ _2a"> </span><span class="fc0">L<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[]</span></div><div class="t m0 x24 hc y5b0 ff11 fs3 fc4 sc0 ls0 ws0">for<span class="_ _2a"> </span><span class="ffa fc0">n<span class="_ _2a"> </span></span><span class="fcb">in<span class="_ _2a"> </span></span><span class="ffa">range<span class="fc0">(<span class="fc6">12</span>):</span></span></div><div class="t m0 x5d hc y5b1 ffa fs3 fc0 sc0 ls0 ws0">L<span class="fc6">.</span>append(n<span class="_ _2a"> </span><span class="fc6">**<span class="_ _2a"> </span>2</span>)</div><div class="t m0 x24 hc y5b2 ffa fs3 fc0 sc0 ls0 ws0">L</div><div class="t m0 x11 hc y5b3 ffa fs3 fcf sc0 ls0 ws0">Out[2]:<span class="_ _2a"> </span><span class="fc0">[0,<span class="_ _2a"> </span>1,<span class="_ _2a"> </span>4,<span class="_ _2a"> </span>9,<span class="_ _2a"> </span>16,<span class="_ _2a"> </span>25,<span class="_ _2a"> </span>36,<span class="_ _2a"> </span>49,<span class="_ _2a"> </span>64,<span class="_ _2a"> </span>81,<span class="_ _2a"> </span>100,<span class="_ _2a"> </span>121]</span></div><div class="t m0 x4 h4 y5b4 ff5 fs3 fc0 sc0 ls0 ws0">與之等價的列表推導如下所示:</div><div class="t m0 x11 hc y5b5 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[3]:<span class="_ _2a"> </span><span class="fc0">[n<span class="_ _2a"> </span><span class="fc6">**<span class="_ _2a"> </span>2<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span></span>n<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span><span class="fc4">range</span>(<span class="fc6">12</span>)]</span></div><div class="t m0 x11 hc y5b6 ffa fs3 fcf sc0 ls0 ws0">Out[3]:<span class="_ _2a"> </span><span class="fc0">[0,<span class="_ _2a"> </span>1,<span class="_ _2a"> </span>4,<span class="_ _2a"> </span>9,<span class="_ _2a"> </span>16,<span class="_ _2a"> </span>25,<span class="_ _2a"> </span>36,<span class="_ _2a"> </span>49,<span class="_ _2a"> </span>64,<span class="_ _2a"> </span>81,<span class="_ _2a"> </span>100,<span class="_ _2a"> </span>121]</span></div><div class="t m0 x4 h4 y5b7 ff5 fs3 fc0 sc0 ls0 ws0">正如許多</div><div class="t m0 x27 h7 y5b8 ff7 fs3 fc0 sc0 ls0 ws0">Python</div><div class="t m0 x5f h4 y5b7 ff5 fs3 fc0 sc0 ls0 ws0">語句一樣,你幾乎可以直接用直白的英語讀出這段程式碼的意義:<span class="_ _33"></span>“對於每</div><div class="t m0 x5 h4 y5b9 ff5 fs3 fc0 sc0 ls0 ws0">個小於<span class="_ _9"> </span><span class="ff15">12<span class="_ _3"> </span></span>的<span class="_ _9"> </span><span class="ffa">n<span class="_ _9"> </span></span>值,組成一個包含<span class="_ _3"> </span><span class="ffa">n<span class="_ _9"> </span></span>的平方的列表”<span class="_ _33"></span>。</div><div class="t m0 x4 h4 y5ba ff5 fs3 fc0 sc0 ls0 ws0">列表推導的基本語法是:<span class="ffa">[<span class="ff14">expr<span class="_ _10"> </span></span>for<span class="_ _3"> </span><span class="ff14">var<span class="_ _10"> </span></span>in<span class="_ _9"> </span><span class="ff14">iterable<span class="_ _4b"> </span></span>]</span>。在這裡<span class="_ _3"> </span><span class="ff14">expr<span class="_ _10"> </span></span>是任意合法的表達</div><div class="t m0 x5 h4 y5bb ff5 fs3 fc0 sc0 ls0 ws0">式,<span class="ff14">var<span class="_ _10"> </span></span>是變數名,<span class="ff14">iterable<span class="_ _10"> </span></span>是任意可以迭代的<span class="_ _3"> </span><span class="ff7">Python<span class="_ _9"> </span></span>物件。</div><div class="t m0 x12 hd yb9 fff fs5 fc0 sc0 ls0 ws0">1</div><div class="t m0 xa he yba ff5 fs6 fc0 sc0 ls0 ws0">譯者注:<span class="_ _8"></span><span class="ff10">list<span class="_ _34"> </span>comprehension<span class="_ _36"> </span><span class="ff5">目前並沒有一個固定的翻譯,有翻譯成列表速構、<span class="_ _8"></span>列表解析、列表內包等,<span class="_ _8"></span>但意思</span></span></div><div class="t m0 x5 he y40 ff5 fs6 fc0 sc0 ls0 ws0">上都是在定義列表結構的時候按照一定的規則進行推導,而不是窮舉所有元素,故本書譯為列表推導。</div><div class="t m0 x2f h7 y20 ff7 fs3 fc0 sc0 ls0 ws0">67</div><a class="l" href="#pf4a" data-dest-detail='[74,"XYZ",88.59,98.3,null]'><div class="d m1" style="border-style:none;position:absolute;left:118.727000px;bottom:607.954000px;width:4.234000px;height:5.137000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf4b" class="pf w0 h0" data-page-no="4b"><div class="pc pc4b w0 h0"><img class="bi x5 y40 w1 h42" alt="" src="images/bg4b.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">68<span class="_ _85"> </span><span class="ffc">12.<span class="_ _16"> </span><span class="ff6">列表推導</span></span></div><div class="t m0 x5 h8 ybb ff1 fs1 fc0 sc0 ls0 ws0">12.2<span class="_ _39"> </span><span class="ff2">多重迭代</span></div><div class="t m0 x4 h4 y5bc ff5 fs3 fc0 sc0 ls0 ws0">有的時候你可能需要從不止一個值建立列表。在這個情況下,只要簡單地在列表推導中加</div><div class="t m0 x5 h4 y5bd ff5 fs3 fc0 sc0 ls0 ws0">入另外一個<span class="_ _9"> </span><span class="ffa">for<span class="_ _3"> </span></span>表示式即可:</div><div class="t m0 x11 hc y5be ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[4]:<span class="_ _2a"> </span><span class="fc0">[(i,<span class="_ _2a"> </span>j)<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span>i<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span><span class="fc4">range</span>(<span class="fc6">2</span>)<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span>j<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span><span class="fc4">range</span>(<span class="fc6">3</span>)]</span></div><div class="t m0 x11 hc y5bf ffa fs3 fcf sc0 ls0 ws0">Out[4]:<span class="_ _2a"> </span><span class="fc0">[(0,<span class="_ _2a"> </span>0),<span class="_ _2a"> </span>(0,<span class="_ _2a"> </span>1),<span class="_ _2a"> </span>(0,<span class="_ _2a"> </span>2),<span class="_ _2a"> </span>(1,<span class="_ _2a"> </span>0),<span class="_ _2a"> </span>(1,<span class="_ _2a"> </span>1),<span class="_ _2a"> </span>(1,<span class="_ _2a"> </span>2)]</span></div><div class="t m0 x4 h4 y5c0 ff5 fs3 fc0 sc0 ls0 ws0">注意到第二個<span class="_ _3"> </span><span class="ffa">for<span class="_ _a"> </span></span>表達式在這裡起到了內層迴圈索引的作用,最快速地改變了結果的列</div><div class="t m0 x5 h4 y5c1 ff5 fs3 fc0 sc0 ls0 ws0">表。列表推導中這種類型的組成可以繼續拓展到<span class="_ _3"> </span><span class="ff7">3<span class="_ _3"> </span></span>次、<span class="ff7">4<span class="_ _3"> </span></span>次甚至更多次迭代,盡管增加到一定</div><div class="t m0 x5 h4 y5c2 ff5 fs3 fc0 sc0 ls0 ws0">程度時程式碼的可讀性會下降!</div><div class="t m0 x5 h8 y5c3 ff1 fs1 fc0 sc0 ls0 ws0">12.3<span class="_ _39"> </span><span class="ff2">條件控制迭代</span></div><div class="t m0 x4 h4 y5c4 ff5 fs3 fc0 sc0 ls0 ws0">你可以通過在表達式尾部增加判斷語句進一步地控制迭代的條件。在這一節的第一個例子</div><div class="t m0 x5 h4 y5c5 ff5 fs3 fc0 sc0 ls0 ws0">中,我們從<span class="_ _3"> </span><span class="ff15">1<span class="_ _3"> </span></span>到<span class="_ _9"> </span><span class="ff15">20<span class="_ _3"> </span></span>迭代了所有整數,然後剔除了<span class="_ _3"> </span><span class="ff15">3<span class="_ _3"> </span></span>的倍數。看下面這個例子,觀察組成語句</div><div class="t m0 x5 h4 y5c6 ff5 fs3 fc0 sc0 ls0 ws0">的不同:</div><div class="t m0 x11 hc y5c7 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[5]:<span class="_ _2a"> </span><span class="fc0">[val<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span>val<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span><span class="fc4">range</span>(<span class="fc6">20</span>)<span class="_ _2a"> </span><span class="ff11 fc4">if<span class="_ _2a"> </span></span>val<span class="_ _2a"> </span><span class="fc6">%<span class="_ _2a"> </span>3<span class="_ _2a"> </span>><span class="_ _2a"> </span>0</span>]</span></div><div class="t m0 x11 hc y5c8 ffa fs3 fcf sc0 ls0 ws0">Out[5]:<span class="_ _2a"> </span><span class="fc0">[1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>4,<span class="_ _2a"> </span>5,<span class="_ _2a"> </span>7,<span class="_ _2a"> </span>8,<span class="_ _2a"> </span>10,<span class="_ _2a"> </span>11,<span class="_ _2a"> </span>13,<span class="_ _2a"> </span>14,<span class="_ _2a"> </span>16,<span class="_ _2a"> </span>17,<span class="_ _2a"> </span>19]</span></div><div class="t m0 x4 h4 y5c9 ff5 fs3 fc0 sc0 ls0 ws0">表達式<span class="_ _3"> </span><span class="ffa">(var<span class="_ _2a"> </span>%<span class="_ _2a"> </span>3<span class="_ _2a"> </span>><span class="_ _2a"> </span>0)<span class="_ _3"> </span></span>為<span class="_ _a"> </span><span class="ffa">True<span class="_ _3"> </span></span>僅當<span class="_ _a"> </span><span class="ffa">val<span class="_ _3"> </span></span>不能被<span class="_ _a"> </span><span class="ff15">3<span class="_ _3"> </span></span>整除時。我們再一次看見,上一行代</div><div class="t m0 x5 h4 y5ca ff5 fs3 fc0 sc0 ls0 ws0">碼的意義可以立即用英語讀出來:<span class="_ _5f"></span>“組成一個列表,包含每一個小於<span class="_ _3"> </span><span class="ff15">20<span class="_ _9"> </span></span>的整數,但是數值不能</div><div class="t m0 x5 h4 y5cb ff5 fs3 fc0 sc0 ls0 ws0">被<span class="_ _3"> </span><span class="ff15">3<span class="_ _3"> </span></span>整除”<span class="_ _44"></span>。一旦你對這種寫法感到習慣,那麼書寫這種程式碼就變得更加簡單——並且一眼看</div><div class="t m0 x5 h4 y5cc ff5 fs3 fc0 sc0 ls0 ws0">上去比與之等價的迴圈語句更好理解:</div><div class="t m0 x11 hc y5cd ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[6]:<span class="_ _2a"> </span><span class="fc0">L<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[]</span></div><div class="t m0 x24 hc y5ce ff11 fs3 fc4 sc0 ls0 ws0">for<span class="_ _2a"> </span><span class="ffa fc0">val<span class="_ _2a"> </span></span><span class="fcb">in<span class="_ _2a"> </span></span><span class="ffa">range<span class="fc0">(<span class="fc6">20</span>):</span></span></div><div class="t m0 x5d hc y5cf ff11 fs3 fc4 sc0 ls0 ws0">if<span class="_ _2a"> </span><span class="ffa fc0">val<span class="_ _2a"> </span><span class="fc6">%<span class="_ _2a"> </span>3</span>:</span></div><div class="t m0 x25 hc y5d0 ffa fs3 fc0 sc0 ls0 ws0">L<span class="fc6">.</span>append(val)</div><div class="t m0 x24 hc y5d1 ffa fs3 fc0 sc0 ls0 ws0">L</div><div class="t m0 x11 hc y5d2 ffa fs3 fcf sc0 ls0 ws0">Out[6]:<span class="_ _2a"> </span><span class="fc0">[1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>4,<span class="_ _2a"> </span>5,<span class="_ _2a"> </span>7,<span class="_ _2a"> </span>8,<span class="_ _2a"> </span>10,<span class="_ _2a"> </span>11,<span class="_ _2a"> </span>13,<span class="_ _2a"> </span>14,<span class="_ _2a"> </span>16,<span class="_ _2a"> </span>17,<span class="_ _2a"> </span>19]</span></div><div class="t m0 x5 h8 y5d3 ff1 fs1 fc0 sc0 ls0 ws0">12.4<span class="_ _39"> </span><span class="ff2">條件控制值</span></div><div class="t m0 x4 h4 y5d4 ff5 fs3 fc0 sc0 ls0 ws0">如果你曾寫過<span class="_ _9"> </span><span class="ff7">C<span class="_ _3"> </span></span>語言程式碼,你可能對使用<span class="ff6">三元運算子<span class="_ _4"></span><span class="ff5">(<span class="ff7">ternary<span class="_ _9"> </span>operator</span>)<span class="_ _3"> </span><span class="ffa">?<span class="_ _e"> </span>:<span class="_ _3"> </span></span>在一行內</span></span></div><div class="t m0 x5 h4 y5d5 ff5 fs3 fc0 sc0 ls0 ws0">進行條件判斷感到熟悉:</div><div class="t m0 x11 hc y119 ffa fs3 fce sc0 ls0 ws0">int<span class="_ _2a"> </span><span class="fc0">absval<span class="_ _2a"> </span>=<span class="_ _2a"> </span>(val<span class="_ _2a"> </span><<span class="_ _2a"> </span><span class="fc7">0</span>)<span class="_ _2a"> </span>?<span class="_ _2a"> </span>-val<span class="_ _2a"> </span>:<span class="_ _2a"> </span>val</span></div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf4c" class="pf w0 h0" data-page-no="4c"><div class="pc pc4c w0 h0"><img class="bi x5 y5d6 w1 h43" alt="" src="images/bg4c.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">12.4.<span class="_ _16"> </span><span class="ff6">條件控制值<span class="_ _86"> </span><span class="ff7">69</span></span></div><div class="t m0 x4 h4 y42 ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _9"> </span><span class="ff5">也有非常類似的語法,<span class="_ _4"></span>並且在列表推導、<span class="_ _4"></span><span class="ff7">Lam<span class="_ _4"></span>bda<span class="_ _9"> </span><span class="ff5">函式和其他簡單的表示式場景中</span></span></span></div><div class="t m0 x5 h4 y77 ff5 fs3 fc0 sc0 ls0 ws0">也十分常用:</div><div class="t m0 x11 hc y5d7 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[7]:<span class="_ _2a"> </span><span class="fc0">val<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>-10</span></span></div><div class="t m0 x24 hc y5d8 ffa fs3 fc0 sc0 ls0 ws0">val<span class="_ _2a"> </span><span class="ff11 fc4">if<span class="_ _2a"> </span></span>val<span class="_ _2a"> </span><span class="fc6">>=<span class="_ _2a"> </span>0<span class="_ _2a"> </span><span class="ff11 fc4">else<span class="_ _2a"> </span></span>-</span>val</div><div class="t m0 x11 hc y5d9 ffa fs3 fcf sc0 ls0 ws0">Out[7]:<span class="_ _2a"> </span><span class="fc0">10</span></div><div class="t m0 x4 h4 y5da ff5 fs3 fc0 sc0 ls0 ws0">我們可以看到,上述程式碼實現了內建的取絕對值<span class="_ _3"> </span><span class="ffa">abs()<span class="_ _9"> </span></span>函式,但是這種組成方式能夠使你</div><div class="t m0 x5 h4 y5db ff5 fs3 fc0 sc0 ls0 ws0">在列表推導中做更多有趣的事情。目前我們的程式碼變得越來越複雜,但是你可以做類別似下面這</div><div class="t m0 x5 h4 y5dc ff5 fs3 fc0 sc0 ls0 ws0">段程式碼的事情:</div><div class="t m0 x11 hc y5dd ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[8]:<span class="_ _2a"> </span><span class="fc0">[val<span class="_ _2a"> </span><span class="ff11 fc4">if<span class="_ _2a"> </span></span>val<span class="_ _2a"> </span><span class="fc6">%<span class="_ _2a"> </span>2<span class="_ _2a"> </span><span class="ff11 fc4">else<span class="_ _2a"> </span></span>-</span>val</span></div><div class="t m0 x31 hc y5de ff11 fs3 fc4 sc0 ls0 ws0">for<span class="_ _2a"> </span><span class="ffa fc0">val<span class="_ _2a"> </span></span><span class="fcb">in<span class="_ _2a"> </span></span><span class="ffa">range<span class="fc0">(<span class="fc6">20</span>)<span class="_ _2a"> </span></span></span>if<span class="_ _2a"> </span><span class="ffa fc0">val<span class="_ _2a"> </span><span class="fc6">%<span class="_ _2a"> </span>3</span>]</span></div><div class="t m0 x11 hc y5df ffa fs3 fcf sc0 ls0 ws0">Out[8]:<span class="_ _2a"> </span><span class="fc0">[1,<span class="_ _2a"> </span>-2,<span class="_ _2a"> </span>-4,<span class="_ _2a"> </span>5,<span class="_ _2a"> </span>7,<span class="_ _2a"> </span>-8,<span class="_ _2a"> </span>-10,<span class="_ _2a"> </span>11,<span class="_ _2a"> </span>13,<span class="_ _2a"> </span>-14,<span class="_ _2a"> </span>-16,<span class="_ _2a"> </span>17,<span class="_ _2a"> </span>19]</span></div><div class="t m0 x4 h4 y5e0 ff5 fs3 fc0 sc0 ls0 ws0">注意到上述列表推導表達式中在<span class="_ _3"> </span><span class="ffa">for<span class="_ _3"> </span></span>迴圈表示式之前有一個換行:這在<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>中是合法</div><div class="t m0 x5 h4 y5e1 ff5 fs3 fc0 sc0 ls0 ws0">的。並且為了更好的可讀性,經常用來截斷過長的列表推導表達式。讓我們重新看一下上述代</div><div class="t m0 x5 h4 y5e2 ff5 fs3 fc0 sc0 ls0 ws0">碼:我們在組成一個表示式,去除所有<span class="_ _9"> </span><span class="ff15">3<span class="_ _3"> </span></span>的倍數,並且把所有<span class="_ _9"> </span><span class="ff15">2<span class="_ _9"> </span></span>的倍數取負。</div><div class="t m0 x4 h4 y5e3 ff5 fs3 fc0 sc0 ls0 ws0">一旦你理解了列表推導的動態性,那麼理解其他類型的推導就變得易如反掌:語法幾乎是</div><div class="t m0 x5 h4 y5e4 ff5 fs3 fc0 sc0 ls0 ws0">一致的,僅僅在於括號的使用方法不同。</div><div class="t m0 x4 h4 y5e5 ff5 fs3 fc0 sc0 ls0 ws0">舉例來說,使用大括號你就可以使用<span class="ff6">集合推導</span>(<span class="ff7">set<span class="_ _9"> </span>comprehension</span>)產生一個<span class="_ _3"> </span><span class="ffa">set<span class="_ _9"> </span></span>物件:</div><div class="t m0 x11 hc y5e6 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[9]:<span class="_ _2a"> </span><span class="fc0">{n<span class="fc6">**2<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span></span>n<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span><span class="fc4">range</span>(<span class="fc6">12</span>)}</span></div><div class="t m0 x11 hc y5e7 ffa fs3 fcf sc0 ls0 ws0">Out[9]:<span class="_ _2a"> </span><span class="fc0">{0,<span class="_ _2a"> </span>1,<span class="_ _2a"> </span>4,<span class="_ _2a"> </span>9,<span class="_ _2a"> </span>16,<span class="_ _2a"> </span>25,<span class="_ _2a"> </span>36,<span class="_ _2a"> </span>49,<span class="_ _2a"> </span>64,<span class="_ _2a"> </span>81,<span class="_ _2a"> </span>100,<span class="_ _2a"> </span>121}</span></div><div class="t m0 x4 h4 y5e8 ff5 fs3 fc0 sc0 ls0 ws0">回憶一下,<span class="ffa">set<span class="_ _3"> </span></span>是一個不含重復項的集合。因此集合推導也遵守了這個規則,消除了所有</div><div class="t m0 x5 h4 y5e9 ff5 fs3 fc0 sc0 ls0 ws0">重複的項:</div><div class="t m0 x11 hc y5ea ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[10]:<span class="_ _2a"> </span><span class="fc0">{a<span class="_ _2a"> </span><span class="fc6">%<span class="_ _2a"> </span>3<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span></span>a<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span><span class="fc4">range</span>(<span class="fc6">1000</span>)}</span></div><div class="t m0 x11 hc y5eb ffa fs3 fcf sc0 ls0 ws0">Out[10]:<span class="_ _2a"> </span><span class="fc0">{0,<span class="_ _2a"> </span>1,<span class="_ _2a"> </span>2}</span></div><div class="t m0 x4 h4 y5ec ff5 fs3 fc0 sc0 ls0 ws0">只要稍微調整一下語法,加一個冒號(<span class="ffa">:</span>)就可以使用<span class="ff6">字典推導</span>(<span class="ff7">dict<span class="_ _9"> </span>comprehension</span>)<span class="_ _33"></span>:</div><div class="t m0 x11 hc y5ed ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[11]:<span class="_ _2a"> </span><span class="fc0">{n:n<span class="fc6">**2<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span></span>n<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span><span class="fc4">range</span>(<span class="fc6">6</span>)}</span></div><div class="t m0 x11 hc y5ee ffa fs3 fcf sc0 ls0 ws0">Out[11]:<span class="_ _2a"> </span><span class="fc0">{0:<span class="_ _2a"> </span>0,<span class="_ _2a"> </span>1:<span class="_ _2a"> </span>1,<span class="_ _2a"> </span>2:<span class="_ _2a"> </span>4,<span class="_ _2a"> </span>3:<span class="_ _2a"> </span>9,<span class="_ _2a"> </span>4:<span class="_ _2a"> </span>16,<span class="_ _2a"> </span>5:<span class="_ _2a"> </span>25}</span></div><div class="t m0 x4 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">最後,<span class="_ _3e"></span>如果你使用小括號而不是方括號,<span class="_ _3e"></span>你就得到了<span class="ff6">產生器表示式<span class="_ _8"></span><span class="ff5">(<span class="ff7">generator<span class="_ _9"> </span>expression</span>)<span class="_ _33"></span>:</span></span></div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf4d" class="pf w0 h0" data-page-no="4d"><div class="pc pc4d w0 h0"><img class="bi x5 y5ef w1 h44" alt="" src="images/bg4d.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">70<span class="_ _85"> </span><span class="ffc">12.<span class="_ _16"> </span><span class="ff6">列表推導</span></span></div><div class="t m0 x11 hc yfa ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[12]:<span class="_ _2a"> </span><span class="fc0">(n<span class="fc6">**2<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span></span>n<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span><span class="fc4">range</span>(<span class="fc6">12</span>))</span></div><div class="t m0 x11 hc yfc ffa fs3 fcf sc0 ls0 ws0">Out[12]:<span class="_ _2a"> </span><span class="fc0"><generator<span class="_ _2a"> </span>object<span class="_ _2a"> </span><genexpr><span class="_ _2a"> </span>at<span class="_ _2a"> </span>0x10e53f150></span></div><div class="t m0 x4 h4 y236 ff5 fs3 fc0 sc0 ls0 ws0">產生器表達式基本上和列表推導沒有什麼不同,區別僅僅在於產生器表達式是<span class="ff2">按需產生</span>對</div><div class="t m0 x5 h4 y21c ff5 fs3 fc0 sc0 ls0 ws0">象,而列表推導是<span class="ff2">一次產生全部</span>物件。這裡語言上的簡單性掩蓋了這種語言功能的強大,我們</div><div class="t m0 x5 h4 y190 ff5 fs3 fc0 sc0 ls0 ws0">將在下面探討這一點。</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf4e" class="pf w0 h0" data-page-no="4e"><div class="pc pc4e w0 h0"><img class="bi x5 y5f0 w1 h45" alt="" src="images/bg4e.png"/><div class="t m0 x0 h1 y5f1 ff2 fs0 fc0 sc0 ls0 ws0">第<span class="_ _0"> </span><span class="ff1">13<span class="_ _0"> </span></span>章<span class="_ _32"> </span>產生器</div><div class="t m0 x4 h4 y5f2 ff5 fs3 fc0 sc0 ls0 ws0">本<span class="_ _4a"></span>章<span class="_ _4a"></span>中<span class="_ _4a"></span>我<span class="_ _55"></span>們<span class="_ _4a"></span>將<span class="_ _4a"></span>深<span class="_ _4a"></span>入<span class="_ _4a"></span>了<span class="_ _55"> </span>解<span class="_ _1"> </span><span class="ff7">Python<span class="_ _10"> </span><span class="ff6">生<span class="_ _4a"></span>成<span class="_ _4a"></span>器<span class="_ _4a"></span></span></span>(<span class="ff7">generator</span>)<span class="_ _8"></span>,<span class="_ _4a"></span>包<span class="_ _4a"></span>括<span class="ff6">生<span class="_ _4a"></span>成<span class="_ _4a"></span>器<span class="_ _55"> </span>表<span class="_ _4a"></span>達<span class="_ _4a"></span>式<span class="_ _4a"></span></span>(<span class="ff7">generator</span></div><div class="t m0 x5 h4 y5f3 ff7 fs3 fc0 sc0 ls0 ws0">expression<span class="ff5">)和<span class="ff6">產生器函式</span>(</span>generator<span class="_ _9"> </span>function<span class="ff5">)<span class="_ _33"></span>。</span></div><div class="t m0 x5 h8 y5f4 ff1 fs1 fc0 sc0 ls0 ws0">13.1<span class="_ _39"> </span><span class="ff2">產生器表示式</span></div><div class="t m0 x4 h4 y5f5 ff5 fs3 fc0 sc0 ls0 ws0">列表推導和產生器表示式的區別有的時候令人十分困惑。這裡我們快速領略一下它們的區</div><div class="t m0 x5 h4 y5f6 ff5 fs3 fc0 sc0 ls0 ws0">別:</div><div class="t m0 x5 ha y5f7 ff1 fs2 fc0 sc0 ls0 ws0">13.1.1<span class="_ _3c"> </span><span class="ff2">列表推導使用方括號,而產生器表示式使用圓括號</span></div><div class="t m0 x4 h4 y5f8 ff5 fs3 fc0 sc0 ls0 ws0">這是一個典型的列表推導:</div><div class="t m0 x11 hc y5f9 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[1]:<span class="_ _2a"> </span><span class="fc0">[n<span class="_ _2a"> </span><span class="fc6">**<span class="_ _2a"> </span>2<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span></span>n<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span><span class="fc4">range</span>(<span class="fc6">12</span>)]</span></div><div class="t m0 x11 hc y5fa ffa fs3 fcf sc0 ls0 ws0">Out[1]:<span class="_ _2a"> </span><span class="fc0">[0,<span class="_ _2a"> </span>1,<span class="_ _2a"> </span>4,<span class="_ _2a"> </span>9,<span class="_ _2a"> </span>16,<span class="_ _2a"> </span>25,<span class="_ _2a"> </span>36,<span class="_ _2a"> </span>49,<span class="_ _2a"> </span>64,<span class="_ _2a"> </span>81,<span class="_ _2a"> </span>100,<span class="_ _2a"> </span>121]</span></div><div class="t m0 x4 h4 y5fb ff5 fs3 fc0 sc0 ls0 ws0">而這是一個典型的產生器表示式:</div><div class="t m0 x11 hc y5fc ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[2]:<span class="_ _2a"> </span><span class="fc0">(n<span class="_ _2a"> </span><span class="fc6">**<span class="_ _2a"> </span>2<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span></span>n<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span><span class="fc4">range</span>(<span class="fc6">12</span>))</span></div><div class="t m0 x11 hc y5fd ffa fs3 fcf sc0 ls0 ws0">Out[2]:<span class="_ _2a"> </span><span class="fc0"><generator<span class="_ _2a"> </span>object<span class="_ _2a"> </span><genexpr><span class="_ _2a"> </span>at<span class="_ _2a"> </span>0x10fe0dfc0></span></div><div class="t m0 x4 h4 y5fe ff5 fs3 fc0 sc0 ls0 ws0">注意:直接輸出產生器表達式並不會列印出內容。要列印出產生器表達式產生的全部內容</div><div class="t m0 x5 h4 y5ff ff5 fs3 fc0 sc0 ls0 ws0">的一個方法是通過向<span class="_ _9"> </span><span class="ffa">list<span class="_ _3"> </span></span>產生器傳遞參數:</div><div class="t m0 x11 hc y600 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[3]:<span class="_ _2a"> </span><span class="fc0">G<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>(n<span class="_ _2a"> </span><span class="fc6">**<span class="_ _2a"> </span>2<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span></span>n<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span><span class="fc4">range</span>(<span class="fc6">12</span>))</span></div><div class="t m0 x24 hc y601 ffa fs3 fc4 sc0 ls0 ws0">list<span class="fc0">(G)</span></div><div class="t m0 x11 hc y602 ffa fs3 fcf sc0 ls0 ws0">Out[3]:<span class="_ _2a"> </span><span class="fc0">[0,<span class="_ _2a"> </span>1,<span class="_ _2a"> </span>4,<span class="_ _2a"> </span>9,<span class="_ _2a"> </span>16,<span class="_ _2a"> </span>25,<span class="_ _2a"> </span>36,<span class="_ _2a"> </span>49,<span class="_ _2a"> </span>64,<span class="_ _2a"> </span>81,<span class="_ _2a"> </span>100,<span class="_ _2a"> </span>121]</span></div><div class="t m0 x5 ha y603 ff1 fs2 fc0 sc0 ls0 ws0">13.1.2<span class="_ _3c"> </span><span class="ff2">列表是值的集合,而產生器是產生值的方法</span></div><div class="t m0 x4 h4 y99 ff5 fs3 fc0 sc0 ls0 ws0">當你建立一個列表的時候,你<span class="ff2">實際上</span>在產生一個數值的集合,並且需要花費一定的記憶體開</div><div class="t m0 x5 h4 y9a ff5 fs3 fc0 sc0 ls0 ws0">銷。當你建立一個產生器的時候,你並沒有建立一個數值的集合,而是建立了一個產生這些數</div><div class="t m0 x5 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">值的<span class="ff2">方法</span>。這兩者都具有相同的迭代器介面,正如我們在這裡看到的:</div><div class="t m0 x2f h7 y20 ff7 fs3 fc0 sc0 ls0 ws0">71</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf4f" class="pf w0 h0" data-page-no="4f"><div class="pc pc4f w0 h0"><img class="bi x5 y604 w1 h46" alt="" src="images/bg4f.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">72<span class="_ _83"> </span><span class="ffc">13.<span class="_ _16"> </span><span class="ff6">產生器</span></span></div><div class="t m0 x11 hc yfa ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[4]:<span class="_ _2a"> </span><span class="fc0">L<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[n<span class="_ _2a"> </span><span class="fc6">**<span class="_ _2a"> </span>2<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span></span>n<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span><span class="fc4">range</span>(<span class="fc6">12</span>)]</span></div><div class="t m0 x24 hc yfb ff11 fs3 fc4 sc0 ls0 ws0">for<span class="_ _2a"> </span><span class="ffa fc0">val<span class="_ _2a"> </span></span><span class="fcb">in<span class="_ _2a"> </span><span class="ffa fc0">L:</span></span></div><div class="t m0 x5d hc yfc ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(val,<span class="_ _2a"> </span>end<span class="fc6">=<span class="ff12 fcc">'<span class="_ _2a"> </span>'</span></span>)</span></div><div class="t m0 x11 hc yfd ffa fs3 fc0 sc0 ls0 ws0">0<span class="_ _2a"> </span>1<span class="_ _2a"> </span>4<span class="_ _2a"> </span>9<span class="_ _2a"> </span>16<span class="_ _2a"> </span>25<span class="_ _2a"> </span>36<span class="_ _2a"> </span>49<span class="_ _2a"> </span>64<span class="_ _2a"> </span>81<span class="_ _2a"> </span>100<span class="_ _2a"> </span>121</div><div class="t m0 x11 hc yff ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[5]:<span class="_ _2a"> </span><span class="fc0">G<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>(n<span class="_ _2a"> </span><span class="fc6">**<span class="_ _2a"> </span>2<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span></span>n<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span><span class="fc4">range</span>(<span class="fc6">12</span>))</span></div><div class="t m0 x24 hc y100 ff11 fs3 fc4 sc0 ls0 ws0">for<span class="_ _2a"> </span><span class="ffa fc0">val<span class="_ _2a"> </span></span><span class="fcb">in<span class="_ _2a"> </span><span class="ffa fc0">G:</span></span></div><div class="t m0 x5d hc y101 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(val,<span class="_ _2a"> </span>end<span class="fc6">=<span class="ff12 fcc">'<span class="_ _2a"> </span>'</span></span>)</span></div><div class="t m0 x11 hc y102 ffa fs3 fc0 sc0 ls0 ws0">0<span class="_ _2a"> </span>1<span class="_ _2a"> </span>4<span class="_ _2a"> </span>9<span class="_ _2a"> </span>16<span class="_ _2a"> </span>25<span class="_ _2a"> </span>36<span class="_ _2a"> </span>49<span class="_ _2a"> </span>64<span class="_ _2a"> </span>81<span class="_ _2a"> </span>100<span class="_ _2a"> </span>121</div><div class="t m0 x4 h4 y605 ff5 fs3 fc0 sc0 ls0 ws0">區別是:產生器表達式並不會在數值被需要之前計算出它們。這並不僅僅可以提高記憶體效</div><div class="t m0 x5 h4 y606 ff5 fs3 fc0 sc0 ls0 ws0">率,也能提高計算效率!這也意味著一個列表的大小受到可用記憶體的限制,而產生器表達式的</div><div class="t m0 x5 h4 y607 ff5 fs3 fc0 sc0 ls0 ws0">大小是無限的!</div><div class="t m0 x4 h4 y608 ff5 fs3 fc0 sc0 ls0 ws0">我們可以通過<span class="_ _9"> </span><span class="ffa">itertools<span class="_ _3"> </span></span>中定義的<span class="_ _9"> </span><span class="ffa">count<span class="_ _9"> </span></span>迭代器建立一個無限的產生器表示式的例子:</div><div class="t m0 x11 hc y609 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[6]:<span class="_ _2a"> </span><span class="ff11 fc4">from<span class="_ _2a"> </span><span class="fc5">itertools<span class="_ _2a"> </span></span>import<span class="_ _2a"> </span></span><span class="fc0">count</span></div><div class="t m0 x24 hc y60a ffa fs3 fc0 sc0 ls0 ws0">count()</div><div class="t m0 x11 hc y60b ffa fs3 fcf sc0 ls0 ws0">Out[6]:<span class="_ _2a"> </span><span class="fc0">count(0)</span></div><div class="t m0 x11 hc y60c ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[7]:<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span><span class="fc0">i<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span>count():</span></div><div class="t m0 x5d hc y60d ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(i,<span class="_ _2a"> </span>end<span class="fc6">=<span class="ff12 fcc">'<span class="_ _2a"> </span>'</span></span>)</span></div><div class="t m0 x5d hc y60e ff11 fs3 fc4 sc0 ls0 ws0">if<span class="_ _2a"> </span><span class="ffa fc0">i<span class="_ _2a"> </span><span class="fc6">>=<span class="_ _2a"> </span>10</span>:<span class="_ _2a"> </span></span>break</div><div class="t m0 x11 hc y60f ffa fs3 fc0 sc0 ls0 ws0">0<span class="_ _2a"> </span>1<span class="_ _2a"> </span>2<span class="_ _2a"> </span>3<span class="_ _2a"> </span>4<span class="_ _2a"> </span>5<span class="_ _2a"> </span>6<span class="_ _2a"> </span>7<span class="_ _2a"> </span>8<span class="_ _2a"> </span>9<span class="_ _2a"> </span>10</div><div class="t m0 x4 h4 y610 ffa fs3 fc0 sc0 ls0 ws0">count<span class="_ _a"> </span><span class="ff5">迭代器會開心地計數直到進程被手動中斷或結束,這使得建立一個永遠不會停止</span></div><div class="t m0 x5 h4 y611 ff5 fs3 fc0 sc0 ls0 ws0">的產生器非常方便:</div><div class="t m0 x11 hc y612 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[8]:<span class="_ _2a"> </span><span class="fc0">factors<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[<span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">3</span>,<span class="_ _2a"> </span><span class="fc6">5</span>,<span class="_ _2a"> </span><span class="fc6">7</span>]</span></div><div class="t m0 x24 hc y613 ffa fs3 fc0 sc0 ls0 ws0">G<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>(i<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span>i<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span>count()<span class="_ _2a"> </span><span class="ff11 fc4">if<span class="_ _2a"> </span><span class="ffa">all</span></span>(i<span class="_ _2a"> </span><span class="fc6">%<span class="_ _2a"> </span></span>n<span class="_ _2a"> </span><span class="fc6">><span class="_ _2a"> </span>0<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span></span>n<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span>factors))</div><div class="t m0 x24 hc y614 ff11 fs3 fc4 sc0 ls0 ws0">for<span class="_ _2a"> </span><span class="ffa fc0">val<span class="_ _2a"> </span></span><span class="fcb">in<span class="_ _2a"> </span><span class="ffa fc0">G:</span></span></div><div class="t m0 x5d hc y615 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(val,<span class="_ _2a"> </span>end<span class="fc6">=<span class="ff12 fcc">'<span class="_ _2a"> </span>'</span></span>)</span></div><div class="t m0 x5d hc y616 ff11 fs3 fc4 sc0 ls0 ws0">if<span class="_ _2a"> </span><span class="ffa fc0">val<span class="_ _2a"> </span><span class="fc6">><span class="_ _2a"> </span>40</span>:<span class="_ _2a"> </span></span>break</div><div class="t m0 x11 hc y617 ffa fs3 fc0 sc0 ls0 ws0">1<span class="_ _2a"> </span>11<span class="_ _2a"> </span>13<span class="_ _2a"> </span>17<span class="_ _2a"> </span>19<span class="_ _2a"> </span>23<span class="_ _2a"> </span>29<span class="_ _2a"> </span>31<span class="_ _2a"> </span>37<span class="_ _2a"> </span>41</div><div class="t m0 x4 h4 y99 ff5 fs3 fc0 sc0 ls0 ws0">你可能看到我們在這裡得到什麼:如果我們把這個包含因子的列表合適地展開的話,我們</div><div class="t m0 x5 h4 y9a ff5 fs3 fc0 sc0 ls0 ws0">在開頭會得到一個質數的產生器<span class="_ _8"></span>(通過愛拉託遜斯篩法)<span class="_ _33"></span>。<span class="_ _2"></span>我們馬上就會開始繼續研究產生質數</div><div class="t m0 x5 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">這個話題。</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf50" class="pf w0 h0" data-page-no="50"><div class="pc pc50 w0 h0"><img class="bi x5 y523 w1 h47" alt="" src="images/bg50.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">13.1.<span class="_ _16"> </span><span class="ff6">產生器表示式<span class="_ _82"> </span><span class="ff7">73</span></span></div><div class="t m0 x5 ha y2ca ff1 fs2 fc0 sc0 ls0 ws0">13.1.3<span class="_ _3c"> </span><span class="ff2">列表可以被迭代多次,而產生器表示式是一次性的</span></div><div class="t m0 x4 h4 y2cb ff5 fs3 fc0 sc0 ls0 ws0">這是產生器表示式潛在的問題之一。我們可以直接對一個列表進行如下操作:</div><div class="t m0 x11 hc y618 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[9]:<span class="_ _2a"> </span><span class="fc0">L<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[n<span class="_ _2a"> </span><span class="fc6">**<span class="_ _2a"> </span>2<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span></span>n<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span><span class="fc4">range</span>(<span class="fc6">12</span>)]</span></div><div class="t m0 x24 hc y619 ff11 fs3 fc4 sc0 ls0 ws0">for<span class="_ _2a"> </span><span class="ffa fc0">val<span class="_ _2a"> </span></span><span class="fcb">in<span class="_ _2a"> </span><span class="ffa fc0">L:</span></span></div><div class="t m0 x5d hc y61a ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(val,<span class="_ _2a"> </span>end<span class="fc6">=<span class="ff12 fcc">'<span class="_ _2a"> </span>'</span></span>)</span></div><div class="t m0 x24 hc y61b ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">()</span></div><div class="t m0 x24 hc y61c ff11 fs3 fc4 sc0 ls0 ws0">for<span class="_ _2a"> </span><span class="ffa fc0">val<span class="_ _2a"> </span></span><span class="fcb">in<span class="_ _2a"> </span><span class="ffa fc0">L:</span></span></div><div class="t m0 x5d hc y61d ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(val,<span class="_ _2a"> </span>end<span class="fc6">=<span class="ff12 fcc">'<span class="_ _2a"> </span>'</span></span>)</span></div><div class="t m0 x11 hc y61e ffa fs3 fc0 sc0 ls0 ws0">0<span class="_ _2a"> </span>1<span class="_ _2a"> </span>4<span class="_ _2a"> </span>9<span class="_ _2a"> </span>16<span class="_ _2a"> </span>25<span class="_ _2a"> </span>36<span class="_ _2a"> </span>49<span class="_ _2a"> </span>64<span class="_ _2a"> </span>81<span class="_ _2a"> </span>100<span class="_ _2a"> </span>121</div><div class="t m0 x11 hc y61f ffa fs3 fc0 sc0 ls0 ws0">0<span class="_ _2a"> </span>1<span class="_ _2a"> </span>4<span class="_ _2a"> </span>9<span class="_ _2a"> </span>16<span class="_ _2a"> </span>25<span class="_ _2a"> </span>36<span class="_ _2a"> </span>49<span class="_ _2a"> </span>64<span class="_ _2a"> </span>81<span class="_ _2a"> </span>100<span class="_ _2a"> </span>121</div><div class="t m0 x4 h4 y620 ff5 fs3 fc0 sc0 ls0 ws0">另一方面,產生器表示式在一次迭代後就被用盡:</div><div class="t m0 x11 hc y621 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[10]:<span class="_ _2a"> </span><span class="fc0">G<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>(n<span class="_ _2a"> </span><span class="fc6">**<span class="_ _2a"> </span>2<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span></span>n<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span><span class="fc4">range</span>(<span class="fc6">12</span>))</span></div><div class="t m0 x31 hc y622 ffa fs3 fc4 sc0 ls0 ws0">list<span class="fc0">(G)</span></div><div class="t m0 x11 hc y623 ffa fs3 fcf sc0 ls0 ws0">Out[10]:<span class="_ _2a"> </span><span class="fc0">[0,<span class="_ _2a"> </span>1,<span class="_ _2a"> </span>4,<span class="_ _2a"> </span>9,<span class="_ _2a"> </span>16,<span class="_ _2a"> </span>25,<span class="_ _2a"> </span>36,<span class="_ _2a"> </span>49,<span class="_ _2a"> </span>64,<span class="_ _2a"> </span>81,<span class="_ _2a"> </span>100,<span class="_ _2a"> </span>121]</span></div><div class="t m0 x11 hc y624 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[11]:<span class="_ _2a"> </span><span class="fc4">list<span class="fc0">(G)</span></span></div><div class="t m0 x11 hc y625 ffa fs3 fcf sc0 ls0 ws0">Out[11]:<span class="_ _2a"> </span><span class="fc0">[]</span></div><div class="t m0 x4 h4 y626 ff5 fs3 fc0 sc0 ls0 ws0">這個特性可以變得非常實用,因為這意味著迭代可以被停止和繼續:</div><div class="t m0 x11 hc y627 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[12]:<span class="_ _2a"> </span><span class="fc0">G<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>(n<span class="fc6">**2<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span></span>n<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span><span class="fc4">range</span>(<span class="fc6">12</span>))</span></div><div class="t m0 x31 hc y628 ff11 fs3 fc4 sc0 ls0 ws0">for<span class="_ _2a"> </span><span class="ffa fc0">n<span class="_ _2a"> </span></span><span class="fcb">in<span class="_ _2a"> </span><span class="ffa fc0">G:</span></span></div><div class="t m0 x28 hc y629 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(n,<span class="_ _2a"> </span>end<span class="fc6">=<span class="ff12 fcc">'<span class="_ _2a"> </span>'</span></span>)</span></div><div class="t m0 x28 hc y62a ff11 fs3 fc4 sc0 ls0 ws0">if<span class="_ _2a"> </span><span class="ffa fc0">n<span class="_ _2a"> </span><span class="fc6">><span class="_ _2a"> </span>30</span>:<span class="_ _2a"> </span></span>break</div><div class="t m0 x31 hc y62b ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fcc">"<span class="ff11 fc12">\n</span>doing<span class="_ _2a"> </span>something<span class="_ _2a"> </span>in<span class="_ _2a"> </span>between"</span>)</span></div><div class="t m0 x31 hc y62c ff11 fs3 fc4 sc0 ls0 ws0">for<span class="_ _2a"> </span><span class="ffa fc0">n<span class="_ _2a"> </span></span><span class="fcb">in<span class="_ _2a"> </span><span class="ffa fc0">G:</span></span></div><div class="t m0 x28 hc y62d ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(n,<span class="_ _2a"> </span>end<span class="fc6">=<span class="ff12 fcc">'<span class="_ _2a"> </span>'</span></span>)</span></div><div class="t m0 x11 hc y62e ffa fs3 fc0 sc0 ls0 ws0">0<span class="_ _2a"> </span>1<span class="_ _2a"> </span>4<span class="_ _2a"> </span>9<span class="_ _2a"> </span>16<span class="_ _2a"> </span>25<span class="_ _2a"> </span>36</div><div class="t m0 x11 hc y62f ffa fs3 fc0 sc0 ls0 ws0">doing<span class="_ _2a"> </span>something<span class="_ _2a"> </span>in<span class="_ _2a"> </span>between</div><div class="t m0 x11 hc y630 ffa fs3 fc0 sc0 ls0 ws0">49<span class="_ _2a"> </span>64<span class="_ _2a"> </span>81<span class="_ _2a"> </span>100<span class="_ _2a"> </span>121</div><div class="t m0 x4 h4 y9a ff5 fs3 fc0 sc0 ls0 ws0">一個我發現這個特性非常實用的場合是在對硬碟上的資料檔案集合進行處理時,這個特性</div><div class="t m0 x5 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">意味著你可以非常輕鬆地批量處理資料,讓產生器負責追蹤那些你尚未處理的區塊。</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf51" class="pf w0 h0" data-page-no="51"><div class="pc pc51 w0 h0"><img class="bi x5 ydc w1 h48" alt="" src="images/bg51.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">74<span class="_ _83"> </span><span class="ffc">13.<span class="_ _16"> </span><span class="ff6">產生器</span></span></div><div class="t m0 x5 h8 ybb ff1 fs1 fc0 sc0 ls0 ws0">13.2<span class="_ _39"> </span><span class="ff2">產生器函式:使用關鍵字<span class="_ _10"> </span><span class="ff11">yield</span></span></div><div class="t m0 x4 h4 ybc ff5 fs3 fc0 sc0 ls0 ws0">在之前的章節中我們看到了列表推導是建立相對簡單的列表的最佳途徑,而常規的<span class="_ _3"> </span><span class="ffa">for<span class="_ _9"> </span></span>循</div><div class="t m0 x5 h4 ybd ff5 fs3 fc0 sc0 ls0 ws0">環在更加複雜的情況下更為合適。對於產生器表達式來說這也是成立的:我們可以通過產生器</div><div class="t m0 x5 h4 ybe ff5 fs3 fc0 sc0 ls0 ws0">函式<span class="_ _9"> </span>使用<span class="_ _3"> </span><span class="ffa">yield<span class="_ _9"> </span></span>語句來建立更加複雜的產生器。</div><div class="t m0 x4 h4 ybf ff5 fs3 fc0 sc0 ls0 ws0">在這裡我們有兩種方式組成同一個列表:</div><div class="t m0 x11 hc y631 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[13]:<span class="_ _2a"> </span><span class="fc0">L1<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[n<span class="_ _2a"> </span><span class="fc6">**<span class="_ _2a"> </span>2<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span></span>n<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span><span class="fc4">range</span>(<span class="fc6">12</span>)]</span></div><div class="t m0 x31 hc y632 ffa fs3 fc0 sc0 ls0 ws0">L2<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[]</div><div class="t m0 x31 hc y633 ff11 fs3 fc4 sc0 ls0 ws0">for<span class="_ _2a"> </span><span class="ffa fc0">n<span class="_ _2a"> </span></span><span class="fcb">in<span class="_ _2a"> </span></span><span class="ffa">range<span class="fc0">(<span class="fc6">12</span>):</span></span></div><div class="t m0 x28 hc y634 ffa fs3 fc0 sc0 ls0 ws0">L2<span class="fc6">.</span>append(n<span class="_ _2a"> </span><span class="fc6">**<span class="_ _2a"> </span>2</span>)</div><div class="t m0 x31 hc y635 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(L1)</span></div><div class="t m0 x31 hc y636 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(L2)</span></div><div class="t m0 x11 hc y637 ffa fs3 fc0 sc0 ls0 ws0">[0,<span class="_ _2a"> </span>1,<span class="_ _2a"> </span>4,<span class="_ _2a"> </span>9,<span class="_ _2a"> </span>16,<span class="_ _2a"> </span>25,<span class="_ _2a"> </span>36,<span class="_ _2a"> </span>49,<span class="_ _2a"> </span>64,<span class="_ _2a"> </span>81,<span class="_ _2a"> </span>100,<span class="_ _2a"> </span>121]</div><div class="t m0 x11 hc y638 ffa fs3 fc0 sc0 ls0 ws0">[0,<span class="_ _2a"> </span>1,<span class="_ _2a"> </span>4,<span class="_ _2a"> </span>9,<span class="_ _2a"> </span>16,<span class="_ _2a"> </span>25,<span class="_ _2a"> </span>36,<span class="_ _2a"> </span>49,<span class="_ _2a"> </span>64,<span class="_ _2a"> </span>81,<span class="_ _2a"> </span>100,<span class="_ _2a"> </span>121]</div><div class="t m0 x4 h4 y639 ff5 fs3 fc0 sc0 ls0 ws0">類似地,我們有兩種方法組成等價的產生器:</div><div class="t m0 x11 hc y63a ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[14]:<span class="_ _2a"> </span><span class="fc0">G1<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>(n<span class="_ _2a"> </span><span class="fc6">**<span class="_ _2a"> </span>2<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span></span>n<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span><span class="fc4">range</span>(<span class="fc6">12</span>))</span></div><div class="t m0 x31 hc y63b ff11 fs3 fc4 sc0 ls0 ws0">def<span class="_ _2a"> </span><span class="ffa fc5">gen<span class="fc0">():</span></span></div><div class="t m0 x28 hc y63c ff11 fs3 fc4 sc0 ls0 ws0">for<span class="_ _2a"> </span><span class="ffa fc0">n<span class="_ _2a"> </span></span><span class="fcb">in<span class="_ _2a"> </span></span><span class="ffa">range<span class="fc0">(<span class="fc6">12</span>):</span></span></div><div class="t m0 x3a hc y63d ff11 fs3 fc4 sc0 ls0 ws0">yield<span class="_ _2a"> </span><span class="ffa fc0">n<span class="_ _2a"> </span><span class="fc6">**<span class="_ _2a"> </span>2</span></span></div><div class="t m0 x31 hc y63e ffa fs3 fc0 sc0 ls0 ws0">G2<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>gen()</div><div class="t m0 x31 hc y63f ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fc6">*</span>G1)</span></div><div class="t m0 x31 hc y640 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fc6">*</span>G2)</span></div><div class="t m0 x11 hc y641 ffa fs3 fc0 sc0 ls0 ws0">0<span class="_ _2a"> </span>1<span class="_ _2a"> </span>4<span class="_ _2a"> </span>9<span class="_ _2a"> </span>16<span class="_ _2a"> </span>25<span class="_ _2a"> </span>36<span class="_ _2a"> </span>49<span class="_ _2a"> </span>64<span class="_ _2a"> </span>81<span class="_ _2a"> </span>100<span class="_ _2a"> </span>121</div><div class="t m0 x11 hc y642 ffa fs3 fc0 sc0 ls0 ws0">0<span class="_ _2a"> </span>1<span class="_ _2a"> </span>4<span class="_ _2a"> </span>9<span class="_ _2a"> </span>16<span class="_ _2a"> </span>25<span class="_ _2a"> </span>36<span class="_ _2a"> </span>49<span class="_ _2a"> </span>64<span class="_ _2a"> </span>81<span class="_ _2a"> </span>100<span class="_ _2a"> </span>121</div><div class="t m0 x4 h4 y643 ff5 fs3 fc0 sc0 ls0 ws0">一個產生器函式是這樣的一個函式:它不使用<span class="_ _9"> </span><span class="ffa">return<span class="_ _3"> </span></span>回傳一個值(僅回傳一次)<span class="_ _33"></span>,而是使</div><div class="t m0 x5 h4 y644 ff5 fs3 fc0 sc0 ls0 ws0">用<span class="_ _9"> </span><span class="ffa">yield<span class="_ _3"> </span></span>來產生一個包含(可能是無窮多的)數值的序列。和產生器表達式中一樣,在部分迭</div><div class="t m0 x5 h4 y645 ff5 fs3 fc0 sc0 ls0 ws0">代</div><div class="t m0 x68 hb y646 ffe fs4 fc1 sc0 ls0 ws0">1</div><div class="t m0 xa h4 y645 ff5 fs3 fc0 sc0 ls0 ws0">的間隔中產生器的狀態得到保留。<span class="_ _8"></span>但是如果我們需要產生器的一個全新的副本,我們只需要</div><div class="t m0 x5 h4 y647 ff5 fs3 fc0 sc0 ls0 ws0">簡單地重新呼叫那個函式即可。</div><div class="t m0 x5 h8 y648 ff1 fs1 fc0 sc0 ls0 ws0">13.3<span class="_ _39"> </span><span class="ff2">案例學習:質數產生器</span></div><div class="t m0 x4 h4 y649 ff5 fs3 fc0 sc0 ls0 ws0">這裡我將展示我最喜歡的一個產生器函數的例子:一個函數來產生無限的質數序列。解決</div><div class="t m0 x5 h4 y64a ff5 fs3 fc0 sc0 ls0 ws0">這個問題的一個經典演算法是愛拉託遜斯篩法,它的工作過程類似下面的程式碼:</div><div class="t m0 x12 hd yf9 fff fs5 fc0 sc0 ls0 ws0">1</div><div class="t m0 xa he y40 ff5 fs6 fc0 sc0 ls0 ws0">譯者注:兩次迭代的中間,此時產生器並沒有被從頭到尾執行完,故稱部分迭代。</div><a class="l" href="#pf51" data-dest-detail='[81,"XYZ",88.59,85.15,null]'><div class="d m1" style="border-style:none;position:absolute;left:82.909000px;bottom:191.176000px;width:4.234000px;height:5.136000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf52" class="pf w0 h0" data-page-no="52"><div class="pc pc52 w0 h0"><img class="bi x5 y64b w1 h49" alt="" src="images/bg52.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">13.3.<span class="_ _16"> </span><span class="ff6">案例學習:質數產生器<span class="_ _87"> </span><span class="ff7">75</span></span></div><div class="t m0 x11 h6 yfa ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[15]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">產生一個包含候選數值的列表</span></span></div><div class="t m0 x31 hc yfb ffa fs3 fc0 sc0 ls0 ws0">L<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[n<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span>n<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span><span class="fc4">range</span>(<span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">40</span>)]</div><div class="t m0 x31 hc yfc ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(L)</span></div><div class="t m0 x11 hc yfd ffa fs3 fc0 sc0 ls0 ws0">[2,<span class="_ _2a"> </span>3,<span class="_ _2a"> </span>4,<span class="_ _2a"> </span>5,<span class="_ _2a"> </span>6,<span class="_ _2a"> </span>7,<span class="_ _2a"> </span>8,<span class="_ _2a"> </span>9,<span class="_ _2a"> </span>10,<span class="_ _2a"> </span>11,<span class="_ _2a"> </span>12,<span class="_ _2a"> </span>13,<span class="_ _2a"> </span>14,<span class="_ _2a"> </span>15,<span class="_ _2a"> </span>16,<span class="_ _2a"> </span>17,<span class="_ _2a"> </span>18,<span class="_ _2a"> </span>19,<span class="_ _2a"> </span>20,<span class="_ _2a"> </span>21,<span class="_ _2a"> </span>22,</div><div class="t m0 x11 hc yfe ffa fs3 fc0 sc0 ls0 ws0">23,<span class="_ _2a"> </span>24,<span class="_ _2a"> </span>25,<span class="_ _2a"> </span>26,<span class="_ _2a"> </span>27,<span class="_ _2a"> </span>28,<span class="_ _2a"> </span>29,<span class="_ _2a"> </span>30,<span class="_ _2a"> </span>31,<span class="_ _2a"> </span>32,<span class="_ _2a"> </span>33,<span class="_ _2a"> </span>34,<span class="_ _2a"> </span>35,<span class="_ _2a"> </span>36,<span class="_ _2a"> </span>37,<span class="_ _2a"> </span>38,<span class="_ _2a"> </span>39]</div><div class="t m0 x11 h6 y100 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[16]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">對於候選數值序列的第一個值,移除它的倍數</span></span></div><div class="t m0 x31 hc y101 ffa fs3 fc0 sc0 ls0 ws0">L<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[n<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span>n<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span>L<span class="_ _2a"> </span><span class="ff11 fc4">if<span class="_ _2a"> </span></span>n<span class="_ _2a"> </span><span class="fc6">==<span class="_ _2a"> </span></span>L[<span class="fc6">0</span>]<span class="_ _2a"> </span><span class="ff11 fcb">or<span class="_ _2a"> </span></span>n<span class="_ _2a"> </span><span class="fc6">%<span class="_ _2a"> </span></span>L[<span class="fc6">0</span>]<span class="_ _2a"> </span><span class="fc6">><span class="_ _2a"> </span>0</span>]</div><div class="t m0 x31 hc y64c ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(L)</span></div><div class="t m0 x11 hc y30a ffa fs3 fc0 sc0 ls0 ws0">[2,<span class="_ _2a"> </span>3,<span class="_ _2a"> </span>5,<span class="_ _2a"> </span>7,<span class="_ _2a"> </span>9,<span class="_ _2a"> </span>11,<span class="_ _2a"> </span>13,<span class="_ _2a"> </span>15,<span class="_ _2a"> </span>17,<span class="_ _2a"> </span>19,<span class="_ _2a"> </span>21,<span class="_ _2a"> </span>23,<span class="_ _2a"> </span>25,<span class="_ _2a"> </span>27,<span class="_ _2a"> </span>29,<span class="_ _2a"> </span>31,<span class="_ _2a"> </span>33,<span class="_ _2a"> </span>35,<span class="_ _2a"> </span>37,<span class="_ _2a"> </span>39]</div><div class="t m0 x11 h6 y30b ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[17]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">對於候選數值序列的第二個值,移除它的倍數</span></span></div><div class="t m0 x31 hc y64d ffa fs3 fc0 sc0 ls0 ws0">L<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[n<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span>n<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span>L<span class="_ _2a"> </span><span class="ff11 fc4">if<span class="_ _2a"> </span></span>n<span class="_ _2a"> </span><span class="fc6">==<span class="_ _2a"> </span></span>L[<span class="fc6">1</span>]<span class="_ _2a"> </span><span class="ff11 fcb">or<span class="_ _2a"> </span></span>n<span class="_ _2a"> </span><span class="fc6">%<span class="_ _2a"> </span></span>L[<span class="fc6">1</span>]<span class="_ _2a"> </span><span class="fc6">><span class="_ _2a"> </span>0</span>]</div><div class="t m0 x31 hc y30c ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(L)</span></div><div class="t m0 x11 hc y64e ffa fs3 fc0 sc0 ls0 ws0">[2,<span class="_ _2a"> </span>3,<span class="_ _2a"> </span>5,<span class="_ _2a"> </span>7,<span class="_ _2a"> </span>11,<span class="_ _2a"> </span>13,<span class="_ _2a"> </span>17,<span class="_ _2a"> </span>19,<span class="_ _2a"> </span>23,<span class="_ _2a"> </span>25,<span class="_ _2a"> </span>29,<span class="_ _2a"> </span>31,<span class="_ _2a"> </span>35,<span class="_ _2a"> </span>37]</div><div class="t m0 x11 h6 y64f ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[18]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">對於候選數值序列的第三個值,移除它的倍數</span></span></div><div class="t m0 x31 hc y30f ffa fs3 fc0 sc0 ls0 ws0">L<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[n<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span>n<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span>L<span class="_ _2a"> </span><span class="ff11 fc4">if<span class="_ _2a"> </span></span>n<span class="_ _2a"> </span><span class="fc6">==<span class="_ _2a"> </span></span>L[<span class="fc6">2</span>]<span class="_ _2a"> </span><span class="ff11 fcb">or<span class="_ _2a"> </span></span>n<span class="_ _2a"> </span><span class="fc6">%<span class="_ _2a"> </span></span>L[<span class="fc6">2</span>]<span class="_ _2a"> </span><span class="fc6">><span class="_ _2a"> </span>0</span>]</div><div class="t m0 x31 hc y310 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(L)</span></div><div class="t m0 x11 hc y311 ffa fs3 fc0 sc0 ls0 ws0">[2,<span class="_ _2a"> </span>3,<span class="_ _2a"> </span>5,<span class="_ _2a"> </span>7,<span class="_ _2a"> </span>11,<span class="_ _2a"> </span>13,<span class="_ _2a"> </span>17,<span class="_ _2a"> </span>19,<span class="_ _2a"> </span>23,<span class="_ _2a"> </span>29,<span class="_ _2a"> </span>31,<span class="_ _2a"> </span>37]</div><div class="t m0 x4 h4 y650 ff5 fs3 fc0 sc0 ls0 ws0">如果我們不斷在一個足夠大的列表上重複這個過程足夠多次,我們就能產生足夠多我們需</div><div class="t m0 x5 h4 y651 ff5 fs3 fc0 sc0 ls0 ws0">要的質數。</div><div class="t m0 x4 h4 y652 ff5 fs3 fc0 sc0 ls0 ws0">讓我們把計算的邏輯封裝成為一個產生器表示式:</div><div class="t m0 x11 hc y653 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[19]:<span class="_ _2a"> </span><span class="ff11 fc4">def<span class="_ _2a"> </span></span><span class="fc5">gen_primes<span class="fc0">(N):</span></span></div><div class="t m0 x28 hc y654 ff14 fs3 fcc sc0 ls0 ws0">"""Generate<span class="_ _2a"> </span>primes<span class="_ _2a"> </span>up<span class="_ _2a"> </span>to<span class="_ _2a"> </span>N"""</div><div class="t m0 x28 hc y655 ffa fs3 fc0 sc0 ls0 ws0">primes<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="fc4">set</span></span>()</div><div class="t m0 x28 hc y656 ff11 fs3 fc4 sc0 ls0 ws0">for<span class="_ _2a"> </span><span class="ffa fc0">n<span class="_ _2a"> </span></span><span class="fcb">in<span class="_ _2a"> </span></span><span class="ffa">range<span class="fc0">(<span class="fc6">2</span>,<span class="_ _2a"> </span>N):</span></span></div><div class="t m0 x3a hc y657 ff11 fs3 fc4 sc0 ls0 ws0">if<span class="_ _2a"> </span><span class="ffa">all<span class="fc0">(n<span class="_ _2a"> </span><span class="fc6">%<span class="_ _2a"> </span></span>p<span class="_ _2a"> </span><span class="fc6">><span class="_ _2a"> </span>0<span class="_ _2a"> </span></span></span></span>for<span class="_ _2a"> </span><span class="ffa fc0">p<span class="_ _2a"> </span></span><span class="fcb">in<span class="_ _2a"> </span><span class="ffa fc0">primes):</span></span></div><div class="t m0 x3d hc y658 ffa fs3 fc0 sc0 ls0 ws0">primes<span class="fc6">.</span>add(n)</div><div class="t m0 x3d hc y659 ff11 fs3 fc4 sc0 ls0 ws0">yield<span class="_ _2a"> </span><span class="ffa fc0">n</span></div><div class="t m0 x31 hc y65a ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(<span class="fc6">*</span>gen_primes(<span class="fc6">100</span>))</span></div><div class="t m0 x11 hc y65b ffa fs3 fc0 sc0 ls0 ws0">2<span class="_ _2a"> </span>3<span class="_ _2a"> </span>5<span class="_ _2a"> </span>7<span class="_ _2a"> </span>11<span class="_ _2a"> </span>13<span class="_ _2a"> </span>17<span class="_ _2a"> </span>19<span class="_ _2a"> </span>23<span class="_ _2a"> </span>29<span class="_ _2a"> </span>31<span class="_ _2a"> </span>37<span class="_ _2a"> </span>41<span class="_ _2a"> </span>43<span class="_ _2a"> </span>47<span class="_ _2a"> </span>53<span class="_ _2a"> </span>59<span class="_ _2a"> </span>61<span class="_ _2a"> </span>67<span class="_ _2a"> </span>71<span class="_ _2a"> </span>73<span class="_ _2a"> </span>79<span class="_ _2a"> </span>83<span class="_ _2a"> </span>89<span class="_ _2a"> </span>97</div><div class="t m0 x4 h4 y9a ff5 fs3 fc0 sc0 ls0 ws0">這就是關於產生器的一切!盡管這絕對不是一個高效實現愛拉託遜斯篩法的方法,但它說</div><div class="t m0 x5 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">明了如何使用產生器函式的語法來方便地建立更複雜的序列。</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf53" class="pf w0 h0" data-page-no="53"><div class="pc pc53 w0 h0"></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf54" class="pf w0 h0" data-page-no="54"><div class="pc pc54 w0 h0"><img class="bi x5 y9b w1 h4a" alt="" src="images/bg54.png"/><div class="t m0 x69 h1 y65c ff2 fs0 fc0 sc0 ls0 ws0">第<span class="_ _0"> </span><span class="ff1">14<span class="_ _0"> </span></span>章<span class="_ _32"> </span>模組和套件</div><div class="t m0 x4 h4 y65d ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _10"> </span><span class="ff5">有<span class="_ _55"></span>一<span class="_ _4a"></span>個<span class="_ _55"> </span>特<span class="_ _55"> </span>性<span class="_ _55"> </span>使<span class="_ _55"> </span>其<span class="_ _55"> </span>可<span class="_ _55"></span>以<span class="_ _4a"></span>用<span class="_ _55"> </span>於<span class="_ _55"> </span>各<span class="_ _55"> </span>種<span class="_ _55"> </span>各<span class="_ _55"> </span>樣<span class="_ _55"></span>的<span class="_ _55"></span>任<span class="_ _4a"></span>務,<span class="_ _55"> </span>那<span class="_ _55"> </span>就<span class="_ _55"> </span>是<span class="_ _55"> </span>“自<span class="_ _55"> </span>帶<span class="_ _55"></span>電<span class="_ _4a"></span>池”</span></div><div class="t m0 x6 hb y65e ffe fs4 fc1 sc0 ls0 ws0">1</div><div class="t m0 x6b h4 y65d ff5 fs3 fc0 sc0 ls0 ws0">。<span class="_ _55"></span>也<span class="_ _4a"></span>就<span class="_ _55"> </span>是</div><div class="t m0 x5 h4 y65f ff5 fs3 fc0 sc0 ls0 ws0">說<span class="_ _72"> </span><span class="ff7">Python<span class="_ _72"> </span></span>標<span class="_ _4a"></span>準<span class="ff6">函式庫</span>(<span class="ff7">library</span>)包<span class="_ _4a"></span>含了<span class="_ _4a"></span>各種<span class="_ _4a"></span>各樣任<span class="_ _4a"></span>務的<span class="_ _4a"></span>有用<span class="_ _4a"></span>工具。除<span class="_ _4a"></span>此之<span class="_ _4a"></span>外,<span class="ff7">Python<span class="_ _72"> </span></span>還有<span class="_ _4a"></span>一</div><div class="t m0 x5 h4 y660 ff5 fs3 fc0 sc0 ls0 ws0">個廣泛的第三方工具和套件的生態系統,可提供更專業化的功能。我們來看看如何導入標</div><div class="t m0 x5 h4 y661 ff5 fs3 fc0 sc0 ls0 ws0">準函式庫模組、安裝第三方模組的工具以及如何製作自己的模組。</div><div class="t m0 x5 h8 y662 ff1 fs1 fc0 sc0 ls0 ws0">14.1<span class="_ _39"> </span><span class="ff2">匯入模組:<span class="ff11">import<span class="_ _10"> </span></span>語句</span></div><div class="t m0 x4 h4 y663 ff5 fs3 fc0 sc0 ls0 ws0">為了載入內建和第三方模組,<span class="ff7">Python<span class="_ _9"> </span></span>提供了<span class="_ _3"> </span><span class="ffa">import<span class="_ _9"> </span></span>語句。我們在這裡按最推薦到最不推</div><div class="t m0 x5 h4 y664 ff5 fs3 fc0 sc0 ls0 ws0">薦的順序簡要介紹一下使用這個語句的一些方法。</div><div class="t m0 x5 ha y665 ff1 fs2 fc0 sc0 ls0 ws0">14.1.1<span class="_ _3c"> </span><span class="ff2">明確模組匯入</span></div><div class="t m0 x4 h4 y666 ff5 fs3 fc0 sc0 ls0 ws0">模塊的顯式導入會將模塊的內容保留在<span class="ff6">命名空間</span>(<span class="ff7">namespace</span>)中。你接下來可以使用這</div><div class="t m0 x5 h4 y667 ff5 fs3 fc0 sc0 ls0 ws0">個名稱空間來引用它們的內容:通過命名空間加<span class="_ _3"> </span><span class="ffa">.<span class="_ _9"> </span></span>加某個具體的內容。例如,這裡我們將導入</div><div class="t m0 x5 h4 y668 ff5 fs3 fc0 sc0 ls0 ws0">內建的<span class="_ _9"> </span><span class="ffa">math<span class="_ _3"> </span></span>模組並計算<span class="_ _9"> </span><span class="ffd">π<span class="_ _3"> </span></span>的餘弦值:</div><div class="t m0 x11 hc y669 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[1]:<span class="_ _2a"> </span><span class="ff11 fc4">import<span class="_ _2a"> </span><span class="fc5">math</span></span></div><div class="t m0 x24 hc y66a ffa fs3 fc0 sc0 ls0 ws0">math<span class="fc6">.</span>cos(math<span class="fc6">.</span>pi)</div><div class="t m0 x11 hc y66b ffa fs3 fcf sc0 ls0 ws0">Out[1]:<span class="_ _2a"> </span><span class="fc0">-1.0</span></div><div class="t m0 x5 ha y66c ff1 fs2 fc0 sc0 ls0 ws0">14.1.2<span class="_ _3c"> </span><span class="ff2">明確模組匯入並取別名</span></div><div class="t m0 x4 h4 y66d ff5 fs3 fc0 sc0 ls0 ws0">如果模塊有更長的名稱,那麼每次訪問某個元素時使用完整模組名稱並不方便。因此,我</div><div class="t m0 x5 h4 y66e ff5 fs3 fc0 sc0 ls0 ws0">們通常會使用<span class="_ _34"> </span><span class="ffa">import...<span class="_ _2a"> </span>as...<span class="_ _34"> </span></span>來為名稱空間建立一個較短的別名。<span class="_ _3e"></span>例如<span class="_ _34"> </span><span class="ff7">NumPy<span class="_ _8"></span><span class="ff5">(<span class="ff7">Numerical</span></span></span></div><div class="t m0 x5 h4 y66f ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="ff5">)套件,一種資料科學領域中很受歡迎的第三方套件,我們可以按照慣例使用別</span></div><div class="t m0 x5 h4 y670 ff5 fs3 fc0 sc0 ls0 ws0">名<span class="_ _9"> </span><span class="ffa">np<span class="_ _3"> </span></span>匯入:</div><div class="t m0 x11 hc y671 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[2]:<span class="_ _2a"> </span><span class="ff11 fc4">import<span class="_ _2a"> </span><span class="fc5">numpy<span class="_ _2a"> </span></span>as<span class="_ _2a"> </span><span class="fc5">np</span></span></div><div class="t m0 x24 hc y672 ffa fs3 fc0 sc0 ls0 ws0">np<span class="fc6">.</span>cos(np<span class="fc6">.</span>pi)</div><div class="t m0 x11 hc y673 ffa fs3 fcf sc0 ls0 ws0">Out[2]:<span class="_ _2a"> </span><span class="fc0">-1.0</span></div><div class="t m0 x12 hd yb9 fff fs5 fc0 sc0 ls0 ws0">1</div><div class="t m0 xa he yba ff5 fs6 fc0 sc0 ls0 ws0">譯者<span class="_ _4a"> </span>注:<span class="_ _4a"></span>這裡<span class="_ _4a"></span>原文<span class="_ _4a"> </span>為<span class="_ _13"> </span><span class="ff10">batteries<span class="_ _13"> </span>included</span>,可<span class="_ _4a"></span>以理<span class="_ _4a"> </span>解<span class="_ _4a"></span>為內<span class="_ _4a"></span>置<span class="_ _4a"></span>了許<span class="_ _4a"></span>多功<span class="_ _4a"></span>能,參<span class="_ _4a"> </span>見<span class="_ _13"> </span><span class="ff10">PEP<span class="_ _13"> </span>206</span>:<span class="ff10">Python<span class="_ _a"> </span></span>高<span class="_ _4a"> </span>級函式庫:</div><div class="t m0 x5 he y40 ff13 fs6 fc2 sc0 ls0 ws0">https://www.python.org/dev/peps/pep-0206/#id3<span class="ff5 fc0">。</span></div><div class="t m0 x2f h7 y20 ff7 fs3 fc0 sc0 ls0 ws0">77</div><a class="l" href="#pf54" data-dest-detail='[84,"XYZ",88.59,98.3,null]'><div class="d m1" style="border-style:none;position:absolute;left:470.999000px;bottom:623.019000px;width:4.234000px;height:5.136000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="https://www.python.org/dev/peps/pep-0206/#id3"><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:70.007000px;width:212.329000px;height:8.220000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf55" class="pf w0 h0" data-page-no="55"><div class="pc pc55 w0 h0"><img class="bi x5 y674 w1 h4b" alt="" src="images/bg55.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">78<span class="_ _85"> </span><span class="ffc">14.<span class="_ _16"> </span><span class="ff6">模組和套件</span></span></div><div class="t m0 x5 ha y2ca ff1 fs2 fc0 sc0 ls0 ws0">14.1.3<span class="_ _3c"> </span><span class="ff2">明確匯入模組內容</span></div><div class="t m0 x4 h4 y675 ff5 fs3 fc0 sc0 ls0 ws0">有時你<span class="_ _4a"></span>只想從模<span class="_ _4a"></span>塊導入一<span class="_ _4a"></span>些特定的<span class="_ _4a"></span>項目,而不<span class="_ _4a"></span>是導入模<span class="_ _4a"></span>塊的整個命<span class="_ _4a"></span>名空間。這<span class="_ _4a"></span>可以通</div><div class="t m0 x5 h4 y676 ff5 fs3 fc0 sc0 ls0 ws0">過<span class="_ _9"> </span><span class="ffa">from...<span class="_ _2a"> </span>import...<span class="_ _3"> </span></span>來完成。例如,我們可以從<span class="_ _9"> </span><span class="ffa">math<span class="_ _9"> </span></span>模組匯入<span class="_ _3"> </span><span class="ffa">cos<span class="_ _9"> </span></span>函式和<span class="_ _3"> </span><span class="ffa">pi<span class="_ _9"> </span></span>常數:</div><div class="t m0 x11 hc y677 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[3]:<span class="_ _2a"> </span><span class="ff11 fc4">from<span class="_ _2a"> </span><span class="fc5">math<span class="_ _2a"> </span></span>import<span class="_ _2a"> </span></span><span class="fc0">cos,<span class="_ _2a"> </span>pi</span></div><div class="t m0 x24 hc y678 ffa fs3 fc0 sc0 ls0 ws0">cos(pi)</div><div class="t m0 x11 hc y679 ffa fs3 fcf sc0 ls0 ws0">Out[3]:<span class="_ _2a"> </span><span class="fc0">-1.0</span></div><div class="t m0 x5 ha y67a ff1 fs2 fc0 sc0 ls0 ws0">14.1.4<span class="_ _3c"> </span><span class="ff2">隱式匯入模組內容</span></div><div class="t m0 x4 h4 y67b ff5 fs3 fc0 sc0 ls0 ws0">最後,<span class="_ _3e"></span>有時候你也可以將整個模組內容匯入本地的名稱空間。<span class="_ _35"></span>這可以通過<span class="_ _34"> </span><span class="ffa">from...<span class="_ _2a"> </span>import</span></div><div class="t m0 x5 h4 y67c ffa fs3 fc0 sc0 ls0 ws0">*<span class="_ _9"> </span><span class="ff5">來完成:</span></div><div class="t m0 x11 hc y67d ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[4]:<span class="_ _2a"> </span><span class="ff11 fc4">from<span class="_ _2a"> </span><span class="fc5">math<span class="_ _2a"> </span></span>import<span class="_ _2a"> </span></span><span class="fc6">*</span></div><div class="t m0 x24 hc y67e ffa fs3 fc0 sc0 ls0 ws0">sin(pi)<span class="_ _2a"> </span><span class="fc6">**<span class="_ _2a"> </span>2<span class="_ _2a"> </span>+<span class="_ _2a"> </span></span>cos(pi)<span class="_ _2a"> </span><span class="fc6">**<span class="_ _2a"> </span>2</span></div><div class="t m0 x11 hc y67f ffa fs3 fcf sc0 ls0 ws0">Out[4]:<span class="_ _2a"> </span><span class="fc0">1.0</span></div><div class="t m0 x4 h4 y680 ff5 fs3 fc0 sc0 ls0 ws0">你應當謹慎使用這種方法。由此帶來的問題是,這樣匯入有時可能會覆蓋你不想覆蓋的函</div><div class="t m0 x5 h4 y681 ff5 fs3 fc0 sc0 ls0 ws0">數名稱,並且這個語句的隱蔽性使得你很難確定發生了什麼變化。例如,<span class="ff7">Python<span class="_ _13"> </span></span>具有內建</div><div class="t m0 x5 h4 y682 ff5 fs3 fc0 sc0 ls0 ws0">的<span class="_ _9"> </span><span class="ff7">“sum”<span class="_ _3"> </span></span>功能,可用於各種操作:</div><div class="t m0 x11 hc y683 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[5]:<span class="_ _2a"> </span><span class="fc0">help(<span class="fc4">sum</span>)</span></div><div class="t m0 x11 hc y684 ffa fs3 fc0 sc0 ls0 ws0">Help<span class="_ _2a"> </span>on<span class="_ _2a"> </span>built-in<span class="_ _2a"> </span>function<span class="_ _2a"> </span>sum<span class="_ _2a"> </span>in<span class="_ _2a"> </span>module<span class="_ _2a"> </span>builtins:</div><div class="t m0 x11 hc y685 ffa fs3 fc0 sc0 ls0 ws0">sum(...)</div><div class="t m0 x29 hc y686 ffa fs3 fc0 sc0 ls0 ws0">sum(iterable[,<span class="_ _2a"> </span>start])<span class="_ _2a"> </span>-><span class="_ _2a"> </span>value</div><div class="t m0 x29 hc y687 ffa fs3 fc0 sc0 ls0 ws0">Return<span class="_ _2a"> </span>the<span class="_ _2a"> </span>sum<span class="_ _2a"> </span>of<span class="_ _2a"> </span>an<span class="_ _2a"> </span>iterable<span class="_ _2a"> </span>of<span class="_ _2a"> </span>numbers<span class="_ _2a"> </span>(NOT<span class="_ _2a"> </span>strings)<span class="_ _2a"> </span>plus<span class="_ _2a"> </span>the<span class="_ _2a"> </span>value</div><div class="t m0 x29 hc y688 ffa fs3 fc0 sc0 ls0 ws0">of<span class="_ _2a"> </span>parameter<span class="_ _2a"> </span><span class="ff12">'</span>start<span class="ff12">'<span class="_ _2a"> </span></span>(which<span class="_ _2a"> </span>defaults<span class="_ _2a"> </span>to<span class="_ _2a"> </span>0).<span class="_ _27"> </span>When<span class="_ _2a"> </span>the<span class="_ _2a"> </span>iterable<span class="_ _2a"> </span>is</div><div class="t m0 x29 hc y689 ffa fs3 fc0 sc0 ls0 ws0">empty,<span class="_ _2a"> </span>return<span class="_ _2a"> </span>start.</div><div class="t m0 x4 h4 y68a ff5 fs3 fc0 sc0 ls0 ws0">我們可以用這個函式從某個指定的初始值開始計算一個序列的和,在這裡我們從<span class="_ _9"> </span><span class="ff16">−<span class="ff15">1<span class="_ _3"> </span></span></span>開始:</div><div class="t m0 x11 hc y68b ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[6]:<span class="_ _2a"> </span><span class="fc4">sum<span class="fc0">(</span>range<span class="fc0">(<span class="fc6">5</span>),<span class="_ _2a"> </span><span class="fc6">-1</span>)</span></span></div><div class="t m0 x11 hc y68c ffa fs3 fcf sc0 ls0 ws0">Out[6]:<span class="_ _2a"> </span><span class="fc0">9</span></div><div class="t m0 x4 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">現在觀察如果我們從<span class="_ _9"> </span><span class="ffa">numpy<span class="_ _3"> </span></span>匯入全部內容後進行<span class="ff2">完全相同的函式呼叫</span>會發生什麼:</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf56" class="pf w0 h0" data-page-no="56"><div class="pc pc56 w0 h0"><img class="bi x5 y68d w1 h4c" alt="" src="images/bg56.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">14.2.<span class="_ _16"> </span><span class="ff6">匯入<span class="_ _9"> </span></span>PYTHON<span class="_ _9"> </span><span class="ff6">標準函式庫<span class="_ _88"> </span><span class="ff7">79</span></span></div><div class="t m0 x11 hc yfa ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[7]:<span class="_ _2a"> </span><span class="ff11 fc4">from<span class="_ _2a"> </span><span class="fc5">numpy<span class="_ _2a"> </span></span>import<span class="_ _2a"> </span></span><span class="fc6">*</span></div><div class="t m0 x11 hc yfc ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[8]:<span class="_ _2a"> </span><span class="fc4">sum<span class="fc0">(</span>range<span class="fc0">(<span class="fc6">5</span>),<span class="_ _2a"> </span><span class="fc6">-1</span>)</span></span></div><div class="t m0 x11 hc yfd ffa fs3 fcf sc0 ls0 ws0">Out[8]:<span class="_ _2a"> </span><span class="fc0">10</span></div><div class="t m0 x4 h4 y68e ff5 fs3 fc0 sc0 ls0 ws0">結果與之前相差了<span class="_ _3"> </span><span class="ff15">1</span>。這是因為<span class="_ _3"> </span><span class="ffa">import<span class="_ _a"> </span></span>語句用<span class="_ _3"> </span><span class="ffa">numpy.sum<span class="_ _a"> </span></span>函數<span class="ff2">替換</span>了內建的<span class="_ _a"> </span><span class="ffa">sum<span class="_ _3"> </span></span>函數:</div><div class="t m0 x5 h4 y68f ff5 fs3 fc0 sc0 ls0 ws0">在前者中,<span class="_ _2"></span><span class="ffa">sum(5)<span class="_ _9"> </span><span class="ff5">從<span class="_ _9"> </span></span>-1<span class="_ _9"> </span><span class="ff5">開始對<span class="_ _9"> </span></span>range(5)<span class="_ _34"> </span><span class="ff5">求和;<span class="_ _8"></span>而在後者中,<span class="_ _2"></span>我們沿著最後一個軸<span class="_ _8"></span>(用<span class="_ _9"> </span><span class="ffa">-1<span class="_ _9"> </span></span>表</span></span></div><div class="t m0 x5 h4 y690 ff5 fs3 fc0 sc0 ls0 ws0">示)<span class="_ _4"></span>對<span class="_ _9"> </span><span class="ffa">range(5)<span class="_ _9"> </span></span>求和。<span class="_ _2"></span>這是使用<span class="_ _9"> </span><span class="ffa">import<span class="_ _9"> </span></span>時很難注意到的情況:<span class="_ _8"></span>為此,<span class="_ _2"></span>除非你確切知道你在做</div><div class="t m0 x5 h4 y691 ff5 fs3 fc0 sc0 ls0 ws0">什麼,否則最好避免這種情況。</div><div class="t m0 x5 h8 y692 ff1 fs1 fc0 sc0 ls0 ws0">14.2<span class="_ _39"> </span><span class="ff2">匯入<span class="_ _10"> </span></span>Python<span class="_ _10"> </span><span class="ff2">標準函式庫</span></div><div class="t m0 x4 h4 y693 ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _26"> </span><span class="ff5">的<span class="_ _60"> </span>標<span class="_ _60"> </span>準函式庫<span class="_ _60"> </span>包<span class="_ _60"> </span>含<span class="_ _89"> </span>很<span class="_ _89"> </span>多<span class="_ _60"> </span>內<span class="_ _60"> </span>置<span class="_ _89"> </span>的<span class="_ _60"> </span>模<span class="_ _60"> </span>塊,<span class="_ _60"> </span>你<span class="_ _89"> </span>可<span class="_ _60"> </span>以<span class="_ _60"> </span>在<span class="_ _26"> </span><span class="ffa fc2">https://docs.python.org/3/</span></span></div><div class="t m0 x5 h4 y694 ffa fs3 fc2 sc0 ls0 ws0">library/<span class="_ _13"> </span><span class="ff5 fc0">處看到它的完整說<span class="_ _4a"></span>明。它們都可以使用<span class="_ _13"> </span><span class="ffa">import<span class="_ _72"> </span></span>語句導入,然後你還可以使用前</span></div><div class="t m0 x5 h4 y695 ff5 fs3 fc0 sc0 ls0 ws0">面一節出現的<span class="_ _3"> </span><span class="ffa">help<span class="_ _3"> </span></span>功能來檢視幫助。以下是一個你可能希望了解和探索的一些模組的不完全</div><div class="t m0 x5 h4 y696 ff5 fs3 fc0 sc0 ls0 ws0">列表:</div><div class="t m0 x4 h4 y697 ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ffa">os<span class="_ _9"> </span><span class="ff5">和<span class="_ _3"> </span></span>sys<span class="ff5">:與作業系統互動的工具,包括檔案目錄間的巡覽和執行<span class="_ _9"> </span><span class="ff7">shell<span class="_ _9"> </span></span>命令。</span></span></div><div class="t m0 x4 h4 y698 ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ffa">math<span class="_ _9"> </span><span class="ff5">和<span class="_ _3"> </span></span>cmath<span class="ff5">:數學功能和對實數和複數的操作。</span></span></div><div class="t m0 x4 h4 y699 ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ffa">itertools<span class="ff5">:使用迭代器和產生器的工具。</span></span></div><div class="t m0 x4 h4 y69a ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ffa">functools<span class="ff5">:函數語言程式設計的工具。</span></span></div><div class="t m0 x4 h4 y69b ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ffa">random<span class="ff5">:產生隨機數的工具。</span></span></div><div class="t m0 x4 h4 y69c ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ffa">pickle<span class="ff5">:物件序列化的工具:儲存物件或者從磁碟中讀取物件。</span></span></div><div class="t m0 x4 h4 y69d ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ffa">json<span class="_ _9"> </span><span class="ff5">和<span class="_ _3"> </span></span>csv<span class="ff5">:讀取<span class="_ _9"> </span><span class="ff7">JSON<span class="_ _9"> </span></span>格式和<span class="_ _3"> </span><span class="ff7">CSV<span class="_ _9"> </span></span>格式檔案的工具。</span></span></div><div class="t m0 x4 h4 y69e ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ffa">urllib<span class="ff7">:<span class="_ _72"> </span><span class="ff5">進行<span class="_ _9"> </span></span>HTTP<span class="_ _3"> </span><span class="ff5">和其他網路請求的工具。</span></span></span></div><div class="t m0 x4 h4 y69f ff5 fs3 fc0 sc0 ls0 ws0">查閱<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>官方標準函式庫文件查閱更多資訊吧!</div><div class="t m0 x5 h8 y6a0 ff1 fs1 fc0 sc0 ls0 ws0">14.3<span class="_ _39"> </span><span class="ff2">匯入第三方模組</span></div><div class="t m0 x4 h4 y6a1 ff5 fs3 fc0 sc0 ls0 ws0">第三方模塊的生態系統使得<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>十分易用,這一點在資料科學領域表現最為明顯。這</div><div class="t m0 x5 h4 y6a2 ff5 fs3 fc0 sc0 ls0 ws0">些模組可以像內建模組一樣被匯入,當然前提是你在你的系統中已經安裝好了它們。</div><div class="t m0 x4 h4 y6a3 ff5 fs3 fc0 sc0 ls0 ws0">這些模組的標準來源是<span class="_ _60"> </span><span class="ff7">Python<span class="_ _60"> </span></span>套件索引<span class="_ _3e"></span>(<span class="ff7">Python<span class="_ _60"> </span>Pac<span class="_ _8"></span>kage<span class="_ _60"> </span>Index<span class="ff5">,<span class="_ _7"></span><span class="ff7">PyPI<span class="ff5">)<span class="_ _33"></span>,<span class="_ _5f"></span>網址為<span class="_ _36"> </span><span class="ffa fc2">https:</span></span></span></span></span></div><div class="t m0 x5 h4 y6a4 ffa fs3 fc2 sc0 ls0 ws0">//pypi.python.org/<span class="ff5 fc0">。為了更方便地使用,<span class="ff7">Python<span class="_ _3"> </span></span>提供了一個叫做<span class="_ _3"> </span><span class="ffa">pip<span class="_ _3"> </span></span>的程序(<span class="ff7">pip<span class="_ _3"> </span>installs</span></span></div><div class="t m0 x5 h4 y6a5 ff7 fs3 fc0 sc0 ls0 ws0">pac<span class="_ _4"></span>kages<span class="_ _a"> </span><span class="ff5">的遞歸縮寫)<span class="_ _45"></span>,它可以在<span class="_ _3"> </span><span class="ff7">PyPI<span class="_ _a"> </span></span>上自動獲取套件的<span class="_ _3"> </span><span class="ff7">release<span class="_ _a"> </span></span>版本。<span class="_ _44"></span>(如果你使用<span class="_ _a"> </span><span class="ff7">Python</span></span></div><div class="t m0 x5 h4 y6a6 ff7 fs3 fc0 sc0 ls0 ws0">2<span class="_ _9"> </span><span class="ff5">的話,你需要額外安裝<span class="_ _3"> </span><span class="ffa">pip</span>)<span class="_ _33"></span>。</span></div><div class="t m0 x4 h4 y6a7 ff5 fs3 fc0 sc0 ls0 ws0">比如,如果你想安裝我寫的<span class="_ _9"> </span><span class="ffa">supersmoother<span class="_ _3"> </span></span>套件,你只需要鍵入下面的命令:</div><div class="t m0 x11 hc y6a8 ffa fs3 fc0 sc0 ls0 ws0">$<span class="_ _2a"> </span>pip<span class="_ _2a"> </span>install<span class="_ _2a"> </span>supersmoother</div><div class="t m0 x4 h4 y99 ff5 fs3 fc0 sc0 ls0 ws0">這個套件的原始碼會被自動地從<span class="_ _3"> </span><span class="ff7">PyPI<span class="_ _3"> </span></span>下載下來,並且安裝在<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>標準路徑下(我</div><div class="t m0 x5 h4 y9a ff5 fs3 fc0 sc0 ls0 ws0">們假定你擁有在你使用的電腦上做這些操作的許可權)<span class="_ _33"></span>。</div><div class="t m0 x4 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">對於<span class="_ _36"> </span><span class="ff7">PyPI<span class="_ _36"> </span></span>和<span class="_ _36"> </span><span class="ffa">pip<span class="_ _60"> </span></span>安裝器的更多資訊,<span class="_ _38"></span>可以檢視這裡的文件:<span class="_ _2"></span><span class="ffa fc2">https://pypi.python.org/<span class="ff5 fc0">。</span></span></div><a class="l" href="https://docs.python.org/3/library/"><div class="d m1" style="border-style:none;position:absolute;left:374.367000px;bottom:531.705000px;width:148.909000px;height:10.000000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="https://docs.python.org/3/library/"><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:515.446000px;width:45.818000px;height:10.000000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="https://pypi.python.org/"><div class="d m1" style="border-style:none;position:absolute;left:488.912000px;bottom:224.272000px;width:34.364000px;height:8.960000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="https://pypi.python.org/"><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:208.013000px;width:103.091000px;height:10.000000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="https://pypi.python.org/"><div class="d m1" style="border-style:none;position:absolute;left:381.603000px;bottom:69.576000px;width:137.454000px;height:10.000000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf57" class="pf w0 h0" data-page-no="57"><div class="pc pc57 w0 h0"></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf58" class="pf w0 h0" data-page-no="58"><div class="pc pc58 w0 h0"><img class="bi x5 y40 w1 h4d" alt="" src="images/bg58.png"/><div class="t m0 x64 h1 y3 ff2 fs0 fc0 sc0 ls0 ws0">第<span class="_ _0"> </span><span class="ff1">15<span class="_ _0"> </span></span>章<span class="_ _32"> </span>字串處理和正則表示式</div><div class="t m0 x4 h4 y204 ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _9"> </span><span class="ff5">語言的一個突出特性是它能輕鬆地處理字串。在本章中我們將會討論<span class="_ _34"> </span></span>Python<span class="_ _3"> </span><span class="ff5">內</span></div><div class="t m0 x5 h4 y205 ff5 fs3 fc0 sc0 ls0 ws0">置的字串處理函數以及<span class="ff6">格式化字串</span>(<span class="ff7">format<span class="_ _3"> </span>string</span>)操作,之後我們將會簡略地介紹一個</div><div class="t m0 x5 h4 y206 ff5 fs3 fc0 sc0 ls0 ws0">非常實用的主題:<span class="ff6">正則表示式</span>(<span class="ff7">regular<span class="_ _9"> </span>expression</span>)<span class="_ _33"></span>。這種字串處理的模式經常在資料科學的</div><div class="t m0 x5 h4 y6a9 ff5 fs3 fc0 sc0 ls0 ws0">工作中出現,並且也是<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>中非常活躍的一個主題。</div><div class="t m0 x4 h4 y6aa ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _3"> </span><span class="ff5">中的字串可以用單引號或者雙引號成對引用來定義(從功能上說這兩種是等價</span></div><div class="t m0 x5 h4 y6ab ff5 fs3 fc0 sc0 ls0 ws0">的)<span class="_ _33"></span>:</div><div class="t m0 x11 hc y6ac ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[1]:<span class="_ _2a"> </span><span class="fc0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">a<span class="_ _2a"> </span>string</span>'</span></span></span></div><div class="t m0 x24 hc y6ad ffa fs3 fc0 sc0 ls0 ws0">y<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="fcc">"a<span class="_ _2a"> </span>string"</span></span></div><div class="t m0 x24 hc y6ae ffa fs3 fc0 sc0 ls0 ws0">x<span class="_ _2a"> </span><span class="fc6">==<span class="_ _2a"> </span></span>y</div><div class="t m0 x11 hc y6af ffa fs3 fcf sc0 ls0 ws0">Out[1]:<span class="_ _2a"> </span><span class="fc0">True</span></div><div class="t m0 x4 h4 y6b0 ff5 fs3 fc0 sc0 ls0 ws0">除此之外,也可以用三重引號來定義跨行字串:</div><div class="t m0 x11 hc y6b1 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[2]:<span class="_ _2a"> </span><span class="fc0">multiline<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="fcc">"""</span></span></span></div><div class="t m0 x24 hc y6b2 ffa fs3 fcc sc0 ls0 ws0">one</div><div class="t m0 x24 hc y6b3 ffa fs3 fcc sc0 ls0 ws0">two</div><div class="t m0 x24 hc y6b4 ffa fs3 fcc sc0 ls0 ws0">three</div><div class="t m0 x24 hc y6b5 ffa fs3 fcc sc0 ls0 ws0">"""</div><div class="t m0 x4 h4 y6b6 ff5 fs3 fc0 sc0 ls0 ws0">有了這些作為基礎,我們來快速瀏覽一下<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>中對字串進行操作的一些工具。</div><div class="t m0 x5 h8 y6b7 ff1 fs1 fc0 sc0 ls0 ws0">15.1<span class="_ _39"> </span>Python<span class="_ _10"> </span><span class="ff2">簡易字串操作</span></div><div class="t m0 x4 h4 y6b8 ff5 fs3 fc0 sc0 ls0 ws0">對於字串的基礎操作,<span class="_ _42"></span><span class="ff7">Python<span class="_ _34"> </span><span class="ff5">內建的字串處理函式極其地方便。<span class="_ _42"></span>如果你有一定的<span class="_ _34"> </span><span class="ff7">C<span class="_ _34"> </span></span>語</span></span></div><div class="t m0 x5 h4 y6b9 ff5 fs3 fc0 sc0 ls0 ws0">言或者其他低階語言的基礎,你一定會覺得<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>提供的這些函式非常簡便。我們已經介紹</div><div class="t m0 x5 h4 y6ba ff5 fs3 fc0 sc0 ls0 ws0">了<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>的字元串類型和一些字串函式,在這一節中我們將會更加深入地介紹字串處理</div><div class="t m0 x5 h4 y6bb ff5 fs3 fc0 sc0 ls0 ws0">的函式。</div><div class="t m0 x5 ha y6bc ff1 fs2 fc0 sc0 ls0 ws0">15.1.1<span class="_ _3c"> </span><span class="ff2">字串格式化:大小寫轉換</span></div><div class="t m0 x4 h4 y6bd ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _10"> </span><span class="ff5">可<span class="_ _55"> </span>以<span class="_ _55"> </span>很<span class="_ _55"></span>容<span class="_ _55"></span>易<span class="_ _55"></span>地<span class="_ _55"></span>調<span class="_ _4a"></span>整<span class="_ _55"> </span>字<span class="_ _55"> </span>符<span class="_ _55"> </span>串<span class="_ _55"> </span>的<span class="_ _55"> </span>大<span class="_ _55"> </span>小<span class="_ _55"> </span>寫。<span class="_ _55"> </span>這<span class="_ _55"> </span>裡<span class="_ _55"> </span>我<span class="_ _55"> </span>們<span class="_ _55"> </span>通<span class="_ _55"> </span>過<span class="_ _55"> </span>幾<span class="_ _55"> </span>個<span class="_ _55"> </span>雜<span class="_ _55"> </span>亂<span class="_ _55"> </span>的<span class="_ _55"></span>例<span class="_ _55"></span>子<span class="_ _55"></span>來<span class="_ _55"></span>看<span class="_ _4a"></span>一</span></div><div class="t m0 x5 h4 y6be ff5 fs3 fc0 sc0 ls0 ws0">下<span class="_ _9"> </span><span class="ffa">upper()</span>、<span class="ffa">lower()</span>、<span class="ffa">capitalize()</span>、<span class="ffa">title()<span class="_ _3"> </span></span>和<span class="_ _9"> </span><span class="ffa">swapcase()<span class="_ _9"> </span></span>這幾個函式:</div><div class="t m0 x11 hc y119 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[3]:<span class="_ _2a"> </span><span class="fc0">fox<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="fcc">"tHe<span class="_ _2a"> </span>qUICk<span class="_ _2a"> </span>bROWn<span class="_ _2a"> </span>fOx."</span></span></span></div><div class="t m0 x2f h7 y20 ff7 fs3 fc0 sc0 ls0 ws0">81</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf59" class="pf w0 h0" data-page-no="59"><div class="pc pc59 w0 h0"><img class="bi x5 y1bd w1 h4e" alt="" src="images/bg59.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">82<span class="_ _8a"> </span><span class="ffc">15.<span class="_ _16"> </span><span class="ff6">字串處理和正則表示式</span></span></div><div class="t m0 x4 h4 y42 ff5 fs3 fc0 sc0 ls0 ws0">要將整個字串轉換為大寫或者小寫,你可以分別使用<span class="_ _9"> </span><span class="ffa">upper()<span class="_ _3"> </span></span>和<span class="_ _9"> </span><span class="ffa">lower()<span class="_ _9"> </span></span>函式:</div><div class="t m0 x11 hc y6bf ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[4]:<span class="_ _2a"> </span><span class="fc0">fox<span class="fc6">.</span>upper()</span></div><div class="t m0 x11 hc y6c0 ffa fs3 fcf sc0 ls0 ws0">Out[4]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">THE<span class="_ _2a"> </span>QUICK<span class="_ _2a"> </span>BROWN<span class="_ _2a"> </span>FOX.</span>'</span></div><div class="t m0 x11 hc y6c1 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[5]:<span class="_ _2a"> </span><span class="fc0">fox<span class="fc6">.</span>lower()</span></div><div class="t m0 x11 hc y6c2 ffa fs3 fcf sc0 ls0 ws0">Out[5]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">the<span class="_ _2a"> </span>quick<span class="_ _2a"> </span>brown<span class="_ _2a"> </span>fox.</span>'</span></div><div class="t m0 x4 h4 y6c3 ff5 fs3 fc0 sc0 ls0 ws0">一個常見格式化的需求是將字串中每一個單詞的首字母大寫,或者每一個段落的首字母</div><div class="t m0 x5 h4 y6c4 ff5 fs3 fc0 sc0 ls0 ws0">大寫,這個時候你可以使用<span class="_ _9"> </span><span class="ffa">title()<span class="_ _3"> </span></span>和<span class="_ _9"> </span><span class="ffa">capitalize()<span class="_ _9"> </span></span>函式:</div><div class="t m0 x11 hc y6c5 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[6]:<span class="_ _2a"> </span><span class="fc0">fox<span class="fc6">.</span>title()</span></div><div class="t m0 x11 hc y6c6 ffa fs3 fcf sc0 ls0 ws0">Out[6]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">The<span class="_ _2a"> </span>Quick<span class="_ _2a"> </span>Brown<span class="_ _2a"> </span>Fox.</span>'</span></div><div class="t m0 x11 hc y6c7 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[7]:<span class="_ _2a"> </span><span class="fc0">fox<span class="fc6">.</span>capitalize()</span></div><div class="t m0 x11 hc y6c8 ffa fs3 fcf sc0 ls0 ws0">Out[7]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">The<span class="_ _2a"> </span>quick<span class="_ _2a"> </span>brown<span class="_ _2a"> </span>fox.</span>'</span></div><div class="t m0 x4 h4 y6c9 ffa fs3 fc0 sc0 ls0 ws0">swapcase()<span class="_ _9"> </span><span class="ff5">函式可以將大小寫顛倒:</span></div><div class="t m0 x11 hc y6ca ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[8]:<span class="_ _2a"> </span><span class="fc0">fox<span class="fc6">.</span>swapcase()</span></div><div class="t m0 x11 hc y6cb ffa fs3 fcf sc0 ls0 ws0">Out[8]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">ThE<span class="_ _2a"> </span>QuicK<span class="_ _2a"> </span>BrowN<span class="_ _2a"> </span>FoX.</span>'</span></div><div class="t m0 x5 ha y6cc ff1 fs2 fc0 sc0 ls0 ws0">15.1.2<span class="_ _3c"> </span><span class="ff2">格式化字串:新增和刪除空格</span></div><div class="t m0 x4 h4 y6cd ff5 fs3 fc0 sc0 ls0 ws0">另一個常見的需求是在字串的頭部或者尾部刪除空格<span class="_ _8"></span>(或者其他字元)<span class="_ _33"></span>。<span class="_ _2"></span>刪除字元的基本</div><div class="t m0 x5 h4 y6ce ff5 fs3 fc0 sc0 ls0 ws0">方法是通過<span class="_ _9"> </span><span class="ffa">strip()<span class="_ _3"> </span></span>函式,這個函式會將頭部和尾部的空白字元刪去:</div><div class="t m0 x11 hc y6cf ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[9]:<span class="_ _2a"> </span><span class="fc0">line<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="_ _47"> </span><span class="ffa">this<span class="_ _2a"> </span>is<span class="_ _2a"> </span>the<span class="_ _2a"> </span>content<span class="_ _47"> </span></span>'</span></span></span></div><div class="t m0 x24 hc y6d0 ffa fs3 fc0 sc0 ls0 ws0">line<span class="fc6">.</span>strip()</div><div class="t m0 x11 hc y6d1 ffa fs3 fcf sc0 ls0 ws0">Out[9]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">this<span class="_ _2a"> </span>is<span class="_ _2a"> </span>the<span class="_ _2a"> </span>content</span>'</span></div><div class="t m0 x4 h4 y6d2 ff5 fs3 fc0 sc0 ls0 ws0">如果只需要刪除右邊或者左邊的空格,可以分別使用<span class="_ _9"> </span><span class="ffa">rstrip()<span class="_ _3"> </span></span>或<span class="_ _9"> </span><span class="ffa">lstrip()</span>:</div><div class="t m0 x11 hc y176 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[10]:<span class="_ _2a"> </span><span class="fc0">line<span class="fc6">.</span>rstrip()</span></div><div class="t m0 x11 hc y99 ffa fs3 fcf sc0 ls0 ws0">Out[10]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="_ _47"> </span><span class="ffa">this<span class="_ _2a"> </span>is<span class="_ _2a"> </span>the<span class="_ _2a"> </span>content</span>'</span></div><div class="t m0 x11 hc y40 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[11]:<span class="_ _2a"> </span><span class="fc0">line<span class="fc6">.</span>lstrip()</span></div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf5a" class="pf w0 h0" data-page-no="5a"><div class="pc pc5a w0 h0"><img class="bi x5 y40 w1 h33" alt="" src="images/bg5a.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">15.1.<span class="_ _16"> </span>PYTHON<span class="_ _9"> </span><span class="ff6">簡易字串操作<span class="_ _8b"> </span><span class="ff7">83</span></span></div><div class="t m0 x11 hc y1d7 ffa fs3 fcf sc0 ls0 ws0">Out[11]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">this<span class="_ _2a"> </span>is<span class="_ _2a"> </span>the<span class="_ _2a"> </span>content<span class="_ _47"> </span></span>'</span></div><div class="t m0 x4 h4 y6d3 ff5 fs3 fc0 sc0 ls0 ws0">如果要刪除的不是空格而是其他字元,向<span class="_ _9"> </span><span class="ffa">strip()<span class="_ _3"> </span></span>函式傳遞你需要刪除的字元作為參數:</div><div class="t m0 x11 hc y6d4 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[12]:<span class="_ _2a"> </span><span class="fc0">num<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="fcc">"000000000000435"</span></span></span></div><div class="t m0 x31 hc y6d5 ffa fs3 fc0 sc0 ls0 ws0">num<span class="fc6">.</span>strip(<span class="ff12 fcc">'<span class="ffa">0</span>'</span>)</div><div class="t m0 x11 hc y6d6 ffa fs3 fcf sc0 ls0 ws0">Out[12]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">435</span>'</span></div><div class="t m0 x4 h4 y6d7 ff5 fs3 fc0 sc0 ls0 ws0">刪除空格的反向操作可以通過<span class="_ _9"> </span><span class="ffa">center()</span>、<span class="ffa">ljust()<span class="_ _3"> </span></span>和<span class="_ _9"> </span><span class="ffa">rjust()<span class="_ _9"> </span></span>三個函式實現。</div><div class="t m0 x4 h4 y6d8 ff5 fs3 fc0 sc0 ls0 ws0">舉例來說,我們可以使用<span class="_ _9"> </span><span class="ffa">center()<span class="_ _3"> </span></span>函式來以一定數量的空格中心對齊一個給定的字串:</div><div class="t m0 x11 hc y6d9 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[13]:<span class="_ _2a"> </span><span class="fc0">line<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="fcc">"this<span class="_ _2a"> </span>is<span class="_ _2a"> </span>the<span class="_ _2a"> </span>content"</span></span></span></div><div class="t m0 x31 hc y6da ffa fs3 fc0 sc0 ls0 ws0">line<span class="fc6">.</span>center(<span class="fc6">30</span>)</div><div class="t m0 x11 hc y6db ffa fs3 fcf sc0 ls0 ws0">Out[13]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="_ _4f"> </span><span class="ffa">this<span class="_ _2a"> </span>is<span class="_ _2a"> </span>the<span class="_ _2a"> </span>content<span class="_ _48"> </span></span>'</span></div><div class="t m0 x4 h4 y6dc ff5 fs3 fc0 sc0 ls0 ws0">類似地,<span class="ffa">ljust()<span class="_ _9"> </span></span>和<span class="_ _3"> </span><span class="ffa">rjust()<span class="_ _9"> </span></span>將會以一定長度的空格左對齊或者右對齊字串:</div><div class="t m0 x11 hc y6dd ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[14]:<span class="_ _2a"> </span><span class="fc0">line<span class="fc6">.</span>ljust(<span class="fc6">30</span>)</span></div><div class="t m0 x11 hc y6de ffa fs3 fcf sc0 ls0 ws0">Out[14]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">this<span class="_ _2a"> </span>is<span class="_ _2a"> </span>the<span class="_ _2a"> </span>content<span class="_ _56"> </span></span>'</span></div><div class="t m0 x11 hc y6df ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[15]:<span class="_ _2a"> </span><span class="fc0">line<span class="fc6">.</span>rjust(<span class="fc6">30</span>)</span></div><div class="t m0 x11 hc y6e0 ffa fs3 fcf sc0 ls0 ws0">Out[15]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="_ _56"> </span><span class="ffa">this<span class="_ _2a"> </span>is<span class="_ _2a"> </span>the<span class="_ _2a"> </span>content</span>'</span></div><div class="t m0 x4 h4 y6e1 ff5 fs3 fc0 sc0 ls0 ws0">除此以外,所有這些函式都支援以任意的字元填充空白。比如:</div><div class="t m0 x11 hc y6e2 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[16]:<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">435</span>'</span><span class="fc6">.<span class="fc0">rjust(</span>10<span class="fc0">,<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">0</span>'</span>)</span></span></div><div class="t m0 x11 hc y6e3 ffa fs3 fcf sc0 ls0 ws0">Out[16]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">0000000435</span>'</span></div><div class="t m0 x4 h4 y6e4 ff5 fs3 fc0 sc0 ls0 ws0">因為填充<span class="_ _3"> </span><span class="ff15">0<span class="_ _3"> </span></span>是一種常見需求,<span class="ff7">Python<span class="_ _3"> </span></span>也提供了<span class="_ _3"> </span><span class="ffa">zfill()<span class="_ _3"> </span></span>函式。這個特殊的函式會在字</div><div class="t m0 x5 h4 y6e5 ff5 fs3 fc0 sc0 ls0 ws0">串左邊填充一個全為<span class="_ _9"> </span><span class="ff7">“0”<span class="_ _3"> </span></span>的字串:</div><div class="t m0 x11 hc y117 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[17]:<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">435</span>'</span><span class="fc6">.<span class="fc0">zfill(</span>10<span class="fc0">)</span></span></div><div class="t m0 x11 hc y119 ffa fs3 fcf sc0 ls0 ws0">Out[17]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">0000000435</span>'</span></div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf5b" class="pf w0 h0" data-page-no="5b"><div class="pc pc5b w0 h0"><img class="bi x5 y1d5 w1 h4f" alt="" src="images/bg5b.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">84<span class="_ _8a"> </span><span class="ffc">15.<span class="_ _16"> </span><span class="ff6">字串處理和正則表示式</span></span></div><div class="t m0 x5 ha y2ca ff1 fs2 fc0 sc0 ls0 ws0">15.1.3<span class="_ _3c"> </span><span class="ff2">查詢和替換子字串</span></div><div class="t m0 x4 h4 y2cb ff5 fs3 fc0 sc0 ls0 ws0">如果你需要查詢一個特定字元在一個字串出現的次數,<span class="_ _3e"></span><span class="ff7">Python<span class="_ _34"> </span><span class="ff5">內建的<span class="_ _9"> </span><span class="ffa">find()</span></span>/<span class="ffa">rfind()<span class="ff5">、</span></span></span></div><div class="t m0 x5 h4 y2cc ffa fs3 fc0 sc0 ls0 ws0">index()<span class="ff7">/</span>rindex()<span class="_ _9"> </span><span class="ff5">和<span class="_ _3"> </span></span>replace()<span class="_ _9"> </span><span class="ff5">函式是最好的選擇。</span></div><div class="t m0 x4 h4 y6e6 ffa fs3 fc0 sc0 ls0 ws0">find()<span class="_ _3"> </span><span class="ff5">和<span class="_ _3"> </span></span>index()<span class="_ _3"> </span><span class="ff5">函式相似,它們都在一個字串中搜尋一個字元或者一個子字串首次出</span></div><div class="t m0 x5 h4 y6e7 ff5 fs3 fc0 sc0 ls0 ws0">現的情況,並且回傳子串的索引:</div><div class="t m0 x11 hc y6e8 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[18]:<span class="_ _2a"> </span><span class="fc0">line<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">the<span class="_ _2a"> </span>quick<span class="_ _2a"> </span>brown<span class="_ _2a"> </span>fox<span class="_ _2a"> </span>jumped<span class="_ _2a"> </span>over<span class="_ _2a"> </span>a<span class="_ _2a"> </span>lazy<span class="_ _2a"> </span>dog</span>'</span></span></span></div><div class="t m0 x31 hc y6e9 ffa fs3 fc0 sc0 ls0 ws0">line<span class="fc6">.</span>find(<span class="ff12 fcc">'<span class="ffa">fox</span>'</span>)</div><div class="t m0 x11 hc y6ea ffa fs3 fcf sc0 ls0 ws0">Out[18]:<span class="_ _2a"> </span><span class="fc0">16</span></div><div class="t m0 x11 hc y6eb ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[19]:<span class="_ _2a"> </span><span class="fc0">line<span class="fc6">.</span>index(<span class="ff12 fcc">'<span class="ffa">fox</span>'</span>)</span></div><div class="t m0 x11 hc y6ec ffa fs3 fcf sc0 ls0 ws0">Out[19]:<span class="_ _2a"> </span><span class="fc0">16</span></div><div class="t m0 x4 h4 y6ed ffa fs3 fc0 sc0 ls0 ws0">find()<span class="_ _72"> </span><span class="ff5">和<span class="_ _72"> </span></span>index()<span class="_ _72"> </span><span class="ff5">唯<span class="_ _4a"></span>一的區<span class="_ _4a"></span>別是<span class="_ _4a"></span>當要搜<span class="_ _4a"></span>索的<span class="_ _4a"></span>字串<span class="_ _4a"></span>不存<span class="_ _4a"></span>在時<span class="_ _4a"></span>這兩個函<span class="_ _4a"></span>數的<span class="_ _4a"></span>行為不<span class="_ _4a"></span>同:</span></div><div class="t m0 x5 h4 y6ee ffa fs3 fc0 sc0 ls0 ws0">find()<span class="_ _9"> </span><span class="ff5">回傳<span class="_ _3"> </span></span>-1<span class="ff5">,而<span class="_ _9"> </span></span>index()<span class="_ _9"> </span><span class="ff5">將會丟擲一個<span class="_ _3"> </span></span>ValueError<span class="_ _9"> </span><span class="ff5">異常:</span></div><div class="t m0 x11 hc y6ef ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[20]:<span class="_ _2a"> </span><span class="fc0">line<span class="fc6">.</span>find(<span class="ff12 fcc">'<span class="ffa">bear</span>'</span>)</span></div><div class="t m0 x11 hc y6f0 ffa fs3 fcf sc0 ls0 ws0">Out[20]:<span class="_ _2a"> </span><span class="fc0">-1</span></div><div class="t m0 x11 hc y6f1 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[21]:<span class="_ _2a"> </span><span class="fc0">line<span class="fc6">.</span>index(<span class="ff12 fcc">'<span class="ffa">bear</span>'</span>)</span></div><div class="t m0 x24 hc y6f2 ffa fs3 fc0 sc0 ls0 ws0">----------------------------------------------------------------------</div><div class="t m0 x24 hc y6f3 ffa fs3 fc0 sc0 ls0 ws0">ValueError<span class="_ _7e"> </span>Traceback<span class="_ _2a"> </span>(most<span class="_ _2a"> </span>recent<span class="_ _2a"> </span>call<span class="_ _2a"> </span>last)</div><div class="t m0 x24 hc y6f4 ffa fs3 fc0 sc0 ls0 ws0"><ipython-input-21-4cbe6ee9b0eb><span class="_ _2a"> </span>in<span class="_ _2a"> </span><module>()</div><div class="t m0 x29 hc y6f5 ffa fs3 fc0 sc0 ls0 ws0">----><span class="_ _2a"> </span>1<span class="_ _2a"> </span>line.index(<span class="ff12">'</span>bear<span class="ff12">'</span>)</div><div class="t m0 x24 hc y6f6 ffa fs3 fc0 sc0 ls0 ws0">ValueError:<span class="_ _2a"> </span>substring<span class="_ _2a"> </span>not<span class="_ _2a"> </span>found</div><div class="t m0 x4 h4 y6f7 ff5 fs3 fc0 sc0 ls0 ws0">與之相關的<span class="_ _3"> </span><span class="ffa">rfind()<span class="_ _3"> </span></span>和<span class="_ _9"> </span><span class="ffa">rindex()<span class="_ _3"> </span></span>函式與上面兩個函式類似,它們會從字元串尾部開始搜</div><div class="t m0 x5 h4 y6f8 ff5 fs3 fc0 sc0 ls0 ws0">索第一次出現的子串,而不是從頭部:</div><div class="t m0 x11 hc y6f9 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[22]:<span class="_ _2a"> </span><span class="fc0">line<span class="fc6">.</span>rfind(<span class="ff12 fcc">'<span class="ffa">a</span>'</span>)</span></div><div class="t m0 x11 hc y6fa ffa fs3 fcf sc0 ls0 ws0">Out[22]:<span class="_ _2a"> </span><span class="fc0">35</span></div><div class="t m0 x4 h4 y9a ff5 fs3 fc0 sc0 ls0 ws0">對<span class="_ _34"> </span>字<span class="_ _34"> </span>符<span class="_ _9"> </span>串<span class="_ _34"> </span>開<span class="_ _34"> </span>頭<span class="_ _34"> </span>或<span class="_ _9"> </span>結<span class="_ _34"> </span>尾<span class="_ _34"> </span>的<span class="_ _34"> </span>子<span class="_ _9"> </span>串<span class="_ _34"> </span>進<span class="_ _34"> </span>行<span class="_ _34"> </span>檢<span class="_ _9"> </span>查<span class="_ _34"> </span>的<span class="_ _34"> </span>特<span class="_ _34"> </span>殊<span class="_ _9"> </span>情<span class="_ _34"> </span>況,<span class="ff7">Python<span class="_ _2c"> </span></span>提<span class="_ _34"> </span>供<span class="_ _9"> </span>了<span class="_ _34"> </span>兩<span class="_ _34"> </span>個<span class="_ _34"> </span>函</div><div class="t m0 x5 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">數<span class="_ _9"> </span><span class="ffa">startswith()<span class="_ _3"> </span></span>和<span class="_ _9"> </span><span class="ffa">endswith()</span>:</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf5c" class="pf w0 h0" data-page-no="5c"><div class="pc pc5c w0 h0"><img class="bi x5 y1bd w1 h50" alt="" src="images/bg5c.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">15.1.<span class="_ _16"> </span>PYTHON<span class="_ _9"> </span><span class="ff6">簡易字串操作<span class="_ _8b"> </span><span class="ff7">85</span></span></div><div class="t m0 x11 hc yfa ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[23]:<span class="_ _2a"> </span><span class="fc0">line<span class="fc6">.</span>endswith(<span class="ff12 fcc">'<span class="ffa">dog</span>'</span>)</span></div><div class="t m0 x11 hc yfc ffa fs3 fcf sc0 ls0 ws0">Out[23]:<span class="_ _2a"> </span><span class="fc0">True</span></div><div class="t m0 x11 hc yfd ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[24]:<span class="_ _2a"> </span><span class="fc0">line<span class="fc6">.</span>startswith(<span class="ff12 fcc">'<span class="ffa">fox</span>'</span>)</span></div><div class="t m0 x11 hc yff ffa fs3 fcf sc0 ls0 ws0">Out[24]:<span class="_ _2a"> </span><span class="fc0">False</span></div><div class="t m0 x4 h4 y6fb ff5 fs3 fc0 sc0 ls0 ws0">進一步地,你可以使用<span class="_ _3"> </span><span class="ffa">replace()<span class="_ _3"> </span></span>函數將一個給定的子串替換為另外一個。在這裡我們</div><div class="t m0 x5 h4 y6fc ff5 fs3 fc0 sc0 ls0 ws0">將<span class="_ _9"> </span><span class="ff12">'<span class="ffa">brown</span>'<span class="_ _3"> </span></span>替換為<span class="_ _9"> </span><span class="ff12">'<span class="ffa">red</span>'</span>:</div><div class="t m0 x11 hc y6fd ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[25]:<span class="_ _2a"> </span><span class="fc0">line<span class="fc6">.</span>replace(<span class="ff12 fcc">'<span class="ffa">brown</span>'</span>,<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">red</span>'</span>)</span></div><div class="t m0 x11 hc y6fe ffa fs3 fcf sc0 ls0 ws0">Out[25]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">the<span class="_ _2a"> </span>quick<span class="_ _2a"> </span>red<span class="_ _2a"> </span>fox<span class="_ _2a"> </span>jumped<span class="_ _2a"> </span>over<span class="_ _2a"> </span>a<span class="_ _2a"> </span>lazy<span class="_ _2a"> </span>dog</span>'</span></div><div class="t m0 x4 h4 y6ff ffa fs3 fc0 sc0 ls0 ws0">replace()<span class="_ _9"> </span><span class="ff5">函式回傳一個新的字串,並且會替換輸入中的所有出現的字串:</span></div><div class="t m0 x11 hc y700 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[26]:<span class="_ _2a"> </span><span class="fc0">line<span class="fc6">.</span>replace(<span class="ff12 fcc">'<span class="ffa">o</span>'</span>,<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">--</span>'</span>)</span></div><div class="t m0 x11 hc y701 ffa fs3 fcf sc0 ls0 ws0">Out[26]:<span class="_ _2a"> </span><span class="ff12 fc0">'</span></div><div class="t m0 x64 hc y702 ffa fs3 fc0 sc0 ls0 ws0">the<span class="_ _2a"> </span>quick<span class="_ _2a"> </span>br--wn<span class="_ _2a"> </span>f--x<span class="_ _2a"> </span>jumped<span class="_ _2a"> </span>--ver<span class="_ _2a"> </span>a<span class="_ _2a"> </span>lazy<span class="_ _2a"> </span>d--g</div><div class="t m0 x42 h34 y701 ff12 fs3 fc0 sc0 ls0 ws0">'</div><div class="t m0 x4 h4 y703 ff5 fs3 fc0 sc0 ls0 ws0">對於<span class="_ _3"> </span><span class="ffa">replace()<span class="_ _3"> </span></span>函數的更為靈活的用法,請參閱第<span class="_ _3"> </span><span class="ff7 fc1">15.3<span class="_ _3"> </span></span>節關於正則表達式的討論:<span class="fc1">使用</span></div><div class="t m0 x5 h4 y704 ff5 fs3 fc1 sc0 ls0 ws0">正則表示式靈活地匹配字串模式<span class="fc0">。</span></div><div class="t m0 x5 ha y705 ff1 fs2 fc0 sc0 ls0 ws0">15.1.4<span class="_ _3c"> </span><span class="ff2">拆分和分割字串</span></div><div class="t m0 x4 h4 y706 ff5 fs3 fc0 sc0 ls0 ws0">如<span class="_ _10"> </span>果<span class="_ _1"> </span>你<span class="_ _10"> </span>需<span class="_ _10"> </span>要<span class="_ _1"> </span>尋<span class="_ _10"> </span>找<span class="_ _1"> </span>一<span class="_ _10"> </span>個<span class="_ _10"> </span>子<span class="_ _1"> </span>串,<span class="_ _10"> </span><span class="ff2">然<span class="_ _10"> </span>後<span class="_ _1"> </span></span>在<span class="_ _10"> </span>子<span class="_ _10"> </span>串<span class="_ _1"> </span>的<span class="_ _10"> </span>位<span class="_ _1"> </span>置<span class="_ _10"> </span>進<span class="_ _10"> </span>行<span class="_ _1"> </span>字<span class="_ _10"> </span>符<span class="_ _10"> </span>串<span class="_ _1"> </span>分<span class="_ _10"> </span>割,</div><div class="t m0 x5 h4 y707 ffa fs3 fc0 sc0 ls0 ws0">partition()<span class="_ _72"> </span><span class="ff5">和<span class="ff7">/</span>或<span class="_ _1"> </span></span>split()<span class="_ _1"> </span><span class="ff5">函<span class="_ _4a"></span>數<span class="_ _4a"></span>是<span class="_ _4a"></span>你需<span class="_ _4a"></span>要<span class="_ _4a"></span>尋<span class="_ _4a"></span>找<span class="_ _4a"></span>的解<span class="_ _4a"></span>決<span class="_ _4a"></span>方<span class="_ _4a"></span>法。<span class="_ _4a"></span>這兩<span class="_ _4a"></span>個函<span class="_ _4a"></span>數<span class="_ _4a"></span>都會<span class="_ _4a"></span>返<span class="_ _4a"></span>回<span class="_ _4a"></span>一<span class="_ _4a"></span>個子</span></div><div class="t m0 x5 h4 y708 ff5 fs3 fc0 sc0 ls0 ws0">串序列。</div><div class="t m0 x4 h4 y709 ffa fs3 fc0 sc0 ls0 ws0">partition()<span class="_ _3"> </span><span class="ff5">函式回傳一個三個元素的Tuple:在待尋找的子串第一次出現的位置(拆分位</span></div><div class="t m0 x5 h4 y70a ff5 fs3 fc0 sc0 ls0 ws0">置)之前的子字串、拆分位置本身以及後面的子字串:</div><div class="t m0 x11 hc y70b ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[27]:<span class="_ _2a"> </span><span class="fc0">line<span class="fc6">.</span>partition(<span class="ff12 fcc">'<span class="ffa">fox</span>'</span>)</span></div><div class="t m0 x11 hc y70c ffa fs3 fcf sc0 ls0 ws0">Out[27]:<span class="_ _2a"> </span><span class="fc0">(<span class="ff12">'</span>the<span class="_ _2a"> </span>quick<span class="_ _2a"> </span>brown<span class="_ _2a"> </span><span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>fox<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'<span class="_ _2a"> </span></span>jumped<span class="_ _2a"> </span>over<span class="_ _2a"> </span>a<span class="_ _2a"> </span>lazy<span class="_ _2a"> </span>dog<span class="ff12">'</span>)</span></div><div class="t m0 x4 h4 y70d ffa fs3 fc0 sc0 ls0 ws0">rpartition()<span class="_ _9"> </span><span class="ff5">函式與之類似,但是是從右往左搜尋字串。</span></div><div class="t m0 x4 h4 y70e ffa fs3 fc0 sc0 ls0 ws0">split()<span class="_ _9"> </span><span class="ff5">函式可能更加實用。這個函式尋找<span class="ff2">所有</span>分割位置的實體並且回傳它們之間的子串。</span></div><div class="t m0 x5 h4 y70f ff5 fs3 fc0 sc0 ls0 ws0">預設呼叫這個函數時它以任意空白字符作為分割的依據,返回一個包含字串中的所有單詞</div><div class="t m0 x5 h4 y710 ff5 fs3 fc0 sc0 ls0 ws0">的列表:</div><div class="t m0 x11 hc y9a ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[28]:<span class="_ _2a"> </span><span class="fc0">line<span class="fc6">.</span>split()</span></div><a class="l" href="#pf5f" data-dest-detail='[95,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:345.288000px;bottom:398.870000px;width:19.394000px;height:6.870000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf5f" data-dest-detail='[95,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:501.262000px;bottom:395.759000px;width:22.014000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf5f" data-dest-detail='[95,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:379.500000px;width:163.636000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf5d" class="pf w0 h0" data-page-no="5d"><div class="pc pc5d w0 h0"><img class="bi x5 y523 w1 h51" alt="" src="images/bg5d.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">86<span class="_ _8a"> </span><span class="ffc">15.<span class="_ _16"> </span><span class="ff6">字串處理和正則表示式</span></span></div><div class="t m0 x11 hc y1d6 ffa fs3 fcf sc0 ls0 ws0">Out[28]:<span class="_ _2a"> </span><span class="fc0">[<span class="ff12">'</span>the<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>quick<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>brown<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>fox<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>jumped<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>over<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>a<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>lazy<span class="ff12">'</span>,</span></div><div class="t m0 x64 hc y1d7 ff12 fs3 fc0 sc0 ls0 ws0">'<span class="ffa">dog</span>'<span class="ffa">]</span></div><div class="t m0 x4 h4 y711 ff5 fs3 fc0 sc0 ls0 ws0">與之相關的一個函式是<span class="_ _3"> </span><span class="ffa">splitlines()</span>,它對換行符號進行分割。讓我們對<span class="_ _9"> </span><span class="ff7">17<span class="_ _3"> </span></span>世紀著名的俳</div><div class="t m0 x5 h4 y712 ff5 fs3 fc0 sc0 ls0 ws0">句詩人松尾芭蕉(まつお<span class="_ _9"> </span>ばしょう,</div><div class="t m0 x6c h7 y713 ff7 fs3 fc0 sc0 ls0 ws0">Matsuo<span class="_ _9"> </span>Bash¯<span class="_ _33"></span>o</div><div class="t m0 x2e h4 y712 ff5 fs3 fc0 sc0 ls0 ws0">)的一首俳句進行分割:</div><div class="t m0 x11 hc y714 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[29]:<span class="_ _2a"> </span><span class="fc0">haiku<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="fcc">"""matsushima-ya</span></span></span></div><div class="t m0 x31 hc y715 ffa fs3 fcc sc0 ls0 ws0">aah<span class="_ _2a"> </span>matsushima-ya</div><div class="t m0 x31 hc y716 ffa fs3 fcc sc0 ls0 ws0">matsushima-ya"""</div><div class="t m0 x31 hc y717 ffa fs3 fc0 sc0 ls0 ws0">haiku<span class="fc6">.</span>splitlines()</div><div class="t m0 x11 hc y718 ffa fs3 fcf sc0 ls0 ws0">Out[29]:<span class="_ _2a"> </span><span class="fc0">[<span class="ff12">'</span>matsushima-ya<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>aah<span class="_ _2a"> </span>matsushima-ya<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>matsushima-ya<span class="ff12">'</span>]</span></div><div class="t m0 x4 h4 y719 ff5 fs3 fc0 sc0 ls0 ws0">注意,如果你希望撤銷<span class="_ _3"> </span><span class="ffa">split()<span class="_ _9"> </span></span>的結果,你可以使用<span class="_ _3"> </span><span class="ffa">join()<span class="_ _3"> </span></span>函式,這個函式將回傳一個</div><div class="t m0 x5 h4 y71a ff5 fs3 fc0 sc0 ls0 ws0">由一個分割位置和一個可迭代物件組成的字串:</div><div class="t m0 x11 hc y71b ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[30]:<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">--</span>'</span><span class="fc6">.<span class="fc0">join([<span class="ff12 fcc">'<span class="ffa">1</span>'</span>,<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">2</span>'</span>,<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">3</span>'</span>])</span></span></div><div class="t m0 x11 hc y71c ffa fs3 fcf sc0 ls0 ws0">Out[30]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">1--2--3</span>'</span></div><div class="t m0 x4 h4 y71d ff5 fs3 fc0 sc0 ls0 ws0">一個常見的模式是使用換行符號<span class="_ _9"> </span><span class="ffa">"\n"<span class="_ _3"> </span></span>來串接之前分割過的字串,這樣可以恢復原始輸入:</div><div class="t m0 x11 hc y71e ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[31]:<span class="_ _2a"> </span><span class="fc4">print<span class="fc0">(<span class="fcc">"<span class="ff11 fc12">\n</span>"<span class="fc6">.</span></span>join([<span class="ff12 fcc">'<span class="ffa">matsushima-ya</span>'</span>,<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">aah<span class="_ _2a"> </span>matsushima-ya</span>'</span>,</span></span></div><div class="t m0 x6d hc y71f ff12 fs3 fcc sc0 ls0 ws0">'<span class="ffa">matsushima-ya</span>'<span class="ffa fc0">]))</span></div><div class="t m0 x11 hc y720 ffa fs3 fc0 sc0 ls0 ws0">matsushima-ya</div><div class="t m0 x11 hc y721 ffa fs3 fc0 sc0 ls0 ws0">aah<span class="_ _2a"> </span>matsushima-ya</div><div class="t m0 x11 hc y722 ffa fs3 fc0 sc0 ls0 ws0">matsushima-ya</div><div class="t m0 x5 h8 y723 ff1 fs1 fc0 sc0 ls0 ws0">15.2<span class="_ _39"> </span><span class="ff2">格式化字串</span></div><div class="t m0 x4 h4 y724 ff5 fs3 fc0 sc0 ls0 ws0">在對上述函式的討論中,<span class="_ _38"></span>我們已經學會了如何從字串中提取值,<span class="_ _37"></span>並對字串進行操作以轉</div><div class="t m0 x5 h4 y725 ff5 fs3 fc0 sc0 ls0 ws0">換為需要的格式。<span class="_ _3a"></span>另外一個字串函式的使用是對字串其他型別的值的<span class="ff6">表示<span class="_ _2"></span><span class="ff5">(<span class="ff7">represen<span class="_ _8"></span>tation<span class="ff5">)</span></span></span></span></div><div class="t m0 x5 h4 y726 ff5 fs3 fc0 sc0 ls0 ws0">進行操作。當然,字串表示總是可以通過</div><div class="t m0 x4d hc y727 ffa fs3 fc0 sc0 ls0 ws0">str()</div><div class="t m0 x48 h4 y726 ff5 fs3 fc0 sc0 ls0 ws0">函式產生,比如:</div><div class="t m0 x11 hc y728 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[32]:<span class="_ _2a"> </span><span class="fc0">pi<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span>3.14159</span></span></div><div class="t m0 x31 hc y729 ffa fs3 fc4 sc0 ls0 ws0">str<span class="fc0">(pi)</span></div><div class="t m0 x11 hc y72a ffa fs3 fcf sc0 ls0 ws0">Out[32]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">3.14159</span>'</span></div><div class="t m0 x4 h4 y9a ff5 fs3 fc0 sc0 ls0 ws0">對於更加複雜的格式,你可以使用第<span class="_ _13"> </span><span class="ff7 fc1">5<span class="_ _a"> </span></span>章<span class="fc1">基礎<span class="_ _a"> </span><span class="ff7">Python<span class="_ _a"> </span></span>語法:運算子</span>中介紹過的字串</div><div class="t m0 x5 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">的算術運算子:</div><a class="l" href="#pf1a" data-dest-detail='[26,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:279.047000px;bottom:88.259000px;width:5.454000px;height:6.870000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf1a" data-dest-detail='[26,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:299.686000px;bottom:85.148000px;width:133.113000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf5e" class="pf w0 h0" data-page-no="5e"><div class="pc pc5e w0 h0"><img class="bi x5 y11a w1 h52" alt="" src="images/bg5e.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">15.2.<span class="_ _16"> </span><span class="ff6">格式化字串<span class="_ _82"> </span><span class="ff7">87</span></span></div><div class="t m0 x11 hc yfa ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[33]:<span class="_ _2a"> </span><span class="fcc">"The<span class="_ _2a"> </span>value<span class="_ _2a"> </span>of<span class="_ _2a"> </span>pi<span class="_ _2a"> </span>is<span class="_ _2a"> </span>"<span class="_ _2a"> </span><span class="fc6">+<span class="_ _2a"> </span><span class="fc4">str<span class="fc0">(pi)</span></span></span></span></div><div class="t m0 x11 hc yfc ffa fs3 fcf sc0 ls0 ws0">Out[33]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">The<span class="_ _2a"> </span>value<span class="_ _2a"> </span>of<span class="_ _2a"> </span>pi<span class="_ _2a"> </span>is<span class="_ _2a"> </span>3.14159</span>'</span></div><div class="t m0 x4 h4 y72b ff5 fs3 fc0 sc0 ls0 ws0">一個更加靈活的方法是使用格式化字串。這是由花括號表示的特殊標記組成的字元串,</div><div class="t m0 x5 h4 y72c ff5 fs3 fc0 sc0 ls0 ws0">代表將要插入字串格式化後的值。這裡有一個簡單的例子:</div><div class="t m0 x11 hc y72d ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[34]:<span class="_ _2a"> </span><span class="fcc">"The<span class="_ _2a"> </span>value<span class="_ _2a"> </span>of<span class="_ _2a"> </span>pi<span class="_ _2a"> </span>is<span class="_ _2a"> </span><span class="ff11 fc10">{}</span>"<span class="fc6">.<span class="fc0">format(pi)</span></span></span></div><div class="t m0 x11 hc y72e ffa fs3 fcf sc0 ls0 ws0">Out[34]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">The<span class="_ _2a"> </span>value<span class="_ _2a"> </span>of<span class="_ _2a"> </span>pi<span class="_ _2a"> </span>is<span class="_ _2a"> </span>3.14159</span>'</span></div><div class="t m0 x4 h4 y72f ff5 fs3 fc0 sc0 ls0 ws0">在<span class="_ _9"> </span><span class="ffa">{}<span class="_ _3"> </span></span>標記內部你也可以包括希望在那裡出現的確切的資訊</div><div class="t m0 x6e hb y730 ffe fs4 fc1 sc0 ls0 ws0">1</div><div class="t m0 x6f h4 y72f ff5 fs3 fc0 sc0 ls0 ws0">。如果你包括了一個數字,它</div><div class="t m0 x5 h4 y731 ff5 fs3 fc0 sc0 ls0 ws0">指向的是帶插入的參數的索引:</div><div class="t m0 x11 hc y732 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[35]:<span class="_ _2a"> </span><span class="ff14 fcc">"""First<span class="_ _2a"> </span>letter:<span class="_ _2a"> </span>{0}.<span class="_ _2a"> </span>Last<span class="_ _2a"> </span>letter:<span class="_ _2a"> </span>{1}."""<span class="_ _4b"> </span></span><span class="fc6">.<span class="fc0">format(<span class="ff12 fcc">'<span class="ffa">A</span>'</span>,<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">Z</span>'</span>)</span></span></div><div class="t m0 x11 hc y733 ffa fs3 fcf sc0 ls0 ws0">Out[35]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">First<span class="_ _2a"> </span>letter:<span class="_ _2a"> </span>A.<span class="_ _2a"> </span>Last<span class="_ _2a"> </span>letter:<span class="_ _2a"> </span>Z.</span>'</span></div><div class="t m0 x4 h4 y734 ff5 fs3 fc0 sc0 ls0 ws0">如果你在大括號中包括了一個字串,它會指向任何關鍵字參數的鍵:</div><div class="t m0 x11 hc y735 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[36]:<span class="_ _2a"> </span><span class="ff14 fcc">"""First<span class="_ _2a"> </span>letter:<span class="_ _2a"> </span>{first}.<span class="_ _2a"> </span>Last<span class="_ _2a"> </span>letter:<span class="_ _2a"> </span>{last}."""<span class="_ _4b"> </span></span><span class="fc6">.<span class="fc0">format(</span></span></div><div class="t m0 x31 hc y736 ffa fs3 fc0 sc0 ls0 ws0">last<span class="fc6">=<span class="ff12 fcc">'<span class="ffa">Z</span>'</span></span>,<span class="_ _2a"> </span>first<span class="fc6">=<span class="ff12 fcc">'<span class="ffa">A</span>'</span></span>)</div><div class="t m0 x11 hc y737 ffa fs3 fcf sc0 ls0 ws0">Out[36]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">First<span class="_ _2a"> </span>letter:<span class="_ _2a"> </span>A.<span class="_ _2a"> </span>Last<span class="_ _2a"> </span>letter:<span class="_ _2a"> </span>Z.</span>'</span></div><div class="t m0 x4 h4 y738 ff5 fs3 fc0 sc0 ls0 ws0">最後,對於數字的輸入,你可以使用格式化程式碼來控制數字轉換為字串的格式。比如要</div><div class="t m0 x5 h4 y739 ff5 fs3 fc0 sc0 ls0 ws0">列印一個小數點後保留<span class="_ _9"> </span><span class="ff15">3<span class="_ _3"> </span></span>位的浮點數,你可以使用如下格式化字串:</div><div class="t m0 x11 hc y73a ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[37]:<span class="_ _2a"> </span><span class="fcc">"pi<span class="_ _2a"> </span>=<span class="_ _2a"> </span><span class="ff11 fc10">{0:.3f}</span>"<span class="fc6">.<span class="fc0">format(pi)</span></span></span></div><div class="t m0 x11 hc y73b ffa fs3 fcf sc0 ls0 ws0">Out[37]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">pi<span class="_ _2a"> </span>=<span class="_ _2a"> </span>3.142</span>'</span></div><div class="t m0 x4 h4 y73c ff5 fs3 fc0 sc0 ls0 ws0">像之前討論的那樣,這裡的<span class="_ _3"> </span><span class="ffa">0<span class="_ _3"> </span></span>指的是將要插入的參數的索引,冒號(<span class="ffa">:</span>)標記了後面將會</div><div class="t m0 x5 h4 y73d ff5 fs3 fc0 sc0 ls0 ws0">跟著格式化程式碼,<span class="ffa">.3f<span class="_ _9"> </span></span>編碼了需要的精度資訊:小數點後保留<span class="_ _3"> </span><span class="ff15">3<span class="_ _9"> </span></span>位小數的浮點數。</div><div class="t m0 x4 h4 y73e ff5 fs3 fc0 sc0 ls0 ws0">這種格式<span class="_ _4a"></span>規範非常靈活。<span class="_ _4a"></span>我們在這裡<span class="_ _4a"></span>舉的例子還不<span class="_ _4a"></span>足以介紹全<span class="_ _4a"></span>部的字串<span class="_ _4a"></span>格式化的語</div><div class="t m0 x5 h4 y73f ff5 fs3 fc0 sc0 ls0 ws0">法。對於更多信息,請查閱<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>官方文件中字串格式化格式說明一節:<span class="ffa fc2">https://docs.</span></div><div class="t m0 x5 h4 y740 ffa fs3 fc2 sc0 ls0 ws0">python.org/3/library/string.html#formatspec<span class="ff5 fc0">。</span></div><div class="t m0 x12 hd y123 fff fs5 fc0 sc0 ls0 ws0">1</div><div class="t m0 xa he ydb ff5 fs6 fc0 sc0 ls0 ws0">譯者注:在本書翻譯時<span class="_ _9"> </span><span class="ff10">Python<span class="_ _9"> </span></span>更新至<span class="_ _9"> </span><span class="ff10">3.6<span class="_ _34"> </span></span>版本,該版本提供了<span class="ff6">字串內插</span>(<span class="ff10">string<span class="_ _9"> </span>in<span class="_ _4"></span>terpolation<span class="ff5">)功能,可</span></span></div><div class="t m0 x5 he yba ff5 fs6 fc0 sc0 ls0 ws0">以使用<span class="_ _36"> </span><span class="ff13">f<span class="ff1a">'</span>The<span class="_ _72"> </span>value<span class="_ _72"> </span>of<span class="_ _72"> </span>pi<span class="_ _72"> </span>is<span class="_ _72"> </span>{pi}<span class="ff1a">'<span class="_ _36"> </span></span></span>來實現上述例子同樣的效果。詳情請參閱網址:<span class="_ _8"></span><span class="ff13 fc2">https://www.python.org/</span></div><div class="t m0 x5 he y40 ff13 fs6 fc2 sc0 ls0 ws0">dev/peps/pep-0498/<span class="ff5 fc0">。</span></div><a class="l" href="#pf5e" data-dest-detail='[94,"XYZ",88.59,111.45,null]'><div class="d m1" style="border-style:none;position:absolute;left:375.999000px;bottom:554.566000px;width:4.235000px;height:5.137000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="https://docs.python.org/3/library/string.html#formatspec"><div class="d m1" style="border-style:none;position:absolute;left:448.821000px;bottom:135.291000px;width:74.455000px;height:10.000000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="https://docs.python.org/3/library/string.html#formatspec"><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:119.032000px;width:246.273000px;height:10.000000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="https://www.python.org/dev/peps/pep-0498/"><div class="d m1" style="border-style:none;position:absolute;left:415.007000px;bottom:83.158000px;width:108.269000px;height:8.219000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="https://www.python.org/dev/peps/pep-0498/"><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:70.007000px;width:85.230000px;height:8.220000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf5f" class="pf w0 h0" data-page-no="5f"><div class="pc pc5f w0 h0"><img class="bi x5 y1bd w1 h53" alt="" src="images/bg5f.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">88<span class="_ _8a"> </span><span class="ffc">15.<span class="_ _16"> </span><span class="ff6">字串處理和正則表示式</span></span></div><div class="t m0 x5 h8 ybb ff1 fs1 fc0 sc0 ls0 ws0">15.3<span class="_ _39"> </span><span class="ff2">使用正則表示式靈活地匹配字串模式</span></div><div class="t m0 x4 h4 ybc ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _3"> </span><span class="ff5">的<span class="_ _3"> </span><span class="ffa">str<span class="_ _3"> </span></span>型別給你提供了一系列強大的字串格式化、分割和操作字串資料的函</span></div><div class="t m0 x5 h4 ybd ff5 fs3 fc0 sc0 ls0 ws0">數。但是<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>還有更強大的工具,這就是正則表達式模塊。正則表達式是一個很龐大的話</div><div class="t m0 x5 h4 ybe ff5 fs3 fc0 sc0 ls0 ws0">題。有許多書整本都是圍繞這個話題進行展開(比如<span class="_ _3"> </span><span class="ff7">Jeffrey<span class="_ _3"> </span>E.F.<span class="_ _3"> </span>F<span class="_ _8"></span>riedl<span class="_ _9"> </span><span class="ff5">的<span class="_ _3"> </span><span class="ff8 fc2">Mastering<span class="_ _a"> </span>R<span class="_ _8"></span>e<span class="_ _8"></span>gular</span></span></span></div><div class="t m0 x5 h4 ybf ff8 fs3 fc2 sc0 ls0 ws0">Expr<span class="_ _8"></span>essions<span class="ff5 fc0">,因此在短短一節內講完全部的知識幾乎是不可能的。</span></div><div class="t m0 x4 h4 yc0 ff5 fs3 fc0 sc0 ls0 ws0">本<span class="_ _4a"></span>節<span class="_ _4a"></span>的<span class="_ _4a"></span>目<span class="_ _55"></span>標<span class="_ _4a"></span>是<span class="_ _4a"></span>介<span class="_ _4a"></span>紹<span class="_ _4a"></span>一<span class="_ _55"></span>些<span class="_ _4a"></span>可<span class="_ _4a"></span>以<span class="_ _4a"></span>通<span class="_ _55"></span>過正<span class="_ _4a"></span>則<span class="_ _4a"></span>表<span class="_ _4a"></span>達<span class="_ _4a"></span>式<span class="_ _55"></span>解<span class="_ _4a"></span>決<span class="_ _4a"></span>的<span class="_ _4a"></span>問<span class="_ _55"></span>題,<span class="_ _4a"></span>以<span class="_ _4a"></span>及<span class="_ _4a"></span>一<span class="_ _4a"></span>些<span class="_ _55"></span>基<span class="_ _4a"></span>本<span class="_ _4a"></span>方<span class="_ _4a"></span>法<span class="_ _55"></span>指<span class="_ _4a"></span>導<span class="_ _4a"></span>你</div><div class="t m0 x5 h4 yc1 ff5 fs3 fc0 sc0 ls0 ws0">在<span class="_ _60"> </span><span class="ff7">Python<span class="_ _36"> </span></span>中解決它們。<span class="_ _7"></span>在本章最後,<span class="_ _7"></span>我將為之後的學習提供一些參考資料,<span class="_ _7"></span>請參閱第<span class="_ _60"> </span><span class="ff7 fc1">15.3.3<span class="_ _60"> </span></span>節<span class="fc1">關</span></div><div class="t m0 x5 h4 y741 ff5 fs3 fc1 sc0 ls0 ws0">於正則表示式的更多資源<span class="fc0">。</span></div><div class="t m0 x4 h4 y742 ff5 fs3 fc0 sc0 ls0 ws0">從根本上說,正則表達式是一種解決在字串中進行靈活的<span class="ff6">模式匹配</span>(<span class="ff7">pattern<span class="_ _72"> </span>matc<span class="_ _4"></span>h-</span></div><div class="t m0 x5 h7 y743 ff7 fs3 fc0 sc0 ls0 ws0">ing</div><div class="t m0 x26 h4 y744 ff5 fs3 fc0 sc0 ls0 ws0">)<span class="_ _4a"></span>的<span class="_ _55"> </span>方<span class="_ _55"></span>法。<span class="_ _4a"></span>如<span class="_ _55"> </span>果<span class="_ _55"></span>你<span class="_ _4a"></span>經<span class="_ _55"> </span>常<span class="_ _55"></span>使<span class="_ _4a"></span>用<span class="_ _55"> </span>命<span class="_ _55"></span>令<span class="_ _4a"></span>行,<span class="_ _55"> </span>你<span class="_ _4a"></span>可<span class="_ _55"> </span>能<span class="_ _55"> </span>會<span class="_ _4a"></span>對<span class="_ _55"> </span>帶</div><div class="t m0 x4f hc y743 ffa fs3 fc0 sc0 ls0 ws0">*</div><div class="t m0 x58 h4 y744 ff5 fs3 fc0 sc0 ls0 ws0">字<span class="_ _4a"></span>符<span class="_ _55"> </span>進<span class="_ _55"></span>行<span class="_ _4a"></span>靈<span class="_ _55"> </span>活<span class="_ _55"></span>的<span class="_ _4a"></span>字<span class="_ _55"> </span><span class="_ _55"></span>串<span class="_ _4a"></span>匹<span class="_ _55"> </span>配<span class="_ _55"></span>有</div><div class="t m0 x5 h4 y745 ff5 fs3 fc0 sc0 ls0 ws0">印<span class="_ _55"></span>象。<span class="_ _4a"></span>在<span class="_ _55"> </span>這<span class="_ _55"> </span>裡<span class="_ _10"> </span><span class="ffa">*<span class="_ _10"> </span></span>是<span class="_ _55"> </span>一<span class="_ _55"> </span>個<span class="ff6">萬用<span class="_ _55"> </span>字<span class="_ _55"> </span>元<span class="_ _55"> </span></span>(<span class="ff7">wildcard</span>)。<span class="_ _55"></span>舉<span class="_ _4a"></span>例<span class="_ _55"> </span>來<span class="_ _55"> </span>說,<span class="_ _55"> </span>我<span class="_ _55"> </span>們<span class="_ _55"> </span>可<span class="_ _55"> </span>以<span class="_ _55"></span>列<span class="_ _4a"></span>出<span class="_ _55"> </span>所<span class="_ _55"> </span>有<span class="_ _55"> </span>文<span class="_ _55"> </span>件<span class="_ _55"> </span>名<span class="_ _55"> </span>中<span class="_ _55"></span>帶</div><div class="t m0 x5 h4 y746 ff5 fs3 fc0 sc0 ls0 ws0">有<span class="_ _72"> </span><span class="ff7">“Python”<span class="_ _1"> </span></span>的<span class="_ _1"> </span><span class="ff7">IPython<span class="_ _1"> </span></span>記<span class="_ _4a"></span>事本<span class="_ _4a"></span>(即<span class="_ _4a"></span>帶有<span class="_ _1"> </span><span class="ffa">.ipynb<span class="_ _1"> </span></span>副<span class="_ _4a"></span>檔<span class="_ _4a"></span>名的<span class="_ _4a"></span>文<span class="_ _4a"></span>件)<span class="_ _3e"></span>,<span class="_ _4a"></span>這<span class="_ _4a"></span>可以<span class="_ _4a"></span>通<span class="_ _4a"></span>過<span class="_ _4a"></span>使用<span class="_ _1"> </span><span class="ffa">*<span class="_ _1"> </span></span>通</div><div class="t m0 x5 h4 y747 ff5 fs3 fc0 sc0 ls0 ws0">配符來匹配檔名中的其他字元:</div><div class="t m0 x11 hc y748 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[38]:<span class="_ _2a"> </span><span class="fc6">!<span class="fc0">ls<span class="_ _2a"> </span>*Python*.ipynb</span></span></div><div class="t m0 x11 hc y749 ffa fs3 fc0 sc0 ls0 ws0">01-How-to-Run-Python-Code.ipynb<span class="_ _2a"> </span>02-Basic-Python-Syntax.ipynb</div><div class="t m0 x4 h4 y74a ff5 fs3 fc0 sc0 ls0 ws0">正則表達式擴充了「萬用字元」這一個概念,形成了一套更廣泛的靈活字串匹配的語法。</div><div class="t m0 x5 h4 y74b ff7 fs3 fc0 sc0 ls0 ws0">Python<span class="_ _9"> </span><span class="ff5">支援正則表示式的介面存放在內建的<span class="_ _3"> </span><span class="ffa">re<span class="_ _9"> </span></span>模組中。我們首先來看一個簡單的例子,這個</span></div><div class="t m0 x5 h4 y74c ff5 fs3 fc0 sc0 ls0 ws0">例子實現了與字串<span class="_ _9"> </span><span class="ffa">split()<span class="_ _3"> </span></span>函式相同的功能:</div><div class="t m0 x11 hc y74d ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[39]:<span class="_ _2a"> </span><span class="ff11 fc4">import<span class="_ _2a"> </span><span class="fc5">re</span></span></div><div class="t m0 x31 hc y74e ffa fs3 fc0 sc0 ls0 ws0">regex<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>re<span class="fc6">.</span>compile(<span class="ff12 fcc">'<span class="ffa">\s+</span>'</span>)</div><div class="t m0 x31 hc y74f ffa fs3 fc0 sc0 ls0 ws0">regex<span class="fc6">.</span>split(line)</div><div class="t m0 x11 hc y750 ffa fs3 fcf sc0 ls0 ws0">Out[39]:<span class="_ _2a"> </span><span class="fc0">[<span class="ff12">'</span>the<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>quick<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>brown<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>fox<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>jumped<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>over<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>a<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>lazy<span class="ff12">'</span>,</span></div><div class="t m0 x64 hc y751 ff12 fs3 fc0 sc0 ls0 ws0">'<span class="ffa">dog</span>'<span class="ffa">]</span></div><div class="t m0 x4 h4 y752 ff5 fs3 fc0 sc0 ls0 ws0">在<span class="_ _60"> </span>這<span class="_ _60"> </span>裡<span class="_ _60"> </span>我<span class="_ _60"> </span>們<span class="_ _60"> </span>首<span class="_ _60"> </span>先<span class="ff6">編<span class="_ _36"> </span>譯<span class="_ _89"> </span></span>(<span class="ff7">compile</span>)<span class="_ _60"> </span>了<span class="_ _36"> </span>一<span class="_ _89"> </span>個正<span class="_ _60"> </span>則<span class="_ _60"> </span>表<span class="_ _36"> </span>達<span class="_ _89"> </span>式,<span class="_ _60"> </span>然<span class="_ _36"> </span>後<span class="_ _89"> </span>用<span class="_ _60"> </span>它<span class="_ _36"> </span>來<span class="_ _89"> </span>分<span class="_ _60"> </span>割<span class="_ _36"> </span>字<span class="_ _89"> </span>符<span class="_ _60"> </span>串。</div><div class="t m0 x5 h4 y753 ff5 fs3 fc0 sc0 ls0 ws0">和<span class="_ _72"> </span><span class="ff7">Python<span class="_ _72"> </span></span>的<span class="_ _72"> </span><span class="ffa">split()<span class="_ _72"> </span></span>函數一樣,<span class="_ _4a"></span>它返回<span class="_ _4a"></span>了一個包<span class="_ _4a"></span>含所有<span class="_ _4a"></span>空格之間<span class="_ _4a"></span>的子串<span class="_ _4a"></span>的列表,正則表</div><div class="t m0 x5 h4 y754 ff5 fs3 fc0 sc0 ls0 ws0">達式的<span class="_ _9"> </span><span class="ffa">split()<span class="_ _3"> </span></span>函式回傳了一個包含所有匹配給定輸入模式串的子串的列表。</div><div class="t m0 x4 h4 y755 ff5 fs3 fc0 sc0 ls0 ws0">在這個例子中,<span class="_ _2"></span>輸入串是<span class="_ _9"> </span><span class="ffa">"\s+"</span>:<span class="_ _8"></span>其中<span class="_ _34"> </span><span class="ffa">\s<span class="_ _9"> </span></span>是一個特殊的字元,<span class="_ _8"></span>它匹配了所有空白字元<span class="_ _8"></span>(包</div><div class="t m0 x5 h4 y756 ff5 fs3 fc0 sc0 ls0 ws0">含空格、<span class="_ _2"></span><span class="ff7">T<span class="_ _8"></span>AB<span class="_ _34"> </span><span class="ff5">字元、<span class="_ _8"></span>換行符號等等)<span class="_ _33"></span>,<span class="_ _2"></span><span class="ffa">+<span class="_ _9"> </span><span class="ff5">指明了在它前面的實體出現<span class="ff2">一次或多次</span>。<span class="_ _8"></span>因此,<span class="_ _2"></span>上述正則</span></span></span></span></div><div class="t m0 x5 h4 y757 ff5 fs3 fc0 sc0 ls0 ws0">表示式匹配了任何包含一個或多個空格的子串。</div><div class="t m0 x4 h4 y758 ff5 fs3 fc0 sc0 ls0 ws0">在這裡<span class="_ _13"> </span><span class="ffa">split()<span class="_ _72"> </span></span>函數基本上是一個基於這種模式匹配行為的簡便函數。更加基礎的則</div><div class="t m0 x5 h4 y759 ff5 fs3 fc0 sc0 ls0 ws0">是<span class="_ _9"> </span><span class="ffa">match()<span class="_ _3"> </span></span>函式,這個函式會回傳一個字串的開頭是否匹配了模式串:</div><div class="t m0 x11 hc y176 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[40]:<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span><span class="fc0">s<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span>[<span class="fcc">"<span class="_ _4f"> </span>"</span>,<span class="_ _2a"> </span><span class="fcc">"abc<span class="_ _27"> </span>"</span>,<span class="_ _2a"> </span><span class="fcc">"<span class="_ _27"> </span>abc"</span>]:</span></div><div class="t m0 x28 hc y98 ff11 fs3 fc4 sc0 ls0 ws0">if<span class="_ _2a"> </span><span class="ffa fc0">regex<span class="fc6">.</span>match(s):</span></div><div class="t m0 x3a hc y99 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(</span>repr<span class="fc0">(s),<span class="_ _2a"> </span><span class="fcc">"matches"</span>)</span></div><div class="t m0 x28 hc y9a ff11 fs3 fc4 sc0 ls0 ws0">else<span class="ffa fc0">:</span></div><div class="t m0 x3a hc y40 ffa fs3 fc4 sc0 ls0 ws0">print<span class="fc0">(</span>repr<span class="fc0">(s),<span class="_ _2a"> </span><span class="fcc">"does<span class="_ _2a"> </span>not<span class="_ _2a"> </span>match"</span>)</span></div><a class="l" href="http://shop.oreilly.com/product/9780596528126.do"><div class="d m1" style="border-style:none;position:absolute;left:435.920000px;bottom:693.114000px;width:87.356000px;height:9.636000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="http://shop.oreilly.com/product/9780596528126.do"><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:676.855000px;width:55.533000px;height:9.636000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf65" data-dest-detail='[101,"XYZ",72,546.13,null]'><div class="d m1" style="border-style:none;position:absolute;left:471.023000px;bottom:646.458000px;width:27.879000px;height:6.870000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf65" data-dest-detail='[101,"XYZ",72,546.13,null]'><div class="d m1" style="border-style:none;position:absolute;left:512.367000px;bottom:643.347000px;width:10.909000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf65" data-dest-detail='[101,"XYZ",72,546.13,null]'><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:627.088000px;width:120.000000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf60" class="pf w0 h0" data-page-no="60"><div class="pc pc60 w0 h0"><img class="bi x5 y75a w1 h54" alt="" src="images/bg60.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">15.3.<span class="_ _16"> </span><span class="ff6">使用正則表示式靈活地匹配字串模式<span class="_ _8c"> </span><span class="ff7">89</span></span></div><div class="t m0 x11 hc y1d7 ffa fs3 fc0 sc0 ls0 ws0">("<span class="ff12">'<span class="_ _4f"> </span>'</span>",<span class="_ _2a"> </span><span class="ff12">'</span>matches<span class="ff12">'</span>)</div><div class="t m0 x11 hc y348 ffa fs3 fc0 sc0 ls0 ws0">("<span class="ff12">'</span>abc<span class="_ _27"> </span><span class="ff12">'</span>",<span class="_ _2a"> </span><span class="ff12">'</span>does<span class="_ _2a"> </span>not<span class="_ _2a"> </span>match<span class="ff12">'</span>)</div><div class="t m0 x11 hc y349 ffa fs3 fc0 sc0 ls0 ws0">("<span class="ff12">'<span class="_ _27"> </span></span>abc<span class="ff12">'</span>",<span class="_ _2a"> </span><span class="ff12">'</span>matches<span class="ff12">'</span>)</div><div class="t m0 x4 h4 y75b ff5 fs3 fc0 sc0 ls0 ws0">像 <span class="ffa">split()<span class="_ _17"> </span></span>一<span class="_ _1"> </span>樣,<span class="ff7">Python<span class="_ _e"> </span></span>有<span class="_ _72"> </span>類別<span class="_ _10"> </span>似<span class="_ _72"> </span>的<span class="_ _10"> </span>方<span class="_ _1"> </span>便<span class="_ _1"> </span>的函<span class="_ _10"> </span>數<span class="_ _72"> </span>來<span class="_ _10"> </span>尋<span class="_ _72"> </span>找<span class="_ _10"> </span>首<span class="_ _1"> </span>次<span class="_ _1"> </span>匹<span class="_ _1"> </span>配<span class="_ _10"> </span>(類別</div><div class="t m0 x5 h4 y75c ff5 fs3 fc0 sc0 ls0 ws0">似<span class="_ _13"> </span><span class="ffa">str.index()<span class="_ _72"> </span></span>或<span class="_ _72"> </span><span class="ffa">str.find()</span>)或者尋找<span class="_ _4a"></span>後匹配(類別似<span class="_ _72"> </span><span class="ffa">str.replace()</span>)<span class="_ _42"></span>。我們會<span class="_ _4a"></span>再次使</div><div class="t m0 x5 h4 y75d ff5 fs3 fc0 sc0 ls0 ws0">用之前用過的這行字串:</div><div class="t m0 x11 hc y75e ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[41]:<span class="_ _2a"> </span><span class="fc0">line<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">the<span class="_ _2a"> </span>quick<span class="_ _2a"> </span>brown<span class="_ _2a"> </span>fox<span class="_ _2a"> </span>jumped<span class="_ _2a"> </span>over<span class="_ _2a"> </span>a<span class="_ _2a"> </span>lazy<span class="_ _2a"> </span>dog</span>'</span></span></span></div><div class="t m0 x4 h4 y75f ff5 fs3 fc0 sc0 ls0 ws0">有<span class="_ _10"> </span>了<span class="_ _10"> </span>這<span class="_ _10"> </span>行<span class="_ _10"> </span>字<span class="_ _10"> </span>符<span class="_ _10"> </span>串,<span class="_ _10"> </span>我<span class="_ _1"> </span>們<span class="_ _10"> </span>可<span class="_ _10"> </span>以<span class="_ _10"> </span>發<span class="_ _10"> </span>現<span class="_ _e"> </span><span class="ffa">regex.search()<span class="_ _27"> </span></span>函<span class="_ _10"> </span>數<span class="_ _10"> </span>的<span class="_ _10"> </span>行<span class="_ _10"> </span>為<span class="_ _10"> </span>非<span class="_ _10"> </span>常</div><div class="t m0 x5 h4 y760 ff5 fs3 fc0 sc0 ls0 ws0">像<span class="_ _9"> </span><span class="ffa">str.index()<span class="_ _3"> </span></span>或<span class="_ _9"> </span><span class="ffa">str.find()<span class="_ _9"> </span></span>函式:</div><div class="t m0 x11 hc y761 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[42]:<span class="_ _2a"> </span><span class="fc0">line<span class="fc6">.</span>index(<span class="ff12 fcc">'<span class="ffa">fox</span>'</span>)</span></div><div class="t m0 x11 hc y762 ffa fs3 fcf sc0 ls0 ws0">Out[42]:<span class="_ _2a"> </span><span class="fc0">16</span></div><div class="t m0 x11 hc y763 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[43]:<span class="_ _2a"> </span><span class="fc0">regex<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>re<span class="fc6">.</span>compile(<span class="ff12 fcc">'<span class="ffa">fox</span>'</span>)</span></div><div class="t m0 x31 hc y764 ffa fs3 fc0 sc0 ls0 ws0">match<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>regex<span class="fc6">.</span>search(line)</div><div class="t m0 x31 hc y765 ffa fs3 fc0 sc0 ls0 ws0">match<span class="fc6">.</span>start()</div><div class="t m0 x11 hc y766 ffa fs3 fcf sc0 ls0 ws0">Out[43]:<span class="_ _2a"> </span><span class="fc0">16</span></div><div class="t m0 x4 h4 y767 ff5 fs3 fc0 sc0 ls0 ws0">類似地,<span class="ffa">regex.sub()<span class="_ _9"> </span></span>函式的行為很像<span class="_ _3"> </span><span class="ffa">str.replace()</span>:</div><div class="t m0 x11 hc y768 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[44]:<span class="_ _2a"> </span><span class="fc0">line<span class="fc6">.</span>replace(<span class="ff12 fcc">'<span class="ffa">fox</span>'</span>,<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">BEAR</span>'</span>)</span></div><div class="t m0 x11 hc y769 ffa fs3 fcf sc0 ls0 ws0">Out[44]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">the<span class="_ _2a"> </span>quick<span class="_ _2a"> </span>brown<span class="_ _2a"> </span>BEAR<span class="_ _2a"> </span>jumped<span class="_ _2a"> </span>over<span class="_ _2a"> </span>a<span class="_ _2a"> </span>lazy<span class="_ _2a"> </span>dog</span>'</span></div><div class="t m0 x11 hc y76a ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[45]:<span class="_ _2a"> </span><span class="fc0">regex<span class="fc6">.</span>sub(<span class="ff12 fcc">'<span class="ffa">BEAR</span>'</span>,<span class="_ _2a"> </span>line)</span></div><div class="t m0 x11 hc y76b ffa fs3 fcf sc0 ls0 ws0">Out[45]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">the<span class="_ _2a"> </span>quick<span class="_ _2a"> </span>brown<span class="_ _2a"> </span>BEAR<span class="_ _2a"> </span>jumped<span class="_ _2a"> </span>over<span class="_ _2a"> </span>a<span class="_ _2a"> </span>lazy<span class="_ _2a"> </span>dog</span>'</span></div><div class="t m0 x4 h4 y76c ff5 fs3 fc0 sc0 ls0 ws0">你可能會有一點這樣的想法:其他簡單的字串操作也可以轉換為正則表示式進行操作。</div><div class="t m0 x5 ha y76d ff1 fs2 fc0 sc0 ls0 ws0">15.3.1<span class="_ _3c"> </span><span class="ff2">一個更複雜的例子</span></div><div class="t m0 x4 h4 y76e ff5 fs3 fc0 sc0 ls0 ws0">但是你可能會問,為什麼要用這種相對來說更複雜的正則表達式語法,而不是用更符合直</div><div class="t m0 x5 h4 y76f ff5 fs3 fc0 sc0 ls0 ws0">覺、更簡單的字串操作呢?正則表示式的優點是它提供的特性<span class="ff2">遠不止</span>靈活性這麼簡單。</div><div class="t m0 x4 h4 y99 ff5 fs3 fc0 sc0 ls0 ws0">這裡我們將會再考慮一個更加複雜的例子,同時也是一個非常常見的任務——匹配電子郵</div><div class="t m0 x5 h4 y9a ff5 fs3 fc0 sc0 ls0 ws0">件地址。我將從粗暴地寫下一個難以理解的正則表達式開始,然後一步一步地帶你理解其中的</div><div class="t m0 x5 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">內涵。這個複雜的表示式如下:</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf61" class="pf w0 h0" data-page-no="61"><div class="pc pc61 w0 h0"><img class="bi x5 y40 w1 h11" alt="" src="images/bg61.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">90<span class="_ _8a"> </span><span class="ffc">15.<span class="_ _16"> </span><span class="ff6">字串處理和正則表示式</span></span></div><div class="t m0 x11 hc yfa ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[46]:<span class="_ _2a"> </span><span class="fc0">email<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>re<span class="fc6">.</span>compile(<span class="ff12 fcc">'<span class="ffa">\w+@\w+\.[a-z]<span class="ff11 fc10">{3}</span></span>'</span>)</span></div><div class="t m0 x4 h4 y770 ff5 fs3 fc0 sc0 ls0 ws0">如果從文件中有一行文字,那麼你可以使用這個正則表示式迅速提取看上去像電子郵件地</div><div class="t m0 x5 h4 y771 ff5 fs3 fc0 sc0 ls0 ws0">址的內容:</div><div class="t m0 x11 hc y772 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[47]:<span class="_ _2a"> </span><span class="fc0">text<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="fcc">"To<span class="_ _2a"> </span>email<span class="_ _2a"> </span>Guido,<span class="_ _2a"> </span>try<span class="_ _2a"> </span>[email protected]<span class="_ _2a"> </span>or<span class="_ _2a"> </span>the<span class="_ _2a"> </span>older<span class="_ _2a"> </span>address<span class="_ _2a"> </span>"</span></span>\</span></div><div class="t m0 x25 hc y773 ffa fs3 fcc sc0 ls0 ws0">"[email protected]."</div><div class="t m0 x31 hc y774 ffa fs3 fc0 sc0 ls0 ws0">email<span class="fc6">.</span>findall(text)</div><div class="t m0 x11 hc y775 ffa fs3 fcf sc0 ls0 ws0">Out[47]:<span class="_ _2a"> </span><span class="fc0">[<span class="ff12">'</span>[email protected]<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>[email protected]<span class="ff12">'</span>]</span></div><div class="t m0 xa h4 y776 ff5 fs3 fc0 sc0 ls0 ws0">(請注意,這些地址完全是瞎編的。也許有更好的方法與<span class="_ _9"> </span><span class="ff7">Guido<span class="_ _3"> </span></span>取得聯絡……)</div><div class="t m0 x4 h4 y777 ff5 fs3 fc0 sc0 ls0 ws0">我們可以進行進一步的操作,比如將這些電子郵件地址替換為其他的字元串,這可以達到</div><div class="t m0 x5 h4 y778 ff5 fs3 fc0 sc0 ls0 ws0">在輸出中隱藏地址、保護個人資訊的作用:</div><div class="t m0 x11 hc y779 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[48]:<span class="_ _2a"> </span><span class="fc0">email<span class="fc6">.</span>sub(<span class="ff12 fcc">'<span class="ffa">[email protected]</span>'</span>,<span class="_ _2a"> </span>text)</span></div><div class="t m0 x11 hc y77a ffa fs3 fcf sc0 ls0 ws0">Out[48]:<span class="_ _2a"> </span><span class="ff12 fc0">'<span class="ffa">To<span class="_ _2a"> </span>email<span class="_ _2a"> </span>Guido,<span class="_ _2a"> </span>try<span class="_ _2a"> </span>[email protected]<span class="_ _2a"> </span>or<span class="_ _2a"> </span>the<span class="_ _2a"> </span>older<span class="_ _2a"> </span>address<span class="_ _2a"> </span>[email protected].</span>'</span></div><div class="t m0 x4 h4 y77b ff5 fs3 fc0 sc0 ls0 ws0">最後,請注意,如果你真的需要匹配<span class="ff2">任意</span>電子郵件地址,之前這個正則表達式太過於簡單</div><div class="t m0 x5 h4 y77c ff5 fs3 fc0 sc0 ls0 ws0">了。比如它只允許由數字和字母組成的地址,並且要求以常見的幾個域名後綴結尾。因此舉例</div><div class="t m0 x5 h4 y77d ff5 fs3 fc0 sc0 ls0 ws0">來說,我們目前使用的時候只是意味著我們暫時只能找到符合要求地址的一部分:</div><div class="t m0 x11 hc y77e ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[49]:<span class="_ _2a"> </span><span class="fc0">email<span class="fc6">.</span>findall(<span class="ff12 fcc">'<span class="ffa">[email protected]</span>'</span>)</span></div><div class="t m0 x11 hc y77f ffa fs3 fcf sc0 ls0 ws0">Out[49]:<span class="_ _2a"> </span><span class="fc0">[<span class="ff12">'</span>[email protected]<span class="ff12">'</span>]</span></div><div class="t m0 x4 h4 y780 ff5 fs3 fc0 sc0 ls0 ws0">這個例子表明如果你不小心的話,使用一個錯誤的正則表達式將帶來不可饒恕的錯誤!如</div><div class="t m0 x5 h4 y781 ff5 fs3 fc0 sc0 ls0 ws0">果你在網際網路上搜尋,你會找到不少用來匹配<span class="ff2">全部</span>合法電子郵件地址的正則表示式的建議,但</div><div class="t m0 x5 h4 y782 ff5 fs3 fc0 sc0 ls0 ws0">是需要小心:它們比這裡用到的例子要複雜太多!</div><div class="t m0 x5 ha y783 ff1 fs2 fc0 sc0 ls0 ws0">15.3.2<span class="_ _3c"> </span><span class="ff2">正則表示式語法基礎</span></div><div class="t m0 x4 h4 y784 ff5 fs3 fc0 sc0 ls0 ws0">正則表達式的語法比本節討論的要複雜許多。我仍然認為熟悉其中的一部分能夠給今後的</div><div class="t m0 x5 h4 y785 ff5 fs3 fc0 sc0 ls0 ws0">學習打下基礎。因此我將在這裡介紹一些基本的重要知識,然後列出一些更完整的資源,從中你</div><div class="t m0 x5 h4 y786 ff5 fs3 fc0 sc0 ls0 ws0">可以了解到更多資訊。我希望下面的這個初學者入門指引能使你高效地利用這些資源。</div><div class="t m0 x5 h4 y787 ff2 fs3 fc0 sc0 ls0 ws0">簡單的字串直接匹配。<span class="_ _13"> </span><span class="ff5">如果你直接用簡單的字母或者數字建立正則表示式的話,<span class="_ _8"></span>它會直接精</span></div><div class="t m0 x5 h4 y788 ff5 fs3 fc0 sc0 ls0 ws0">確匹配對應的字串:</div><div class="t m0 x11 hc y21a ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[50]:<span class="_ _2a"> </span><span class="fc0">regex<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>re<span class="fc6">.</span>compile(<span class="ff12 fcc">'<span class="ffa">ion</span>'</span>)</span></div><div class="t m0 x31 hc y117 ffa fs3 fc0 sc0 ls0 ws0">regex<span class="fc6">.</span>findall(<span class="ff12 fcc">'<span class="ffa">Great<span class="_ _2a"> </span>Expectations</span>'</span>)</div><div class="t m0 x11 hc y119 ffa fs3 fcf sc0 ls0 ws0">Out[50]:<span class="_ _2a"> </span><span class="fc0">[<span class="ff12">'</span>ion<span class="ff12">'</span>]</span></div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf62" class="pf w0 h0" data-page-no="62"><div class="pc pc62 w0 h0"><img class="bi x5 y789 w1 h55" alt="" src="images/bg62.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">15.3.<span class="_ _16"> </span><span class="ff6">使用正則表示式靈活地匹配字串模式<span class="_ _8c"> </span><span class="ff7">91</span></span></div><div class="t m0 x5 h4 y42 ff2 fs3 fc0 sc0 ls0 ws0">一些字元有特殊含義。<span class="_ _13"> </span><span class="ff5">儘管一些字元或者數字是直接匹配的,<span class="_ _8"></span>有另外一些字元在正則表示式裡</span></div><div class="t m0 x5 h4 y77 ff5 fs3 fc0 sc0 ls0 ws0">是有特殊含義的。它們是:</div><div class="t m0 x5 hc y78a ffa fs3 fc0 sc0 ls0 ws0">.<span class="_ _2a"> </span>^<span class="_ _2a"> </span>$<span class="_ _2a"> </span>*<span class="_ _2a"> </span>+<span class="_ _2a"> </span>?<span class="_ _2a"> </span>{<span class="_ _2a"> </span>}<span class="_ _2a"> </span>[<span class="_ _2a"> </span>]<span class="_ _2a"> </span>\<span class="_ _2a"> </span>|<span class="_ _2a"> </span>(<span class="_ _2a"> </span>)</div><div class="t m0 x4 h4 y78b ff5 fs3 fc0 sc0 ls0 ws0">我們馬上會開始討論其中一些字符的含義。同時,你需要知道如果你希望直接匹配這其中</div><div class="t m0 x5 h4 y78c ff5 fs3 fc0 sc0 ls0 ws0">任何一個字元,你可以使用一個反斜線<span class="ff6">跳脫</span>(<span class="ff7">escape</span>)它們:</div><div class="t m0 x11 hc y78d ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[51]:<span class="_ _2a"> </span><span class="fc0">regex<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>re<span class="fc6">.</span>compile(<span class="fcc">r<span class="ff12">'</span>\$<span class="ff12">'</span></span>)</span></div><div class="t m0 x31 hc y78e ffa fs3 fc0 sc0 ls0 ws0">regex<span class="fc6">.</span>findall(<span class="fcc">"the<span class="_ _2a"> </span>cost<span class="_ _2a"> </span>is<span class="_ _2a"> </span>$20"</span>)</div><div class="t m0 x11 hc y78f ffa fs3 fcf sc0 ls0 ws0">Out[51]:<span class="_ _2a"> </span><span class="fc0">[<span class="ff12">'</span>$<span class="ff12">'</span>]</span></div><div class="t m0 x4 h4 y790 ffa fs3 fc0 sc0 ls0 ws0">r<span class="ff12">'</span>\$<span class="ff12">'<span class="_ _3"> </span><span class="ff5">的<span class="_ _3"> </span></span></span>r<span class="_ _9"> </span><span class="ff5">前綴表明這是一個<span class="ff6">原始字串</span>(<span class="ff7">raw<span class="_ _9"> </span>string</span>)<span class="_ _7"></span>。在<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>標準字串中,反斜</span></div><div class="t m0 x5 h4 y791 ff5 fs3 fc0 sc0 ls0 ws0">線用來表示特殊的字元。舉例來說,<span class="ff7">T<span class="_ _2"></span>AB<span class="_ _3"> </span><span class="ff5">製表符是用<span class="_ _9"> </span><span class="ffa">\t<span class="_ _9"> </span></span>來代表的:</span></span></div><div class="t m0 x11 hc y792 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[52]:<span class="_ _2a"> </span><span class="fc4">print<span class="fc0">(<span class="ff12 fcc">'<span class="ffa">a<span class="ff11 fc12">\t</span>b<span class="ff11 fc12">\t</span>c</span>'</span>)</span></span></div><div class="t m0 x11 hc y793 ffa fs3 fc0 sc0 ls0 ws0">a<span class="_ _8d"> </span>b<span class="_ _8d"> </span>c</div><div class="t m0 x4 h4 y794 ff5 fs3 fc0 sc0 ls0 ws0">這樣的替換不會在原始字串中進行:</div><div class="t m0 x11 hc y795 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[53]:<span class="_ _2a"> </span><span class="fc4">print<span class="fc0">(<span class="fcc">r<span class="ff12">'</span>a\tb\tc<span class="ff12">'</span></span>)</span></span></div><div class="t m0 x11 hc y796 ffa fs3 fc0 sc0 ls0 ws0">a\tb\tc</div><div class="t m0 x4 h4 y797 ff5 fs3 fc0 sc0 ls0 ws0">基於這樣一種原因考慮,無論何時,只要在正則表達式中使用反斜槓,最好使用原始字元</div><div class="t m0 x5 h4 y798 ff5 fs3 fc0 sc0 ls0 ws0">串。</div><div class="t m0 x5 h4 y799 ff2 fs3 fc0 sc0 ls0 ws0">特殊字符可以匹配字Tuple。<span class="_ _13"> </span><span class="ff5">就像在正則表達式中<span class="_ _3"> </span><span class="ffa">\<span class="_ _a"> </span></span>字元可以通過轉義特殊的字元使它們變成</span></div><div class="t m0 x5 h4 y79a ff5 fs3 fc0 sc0 ls0 ws0">普通的字元,它也可以用來賦予普通字元意義。這些特殊的字元可以匹配指定的字Tuple,我們</div><div class="t m0 x5 h4 y79b ff5 fs3 fc0 sc0 ls0 ws0">已經見到了它們的作用。在電子郵件匹配的正則表達式中,我們使用<span class="_ _3"> </span><span class="ffa">\w<span class="_ _3"> </span></span>標記匹配<span class="ff2">任意數字或</span></div><div class="t m0 x5 h4 y79c ff2 fs3 fc0 sc0 ls0 ws0">者字母<span class="ff5">。<span class="_ _8"></span>類似地,<span class="_ _8"></span>在簡單的<span class="_ _34"> </span><span class="ffa">split()<span class="_ _9"> </span></span>例子中,<span class="_ _8"></span>我們也見到了<span class="_ _9"> </span><span class="ffa">\s</span>,<span class="_ _8"></span>這是一個特殊的標記,<span class="_ _8"></span>匹配<span class="ff2">任</span></span></div><div class="t m0 x5 h4 y79d ff2 fs3 fc0 sc0 ls0 ws0">意空白字元<span class="ff5">。</span></div><div class="t m0 x4 h4 y79e ff5 fs3 fc0 sc0 ls0 ws0">把這些組合在一起,我們可以組成一個匹配<span class="ff2">任何兩個字母<span class="ff9">/</span>數字之間有空格</span>字串的正則</div><div class="t m0 x5 h4 y79f ff5 fs3 fc0 sc0 ls0 ws0">表示式:</div><div class="t m0 x11 hc y7a0 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[54]:<span class="_ _2a"> </span><span class="fc0">regex<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>re<span class="fc6">.</span>compile(<span class="fcc">r<span class="ff12">'</span>\w\s\w<span class="ff12">'</span></span>)</span></div><div class="t m0 x31 hc y7a1 ffa fs3 fc0 sc0 ls0 ws0">regex<span class="fc6">.</span>findall(<span class="ff12 fcc">'<span class="ffa">the<span class="_ _2a"> </span>fox<span class="_ _2a"> </span>is<span class="_ _2a"> </span>9<span class="_ _2a"> </span>years<span class="_ _2a"> </span>old</span>'</span>)</div><div class="t m0 x11 hc y7a2 ffa fs3 fcf sc0 ls0 ws0">Out[54]:<span class="_ _2a"> </span><span class="fc0">[<span class="ff12">'</span>e<span class="_ _2a"> </span>f<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>x<span class="_ _2a"> </span>i<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>s<span class="_ _2a"> </span>9<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>s<span class="_ _2a"> </span>o<span class="ff12">'</span>]</span></div><div class="t m0 x4 h4 y7a3 ff5 fs3 fc0 sc0 ls0 ws0">從這個例子開始,正則表示式的強大和靈活性漸漸開始顯現。</div><div class="t m0 x4 h4 y7a4 ff5 fs3 fc0 sc0 ls0 ws0">下表列出了一些通常比較有用的字元和它們對應的意義:</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf63" class="pf w0 h0" data-page-no="63"><div class="pc pc63 w0 h0"><img class="bi x5 y235 w1 h52" alt="" src="images/bg63.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">92<span class="_ _8a"> </span><span class="ffc">15.<span class="_ _16"> </span><span class="ff6">字串處理和正則表示式</span></span></div><div class="t m0 x70 h5 y27d ff2 fs3 fc0 sc0 ls0 ws0">字元<span class="_ _5c"> </span>描述</div><div class="t m0 x46 h4 y27e ffa fs3 fc0 sc0 ls0 ws0">\d<span class="_ _49"> </span><span class="ff5">匹配任意數字</span></div><div class="t m0 x46 h4 y27f ffa fs3 fc0 sc0 ls0 ws0">\D<span class="_ _49"> </span><span class="ff5">匹配任意非數字</span></div><div class="t m0 x46 h4 y280 ffa fs3 fc0 sc0 ls0 ws0">\s<span class="_ _49"> </span><span class="ff5">匹配任意空白字元</span></div><div class="t m0 x46 h4 y281 ffa fs3 fc0 sc0 ls0 ws0">\S<span class="_ _49"> </span><span class="ff5">匹配任意非空白字元</span></div><div class="t m0 x46 h4 y7a5 ffa fs3 fc0 sc0 ls0 ws0">\w<span class="_ _49"> </span><span class="ff5">匹配任意字母和數字</span></div><div class="t m0 x46 h4 y7a6 ffa fs3 fc0 sc0 ls0 ws0">\W<span class="_ _49"> </span><span class="ff5">匹配任意非字母和數字</span></div><div class="t m0 x4 h4 y7a7 ff5 fs3 fc0 sc0 ls0 ws0">這個表格並<span class="ff2">不是</span>一個完整的例子,也沒有提供完整的描述。如果你需要獲得更多細節,</div><div class="t m0 x5 h4 y7a8 ff5 fs3 fc0 sc0 ls0 ws0">請查閱<span class="_ _a"> </span><span class="ff7">Python<span class="_ _13"> </span></span>官方正則表達式語法文件:<span class="ffa fc2">https://docs.python.org/3/library/re.html#</span></div><div class="t m0 x5 h4 y7a9 ffa fs3 fc2 sc0 ls0 ws0">re-syntax<span class="ff5 fc0">。</span></div><div class="t m0 x5 h4 y7aa ff2 fs3 fc0 sc0 ls0 ws0">方括號匹配自定義字Tuple。<span class="_ _13"> </span><span class="ff5">如果內建的字Tuple不能滿足你的定製需求,<span class="_ _8"></span>你可以使用方括號來指</span></div><div class="t m0 x5 h4 y7ab ff5 fs3 fc0 sc0 ls0 ws0">定任意字元的集合。舉例來說,下列正則表示式可以匹配任意小寫母音字母:</div><div class="t m0 x11 hc y7ac ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[55]:<span class="_ _2a"> </span><span class="fc0">regex<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>re<span class="fc6">.</span>compile(<span class="ff12 fcc">'<span class="ffa">[aeiou]</span>'</span>)</span></div><div class="t m0 x31 hc y7ad ffa fs3 fc0 sc0 ls0 ws0">regex<span class="fc6">.</span>split(<span class="ff12 fcc">'<span class="ffa">consequential</span>'</span>)</div><div class="t m0 x11 hc y7ae ffa fs3 fcf sc0 ls0 ws0">Out[55]:<span class="_ _2a"> </span><span class="fc0">[<span class="ff12">'</span>c<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>ns<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>q<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">''</span>,<span class="_ _2a"> </span><span class="ff12">'</span>nt<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">''</span>,<span class="_ _2a"> </span><span class="ff12">'</span>l<span class="ff12">'</span>]</span></div><div class="t m0 x4 h4 y7af ff5 fs3 fc0 sc0 ls0 ws0">類別似地,<span class="_ _4a"></span>你可<span class="_ _4a"></span>以使用<span class="_ _4a"></span>短橫線<span class="_ _4a"></span>(<span class="ffa">-</span>)來指<span class="_ _4a"></span>定字<span class="_ _4a"></span>符的範<span class="_ _4a"></span>圍:比如<span class="_ _72"> </span><span class="ffa">[a-z]<span class="_ _72"> </span></span>匹<span class="_ _4a"></span>配任<span class="_ _4a"></span>意小寫<span class="_ _4a"></span>字母,</div><div class="t m0 x5 h4 y7b0 ffa fs3 fc0 sc0 ls0 ws0">[1-3]<span class="_ _9"> </span><span class="ff5">匹配任意數字<span class="_ _3"> </span></span>1<span class="ff5">、</span>2<span class="_ _9"> </span><span class="ff5">和<span class="_ _9"> </span></span>3<span class="ff5">。</span></div><div class="t m0 x4 h4 y7b1 ff5 fs3 fc0 sc0 ls0 ws0">你可能需要從文件中提取特定的數字程式碼,比如包含一個大寫字母後跟一個數字。你可以</div><div class="t m0 x5 h4 y7b2 ff5 fs3 fc0 sc0 ls0 ws0">這樣使用正則表示式:</div><div class="t m0 x11 hc y7b3 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[56]:<span class="_ _2a"> </span><span class="fc0">regex<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>re<span class="fc6">.</span>compile(<span class="ff12 fcc">'<span class="ffa">[A-Z][0-9]</span>'</span>)</span></div><div class="t m0 x31 hc y7b4 ffa fs3 fc0 sc0 ls0 ws0">regex<span class="fc6">.</span>findall(<span class="ff12 fcc">'<span class="ffa">1043879,<span class="_ _2a"> </span>G2,<span class="_ _2a"> </span>H6</span>'</span>)</div><div class="t m0 x11 hc y7b5 ffa fs3 fcf sc0 ls0 ws0">Out[56]:<span class="_ _2a"> </span><span class="fc0">[<span class="ff12">'</span>G2<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>H6<span class="ff12">'</span>]</span></div><div class="t m0 x5 h4 y7b6 ff2 fs3 fc0 sc0 ls0 ws0">萬用字元匹配重複字元。<span class="_ _13"> </span><span class="ff5">如果你需要匹配一個一行中有三個字母或數字的字串,<span class="_ _8"></span>你也許會這樣</span></div><div class="t m0 x5 h4 y7b7 ff5 fs3 fc0 sc0 ls0 ws0">組成正則表達式:<span class="ffa">\w\w\w</span>。但是,由於這是一個普遍的需求,有更具體的語法支援這一重複匹</div><div class="t m0 x5 h4 y7b8 ff5 fs3 fc0 sc0 ls0 ws0">配的需要——用花括號括起一個數字:</div><div class="t m0 x11 hc y7b9 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[57]:<span class="_ _2a"> </span><span class="fc0">regex<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>re<span class="fc6">.</span>compile(<span class="fcc">r<span class="ff12">'</span>\w<span class="ff11 fc10">{3}</span><span class="ff12">'</span></span>)</span></div><div class="t m0 x31 hc y7ba ffa fs3 fc0 sc0 ls0 ws0">regex<span class="fc6">.</span>findall(<span class="ff12 fcc">'<span class="ffa">The<span class="_ _2a"> </span>quick<span class="_ _2a"> </span>brown<span class="_ _2a"> </span>fox</span>'</span>)</div><div class="t m0 x11 hc y7bb ffa fs3 fcf sc0 ls0 ws0">Out[57]:<span class="_ _2a"> </span><span class="fc0">[<span class="ff12">'</span>The<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>qui<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>bro<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>fox<span class="ff12">'</span>]</span></div><div class="t m0 x4 h4 y9a ff5 fs3 fc0 sc0 ls0 ws0">同樣,可以使用另外一些標記來匹配任意數量的重復字符——比如,<span class="ffa">+<span class="_ _3"> </span></span>字元將會匹配前面</div><div class="t m0 x5 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">字元出現<span class="_ _9"> </span><span class="ff15">1<span class="_ _a"> </span><span class="ff2">次或多次</span></span>重複的情況:</div><a class="l" href="https://docs.python.org/3/library/re.html#re-syntax"><div class="d m1" style="border-style:none;position:absolute;left:282.731000px;bottom:594.361000px;width:240.545000px;height:10.001000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="https://docs.python.org/3/library/re.html#re-syntax"><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:578.102000px;width:52.044000px;height:8.561000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf64" class="pf w0 h0" data-page-no="64"><div class="pc pc64 w0 h0"><img class="bi x5 y40 w1 h11" alt="" src="images/bg64.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">15.3.<span class="_ _16"> </span><span class="ff6">使用正則表示式靈活地匹配字串模式<span class="_ _8c"> </span><span class="ff7">93</span></span></div><div class="t m0 x11 hc yfa ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[58]:<span class="_ _2a"> </span><span class="fc0">regex<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>re<span class="fc6">.</span>compile(<span class="fcc">r<span class="ff12">'</span>\w+<span class="ff12">'</span></span>)</span></div><div class="t m0 x31 hc yfb ffa fs3 fc0 sc0 ls0 ws0">regex<span class="fc6">.</span>findall(<span class="ff12 fcc">'<span class="ffa">The<span class="_ _2a"> </span>quick<span class="_ _2a"> </span>brown<span class="_ _2a"> </span>fox</span>'</span>)</div><div class="t m0 x11 hc y21b ffa fs3 fcf sc0 ls0 ws0">Out[58]:<span class="_ _2a"> </span><span class="fc0">[<span class="ff12">'</span>The<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>quick<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>brown<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>fox<span class="ff12">'</span>]</span></div><div class="t m0 x4 h4 y21c ff5 fs3 fc0 sc0 ls0 ws0">下表總結了正則表示式中的重複標記:</div><div class="t m0 x71 h5 y7bc ff2 fs3 fc0 sc0 ls0 ws0">字元<span class="_ _8e"> </span>描述<span class="_ _8f"> </span>舉例</div><div class="t m0 x71 h4 y7bd ffa fs3 fc0 sc0 ls0 ws0">?<span class="_ _5d"> </span><span class="ff5">匹配前面字元<span class="_ _9"> </span><span class="ff7">0<span class="_ _3"> </span></span>次或<span class="_ _9"> </span><span class="ff7">1<span class="_ _9"> </span></span>次重複<span class="_ _12"> </span></span>ab?<span class="_ _3"> </span><span class="ff5">匹配<span class="_ _9"> </span></span>"a"<span class="_ _3"> </span><span class="ff5">或<span class="_ _9"> </span></span>"ab"</div><div class="t m0 x71 h4 y7be ffa fs3 fc0 sc0 ls0 ws0">*<span class="_ _5d"> </span><span class="ff5">匹配前面字元<span class="_ _9"> </span><span class="ff7">0<span class="_ _3"> </span></span>次或多次重複<span class="_ _90"> </span></span>ab*<span class="_ _9"> </span><span class="ff5">匹配<span class="_ _3"> </span></span>"a"<span class="ff5">、</span>"ab"<span class="ff5">、</span>"abb"<span class="ff5">、</span>"abbb"<span class="ff5">…</span></div><div class="t m0 x71 h4 y7bf ffa fs3 fc0 sc0 ls0 ws0">+<span class="_ _5d"> </span><span class="ff5">匹配前面字元<span class="_ _9"> </span><span class="ff7">1<span class="_ _3"> </span></span>次或多次重複<span class="_ _90"> </span></span>ab+<span class="_ _9"> </span><span class="ff5">匹配<span class="_ _3"> </span></span>"ab"<span class="ff5">、</span>"abb"<span class="ff5">、</span>"abbb"<span class="ff5">…但是不匹配<span class="_ _9"> </span></span>"a"</div><div class="t m0 x71 h4 y7c0 ffa fs3 fc0 sc0 ls0 ws0">{n}<span class="_ _32"> </span><span class="ff5">匹配前面字元<span class="_ _3"> </span></span>n<span class="_ _9"> </span><span class="ff5">次重複<span class="_ _91"> </span></span>ab{2}<span class="_ _9"> </span><span class="ff5">匹配<span class="_ _3"> </span></span>"abb"</div><div class="t m0 x71 h4 y7c1 ffa fs3 fc0 sc0 ls0 ws0">{m,n}<span class="_ _5c"> </span><span class="ff5">匹配前面字元<span class="_ _9"> </span></span>m<span class="_ _3"> </span><span class="ff5">次到<span class="_ _9"> </span></span>n<span class="_ _9"> </span><span class="ff5">次重複<span class="_ _5c"> </span></span>ab{2,3}<span class="_ _3"> </span><span class="ff5">匹配<span class="_ _9"> </span></span>"abb"<span class="_ _9"> </span><span class="ff5">或<span class="_ _3"> </span></span>"abbb"</div><div class="t m0 x4 h4 y7c2 ff5 fs3 fc0 sc0 ls0 ws0">腦海中有了這些內容作為基礎,我們將回到之前郵件地址匹配的那個正則表示式:</div><div class="t m0 x11 hc y7c3 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[59]:<span class="_ _2a"> </span><span class="fc0">email<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>re<span class="fc6">.</span>compile(<span class="fcc">r<span class="ff12">'</span>\w+@\w+\.[a-z]<span class="ff11 fc10">{3}</span><span class="ff12">'</span></span>)</span></div><div class="t m0 x4 h4 y7c4 ff5 fs3 fc0 sc0 ls0 ws0">我們現在能夠理解這意味著什麼:我們需要一個或者多個數字或字母(<span class="ffa">\w+</span>)後跟<span class="_ _3"> </span><span class="ff7">at<span class="_ _3"> </span></span>符號</div><div class="t m0 x30 h4 y7c5 ff5 fs3 fc0 sc0 ls0 ws0">(<span class="ffa">@</span>)<span class="_ _33"></span>,<span class="_ _2"></span>然後接一個或者多個數字或字母<span class="_ _4"></span>(<span class="ffa">\w+</span>)<span class="_ _33"></span>,<span class="_ _2"></span>跟著一個句號(<span class="ffa">\.</span>,<span class="_ _2"></span>注意這裡需要一個反斜槓轉</div><div class="t m0 x5 h4 y7c6 ff5 fs3 fc0 sc0 ls0 ws0">義)<span class="_ _33"></span>,最後跟恰好三個小寫字母(<span class="ffa">[a-z]{3}</span>)<span class="_ _33"></span>。</div><div class="t m0 x4 h4 y7c7 ff5 fs3 fc0 sc0 ls0 ws0">如果我們要修改這個正則表達式使得<span class="_ _3"> </span><span class="ff7">Obama<span class="_ _3"> </span></span>的郵件地址能夠得到匹配,我們可以使用方</div><div class="t m0 x5 h4 y7c8 ff5 fs3 fc0 sc0 ls0 ws0">括號:</div><div class="t m0 x11 hc y7c9 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[60]:<span class="_ _2a"> </span><span class="fc0">email2<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>re<span class="fc6">.</span>compile(<span class="fcc">r<span class="ff12">'</span>[\w.]+@\w+\.[a-z]<span class="ff11 fc10">{3}</span><span class="ff12">'</span></span>)</span></div><div class="t m0 x31 hc y7ca ffa fs3 fc0 sc0 ls0 ws0">email2<span class="fc6">.</span>findall(<span class="ff12 fcc">'<span class="ffa">[email protected]</span>'</span>)</div><div class="t m0 x11 hc y7cb ffa fs3 fcf sc0 ls0 ws0">Out[60]:<span class="_ _2a"> </span><span class="fc0">[<span class="ff12">'</span>[email protected]<span class="ff12">'</span>]</span></div><div class="t m0 x4 h4 y7cc ff5 fs3 fc0 sc0 ls0 ws0">我們將<span class="_ _3"> </span><span class="ffa">\w+<span class="_ _3"> </span></span>改為<span class="_ _3"> </span><span class="ffa">[\w.]+</span>,這樣我們就能匹配任意字母或者數字<span class="ff2">或者</span>一個句號。有了這個</div><div class="t m0 x5 h4 y7cd ff5 fs3 fc0 sc0 ls0 ws0">更為靈活的表達式,我們能夠匹配更廣範圍的郵件地址(儘管仍然不是全部)——你能找出這</div><div class="t m0 x5 h4 y7ce ff5 fs3 fc0 sc0 ls0 ws0">個正則表示式的其他不足嗎?</div><div class="t m0 x5 h4 y7cf ff2 fs3 fc0 sc0 ls0 ws0">通過小括號進行分組提取。<span class="_ _13"> </span><span class="ff5">對於複合正則表示式,比如我們的郵件匹配器,<span class="_ _8"></span>我們經常需要提取</span></div><div class="t m0 x5 h4 y7d0 ff5 fs3 fc0 sc0 ls0 ws0">它們的一部分,而不是全部匹配內容。這可以通過<span class="ff6">分組</span>(<span class="ff7">group</span>)實現:</div><div class="t m0 x11 hc y328 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[61]:<span class="_ _2a"> </span><span class="fc0">email3<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>re<span class="fc6">.</span>compile(<span class="fcc">r<span class="ff12">'</span>([\w.]+)@(\w+)\.([a-z]<span class="ff11 fc10">{3}</span>)<span class="ff12">'</span></span>)</span></div><div class="t m0 x11 hc y329 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[62]:<span class="_ _2a"> </span><span class="fc0">text<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span><span class="fcc">"To<span class="_ _2a"> </span>email<span class="_ _2a"> </span>Guido,<span class="_ _2a"> </span>try<span class="_ _2a"> </span>[email protected]<span class="_ _2a"> </span>or<span class="_ _2a"> </span>the<span class="_ _2a"> </span>older<span class="_ _2a"> </span>address<span class="_ _2a"> </span>"</span></span>\</span></div><div class="t m0 x25 hc y21a ffa fs3 fcc sc0 ls0 ws0">"[email protected]."</div><div class="t m0 x31 hc y117 ffa fs3 fc0 sc0 ls0 ws0">email3<span class="fc6">.</span>findall(text)</div><div class="t m0 x11 hc y119 ffa fs3 fcf sc0 ls0 ws0">Out[62]:<span class="_ _2a"> </span><span class="fc0">[(<span class="ff12">'</span>guido<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>python<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>org<span class="ff12">'</span>),<span class="_ _2a"> </span>(<span class="ff12">'</span>guido<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>google<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>com<span class="ff12">'</span>)]</span></div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf65" class="pf w0 h0" data-page-no="65"><div class="pc pc65 w0 h0"><img class="bi x5 ydc w1 h56" alt="" src="images/bg65.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">94<span class="_ _8a"> </span><span class="ffc">15.<span class="_ _16"> </span><span class="ff6">字串處理和正則表示式</span></span></div><div class="t m0 x4 h4 y42 ff5 fs3 fc0 sc0 ls0 ws0">正如我們所看到的這樣,這個分組恰好提取出了一個包含所有郵件各個組成部分的列表。</div><div class="t m0 x5 h4 y77 ff5 fs3 fc0 sc0 ls0 ws0">我們可以進一步地使用<span class="_ _3"> </span><span class="ffa">(?P<name>)<span class="_ _3"> </span></span>語法給提取的組<span class="ff6">命名</span>(<span class="ff7">name</span>)<span class="_ _44"></span>,在這種情況下組別被提取</div><div class="t m0 x5 h4 y78 ff5 fs3 fc0 sc0 ls0 ws0">為<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>的字典:</div><div class="t m0 x11 hc y7d1 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[63]:<span class="_ _2a"> </span><span class="fc0">email4<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>re<span class="fc6">.</span>compile(<span class="fcc">r<span class="ff12">'</span>(?P<user>[\w.]+)@(?P<domain>\w+)<span class="ff12">'</span></span>\</span></div><div class="t m0 x44 hc y7d2 ff12 fs3 fcc sc0 ls0 ws0">'<span class="ffa">.(?P<suffix>[a-z]<span class="ff11 fc10">{3}</span>)</span>'<span class="ffa fc0">)</span></div><div class="t m0 x31 hc y7d3 ffa fs3 fc0 sc0 ls0 ws0">match<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>email4<span class="fc6">.</span>match(<span class="ff12 fcc">'<span class="ffa">[email protected]</span>'</span>)</div><div class="t m0 x31 hc y7d4 ffa fs3 fc0 sc0 ls0 ws0">match<span class="fc6">.</span>groupdict()</div><div class="t m0 x11 hc y7d5 ffa fs3 fcf sc0 ls0 ws0">Out[63]:<span class="_ _2a"> </span><span class="fc0">{<span class="ff12">'</span>domain<span class="ff12">'</span>:<span class="_ _2a"> </span><span class="ff12">'</span>python<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>suffix<span class="ff12">'</span>:<span class="_ _2a"> </span><span class="ff12">'</span>org<span class="ff12">'</span>,<span class="_ _2a"> </span><span class="ff12">'</span>user<span class="ff12">'</span>:<span class="_ _2a"> </span><span class="ff12">'</span>guido<span class="ff12">'</span>}</span></div><div class="t m0 x4 h4 y7d6 ff5 fs3 fc0 sc0 ls0 ws0">結合這些想法<span class="_ _8"></span>(以及我們未在這裡介紹的一些強大的正則表示式語法)<span class="_ _33"></span>,<span class="_ _2"></span>你可以靈活快速地</div><div class="t m0 x5 h4 y7d7 ff5 fs3 fc0 sc0 ls0 ws0">從<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>中的字串中提取資訊。</div><div class="t m0 x5 ha y7d8 ff1 fs2 fc0 sc0 ls0 ws0">15.3.3<span class="_ _3c"> </span><span class="ff2">關於正則表示式的更多資源</span></div><div class="t m0 x4 h4 y7d9 ff5 fs3 fc0 sc0 ls0 ws0">之前我們對於正則表示式的討論僅僅只是一個概覽,對於這個龐大的主題來說還遠遠不夠。</div><div class="t m0 x5 h4 y7da ff5 fs3 fc0 sc0 ls0 ws0">如果你對此有興趣並且想知道更多的內容,我推薦如下的資源:</div><div class="t m0 x4 h4 y7db ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff5 fc2">《<span class="ff7">Python<span class="_ _9"> </span><span class="ffa">re<span class="_ _3"> </span></span></span>函式庫函式文件》<span class="fc0">:我發現我每次需要使用正則表達式的時候都會忘記如何使</span></span></div><div class="t m0 xd h4 y7dc ff5 fs3 fc0 sc0 ls0 ws0">用它們。既然我已經對這些內容有一些基本的了解,我覺得這個文件對我來說非常有價</div><div class="t m0 xd h4 y7dd ff5 fs3 fc0 sc0 ls0 ws0">值。通過這個文件我可以迅速回憶起來正則表示式中一個特定的符號或者序列的意義。</div><div class="t m0 x4 h4 y7de ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff5 fc2">《<span class="ff7">Python<span class="_ _72"> </span></span>官<span class="_ _4a"></span>方<span class="_ _4a"></span>正則<span class="_ _4a"></span>表<span class="_ _4a"></span>達<span class="_ _4a"></span>式入<span class="_ _4a"></span>門<span class="_ _4a"></span>手冊》<span class="fc0">:<span class="_ _4a"></span>這<span class="_ _4a"></span>個<span class="_ _4a"></span>鏈接<span class="_ _4a"></span>提<span class="_ _4a"></span>供了<span class="_ _4a"></span>一<span class="_ _4a"></span>個<span class="_ _4a"></span>更加<span class="_ _4a"></span>直<span class="_ _4a"></span>白的<span class="_ _4a"></span>方<span class="_ _4a"></span>法<span class="_ _4a"></span>來介</span></span></div><div class="t m0 xd h4 y7df ff5 fs3 fc0 sc0 ls0 ws0">紹<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>中的正則表示式。</div><div class="t m0 x4 h7 y7e0 ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff8 fc2">Mastering<span class="_ _3"> </span>R<span class="_ _8"></span>e<span class="_ _8"></span>gular<span class="_ _3"> </span>Expr<span class="_ _8"></span>essions</span></div><div class="t m0 x3b hb y7e1 ffe fs4 fc1 sc0 ls0 ws0">2</div><div class="t m0 x72 h4 y7e0 ff5 fs3 fc0 sc0 ls0 ws0">:這本書<span class="_ _9"> </span><span class="ff15">500<span class="_ _3"> </span></span>餘頁,完整詳細地介紹了正則表示式的內</div><div class="t m0 xd h4 y7e2 ff5 fs3 fc0 sc0 ls0 ws0">容。如果你需要一個完整的解決方案,這本書是你的不二之選。</div><div class="t m0 x4 h4 y7e3 ff5 fs3 fc0 sc0 ls0 ws0">對於更大範圍內字串操作和實際使用正則表示式的一些例子,可以看第<span class="_ _3"> </span><span class="ff7 fc1">16.2<span class="_ _9"> </span></span>節<span class="_ _3"> </span><span class="ff7 fc1">P<span class="_ _8"></span>andas<span class="ff5">:</span></span></div><div class="t m0 x5 h4 y7e4 ff5 fs3 fc1 sc0 ls0 ws0">標籤化的面向行資料儲存(Labeled Column-oriented Data)<span class="fc0">。<span class="_ _40"></span>在這一節中我們利用<span class="_ _34"> </span><span class="ff7">Pandas<span class="_ _36"> </span></span>函式庫,<span class="_ _40"></span>你會看到如何應用這些表示式在<span class="ff6">表</span></span></div><div class="t m0 x5 h4 y7e5 ff6 fs3 fc0 sc0 ls0 ws0">格<span class="ff5">(<span class="ff7">table</span>)之間處理字串資料。</span></div><div class="t m0 x12 hd yf9 fff fs5 fc0 sc0 ls0 ws0">2</div><div class="t m0 xa he y40 ff5 fs6 fc0 sc0 ls0 ws0">譯者注:中文版《精通正則表示式(第三版)<span class="_ _44"></span>》已由電子工業出版社於<span class="_ _36"> </span><span class="ff10">2017<span class="_ _34"> </span></span>年出版。</div><a class="l" href="https://docs.python.org/3/library/re.html"><div class="d m1" style="border-style:none;position:absolute;left:107.758000px;bottom:460.621000px;width:130.486000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="https://docs.python.org/3/howto/regex.html"><div class="d m1" style="border-style:none;position:absolute;left:107.758000px;bottom:411.844000px;width:191.229000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="http://shop.oreilly.com/product/9780596528126.do"><div class="d m1" style="border-style:none;position:absolute;left:107.758000px;bottom:380.316000px;width:146.609000px;height:9.636000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf65" data-dest-detail='[101,"XYZ",88.59,85.15,null]'><div class="d m1" style="border-style:none;position:absolute;left:254.781000px;bottom:386.396000px;width:4.234000px;height:5.136000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf68" data-dest-detail='[104,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:447.689000px;bottom:337.964000px;width:19.394000px;height:6.870000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf68" data-dest-detail='[104,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:485.370000px;bottom:334.853000px;width:45.363000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf68" data-dest-detail='[104,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:318.594000px;width:120.000000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf66" class="pf w0 h0" data-page-no="66"><div class="pc pc66 w0 h0"><img class="bi x5 y7e6 w1 h57" alt="" src="images/bg66.png"/><div class="t m0 x73 h1 y3 ff2 fs0 fc0 sc0 ls0 ws0">第<span class="_ _0"> </span><span class="ff1">16<span class="_ _0"> </span></span>章<span class="_ _32"> </span>資料科學工具概覽</div><div class="t m0 x4 h4 ydd ff5 fs3 fc0 sc0 ls0 ws0">如果你想從這裡開始,進一步使用<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>進行科學計算或進行資料科學研究,那麼本章</div><div class="t m0 x5 h4 yde ff5 fs3 fc0 sc0 ls0 ws0">介紹的幾個套件可以讓你的工作變得更加輕鬆。本章將介紹其中幾個比較重要的工具,併為</div><div class="t m0 x5 h4 ydf ff5 fs3 fc0 sc0 ls0 ws0">你簡單介紹這些工具適用哪些類別型的問題。如果你正在使用在本書開頭提到的<span class="_ _a"> </span><span class="ff7">Anaconda<span class="_ _a"> </span></span>或</div><div class="t m0 x5 h4 ye1 ff5 fs3 fc0 sc0 ls0 ws0">者</div><div class="t m0 x26 h7 ye2 ff7 fs3 fc0 sc0 ls0 ws0">Miniconda</div><div class="t m0 x74 h4 ye1 ff5 fs3 fc0 sc0 ls0 ws0">環境的話,你可以使用下面這個命令安裝本章中有關的工具套件:</div><div class="t m0 x11 hc y7e7 ffa fs3 fc0 sc0 ls0 ws0">$<span class="_ _2a"> </span>conda<span class="_ _2a"> </span>install<span class="_ _2a"> </span>numpy<span class="_ _2a"> </span>scipy<span class="_ _2a"> </span>pandas<span class="_ _2a"> </span>matplotlib<span class="_ _2a"> </span>scikit-learn</div><div class="t m0 x4 h4 y7e8 ff5 fs3 fc0 sc0 ls0 ws0">我們來簡單看看這些工具。</div><div class="t m0 x5 h8 y7e9 ff1 fs1 fc0 sc0 ls0 ws0">16.1<span class="_ _39"> </span>NumPy<span class="ff2">:</span>Python<span class="_ _10"> </span><span class="ff2">數值計算</span></div><div class="t m0 x4 h4 y7ea ff7 fs3 fc0 sc0 ls0 ws0">NumPy<span class="_ _9"> </span><span class="ff5">對<span class="_ _3"> </span></span>Python<span class="_ _3"> </span><span class="ff5">中稠密高維的陣列進行儲存和操作提供了一個高效的途徑。</span>NumPy<span class="_ _3"> </span><span class="ff5">的</span></div><div class="t m0 x5 h4 y7eb ff5 fs3 fc0 sc0 ls0 ws0">重要特性有:</div><div class="t m0 x4 h4 y7ec ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff5">它提供了<span class="_ _a"> </span><span class="ffa">ndarray<span class="_ _3"> </span></span>結構體,使得能夠高效地儲存和操作向量、矩陣和更高維度的資料</span></div><div class="t m0 xd h4 y7ed ff5 fs3 fc0 sc0 ls0 ws0">集。</div><div class="t m0 x4 h4 y7ee ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff5">它提供了一個易讀並且高效的語法來對資料進行處理,支援從簡單的按元素的算術運算(element-wise arithmetic)</span></div><div class="t m0 xd h4 y7ef ff5 fs3 fc0 sc0 ls0 ws0">到相對複雜的線性代數運算。</div><div class="t m0 x4 h4 y7f0 ff5 fs3 fc0 sc0 ls0 ws0">在最簡單的情況中,<span class="_ _8"></span><span class="ff7">NumPy<span class="_ _9"> </span><span class="ff5">的陣列與<span class="_ _9"> </span></span>Python<span class="_ _9"> </span><span class="ff5">的列表十分相像。<span class="_ _8"></span>舉例來說,<span class="_ _8"></span>這裡有一個包</span></span></div><div class="t m0 x5 h4 y7f1 ff5 fs3 fc0 sc0 ls0 ws0">含範圍從<span class="_ _9"> </span><span class="ff15">1<span class="_ _3"> </span></span>到<span class="_ _9"> </span><span class="ff15">9<span class="_ _9"> </span></span>的數字的陣列(把這個例子與<span class="_ _3"> </span><span class="ff7">Python<span class="_ _9"> </span></span>中內建的<span class="_ _3"> </span><span class="ffa">range()<span class="_ _9"> </span></span>函式進行比較)<span class="_ _33"></span>:</div><div class="t m0 x11 hc y7f2 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[1]:<span class="_ _2a"> </span><span class="ff11 fc4">import<span class="_ _2a"> </span><span class="fc5">numpy<span class="_ _2a"> </span></span>as<span class="_ _2a"> </span><span class="fc5">np</span></span></div><div class="t m0 x24 hc y7f3 ffa fs3 fc0 sc0 ls0 ws0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>np<span class="fc6">.</span>arange(<span class="fc6">1</span>,<span class="_ _2a"> </span><span class="fc6">10</span>)</div><div class="t m0 x24 hc y7f4 ffa fs3 fc0 sc0 ls0 ws0">x</div><div class="t m0 x11 hc y7f5 ffa fs3 fcf sc0 ls0 ws0">Out[1]:<span class="_ _2a"> </span><span class="fc0">array([1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>3,<span class="_ _2a"> </span>4,<span class="_ _2a"> </span>5,<span class="_ _2a"> </span>6,<span class="_ _2a"> </span>7,<span class="_ _2a"> </span>8,<span class="_ _2a"> </span>9])</span></div><div class="t m0 x4 h4 y7f6 ff7 fs3 fc0 sc0 ls0 ws0">NumPy<span class="_ _3"> </span><span class="ff5">的陣列既提供了高效儲存資料的方法,也提供了一些高效的函式來對這些資料按</span></div><div class="t m0 x5 h4 y7f7 ff5 fs3 fc0 sc0 ls0 ws0">元素進行運算。比如,對陣列中每個元素進行平方,我們可以直接對陣列進行<span class="_ _9"> </span><span class="ffa">**<span class="_ _3"> </span></span>操作:</div><div class="t m0 x11 hc y7f8 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[2]:<span class="_ _2a"> </span><span class="fc0">x<span class="_ _2a"> </span><span class="fc6">**<span class="_ _2a"> </span>2</span></span></div><div class="t m0 x11 hc y7f9 ffa fs3 fcf sc0 ls0 ws0">Out[2]:<span class="_ _2a"> </span><span class="fc0">array([<span class="_ _2a"> </span>1,<span class="_ _27"> </span>4,<span class="_ _27"> </span>9,<span class="_ _2a"> </span>16,<span class="_ _2a"> </span>25,<span class="_ _2a"> </span>36,<span class="_ _2a"> </span>49,<span class="_ _2a"> </span>64,<span class="_ _2a"> </span>81])</span></div><div class="t m0 x4 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">把上面的程式碼與有些複雜的<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>風格的列表推導式(兩者產生相同的結果)進行比較:</div><div class="t m0 x2f h7 y20 ff7 fs3 fc0 sc0 ls0 ws0">95</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf67" class="pf w0 h0" data-page-no="67"><div class="pc pc67 w0 h0"><img class="bi x5 y9b w1 h58" alt="" src="images/bg67.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">96<span class="_ _92"> </span><span class="ffc">16.<span class="_ _16"> </span><span class="ff6">資料科學工具概覽</span></span></div><div class="t m0 x11 hc yfa ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[3]:<span class="_ _2a"> </span><span class="fc0">[val<span class="_ _2a"> </span><span class="fc6">**<span class="_ _2a"> </span>2<span class="_ _2a"> </span><span class="ff11 fc4">for<span class="_ _2a"> </span></span></span>val<span class="_ _2a"> </span><span class="ff11 fcb">in<span class="_ _2a"> </span></span><span class="fc4">range</span>(<span class="fc6">1</span>,<span class="_ _2a"> </span><span class="fc6">10</span>)]</span></div><div class="t m0 x11 hc yfc ffa fs3 fcf sc0 ls0 ws0">Out[3]:<span class="_ _2a"> </span><span class="fc0">[1,<span class="_ _2a"> </span>4,<span class="_ _2a"> </span>9,<span class="_ _2a"> </span>16,<span class="_ _2a"> </span>25,<span class="_ _2a"> </span>36,<span class="_ _2a"> </span>49,<span class="_ _2a"> </span>64,<span class="_ _2a"> </span>81]</span></div><div class="t m0 x4 h4 y7fa ff5 fs3 fc0 sc0 ls0 ws0">不像<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>的列表(受限於一維)<span class="_ _5f"></span>,<span class="ff7">NumPy<span class="_ _3"> </span></span>的數組可以是多維的。比如,這裡我們將之</div><div class="t m0 x5 h4 y7fb ff5 fs3 fc0 sc0 ls0 ws0">前的<span class="_ _9"> </span><span class="ffa">x<span class="_ _3"> </span></span>陣列調整為一個<span class="_ _9"> </span><span class="ff15">3<span class="_ _60"> </span><span class="ff16">×<span class="_ _60"> </span></span>3<span class="_ _9"> </span></span>維度的陣列:</div><div class="t m0 x11 hc y7fc ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[4]:<span class="_ _2a"> </span><span class="fc0">M<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>x<span class="fc6">.</span>reshape((<span class="fc6">3</span>,<span class="_ _2a"> </span><span class="fc6">3</span>))</span></div><div class="t m0 x24 hc y7fd ffa fs3 fc0 sc0 ls0 ws0">M</div><div class="t m0 x11 hc y7fe ffa fs3 fcf sc0 ls0 ws0">Out[4]:<span class="_ _2a"> </span><span class="fc0">array([[1,<span class="_ _2a"> </span>2,<span class="_ _2a"> </span>3],</span></div><div class="t m0 x6d hc y7ff ffa fs3 fc0 sc0 ls0 ws0">[4,<span class="_ _2a"> </span>5,<span class="_ _2a"> </span>6],</div><div class="t m0 x6d hc y800 ffa fs3 fc0 sc0 ls0 ws0">[7,<span class="_ _2a"> </span>8,<span class="_ _2a"> </span>9]])</div><div class="t m0 x4 h4 y801 ff5 fs3 fc0 sc0 ls0 ws0">一個二維數組是矩陣的一種表示形式,並且<span class="_ _3"> </span><span class="ff7">NumPy<span class="_ _3"> </span></span>可以高效地進行典型的矩陣運算。比</div><div class="t m0 x5 h4 y802 ff5 fs3 fc0 sc0 ls0 ws0">如,你可以通過<span class="_ _9"> </span><span class="ffa">.T<span class="_ _3"> </span></span>計算矩陣的<span class="ff6">轉置矩陣</span>(<span class="ff7">transpose<span class="_ _3"> </span>matrix</span>)<span class="_ _33"></span>:</div><div class="t m0 x11 hc y803 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[5]:<span class="_ _2a"> </span><span class="fc0">M<span class="fc6">.</span>T</span></div><div class="t m0 x11 hc y804 ffa fs3 fcf sc0 ls0 ws0">Out[5]:<span class="_ _2a"> </span><span class="fc0">array([[1,<span class="_ _2a"> </span>4,<span class="_ _2a"> </span>7],</span></div><div class="t m0 x6d hc y805 ffa fs3 fc0 sc0 ls0 ws0">[2,<span class="_ _2a"> </span>5,<span class="_ _2a"> </span>8],</div><div class="t m0 x6d hc y806 ffa fs3 fc0 sc0 ls0 ws0">[3,<span class="_ _2a"> </span>6,<span class="_ _2a"> </span>9]])</div><div class="t m0 x4 h4 y807 ff5 fs3 fc0 sc0 ls0 ws0">或者通過<span class="_ _9"> </span><span class="ffa">np.dot<span class="_ _3"> </span></span>進行矩陣乘法:</div><div class="t m0 x11 hc y808 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[6]:<span class="_ _2a"> </span><span class="fc0">np<span class="fc6">.</span>dot(M,<span class="_ _2a"> </span>[<span class="fc6">5</span>,<span class="_ _2a"> </span><span class="fc6">6</span>,<span class="_ _2a"> </span><span class="fc6">7</span>])</span></div><div class="t m0 x11 hc y809 ffa fs3 fcf sc0 ls0 ws0">Out[6]:<span class="_ _2a"> </span><span class="fc0">array([<span class="_ _2a"> </span>38,<span class="_ _27"> </span>92,<span class="_ _2a"> </span>146])</span></div><div class="t m0 x4 h4 y80a ff5 fs3 fc0 sc0 ls0 ws0">並且你可以進行更複雜的操作,比如<span class="ff6">特徵值分解</span>(<span class="ff7">eigen<span class="_ _4"></span>v<span class="_ _8"></span>alue<span class="_ _9"> </span>decomposition<span class="ff5">)</span></span></div><div class="t m0 x75 hb y80b ffe fs4 fc1 sc0 ls0 ws0">1</div><div class="t m0 x5c h4 y80a ff5 fs3 fc0 sc0 ls0 ws0">:</div><div class="t m0 x11 hc y80c ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[7]:<span class="_ _2a"> </span><span class="fc0">np<span class="fc6">.</span>linalg<span class="fc6">.</span>eigvals(M)</span></div><div class="t m0 x11 hc y80d ffa fs3 fcf sc0 ls0 ws0">Out[7]:<span class="_ _2a"> </span><span class="fc0">array([<span class="_ _27"> </span>1.61168440e+01,<span class="_ _27"> </span>-1.11684397e+00,<span class="_ _27"> </span>-1.30367773e-15])</span></div><div class="t m0 x4 h4 y80e ff5 fs3 fc0 sc0 ls0 ws0">這種線性代數計算是現代資料分析的基礎,這在機器學習和資料探勘領域體現得尤為明顯。</div><div class="t m0 x4 h4 y80f ff5 fs3 fc0 sc0 ls0 ws0">想了解關於<span class="_ _9"> </span><span class="ff7">NumPy<span class="_ _3"> </span></span>的更多知識,查閱第<span class="_ _9"> </span><span class="ff7 fc1">17<span class="_ _9"> </span></span>章<span class="_ _3"> </span><span class="fc1">更多學習資源</span>。</div><div class="t m0 x12 hd yb9 fff fs5 fc0 sc0 ls0 ws0">1</div><div class="t m0 xa he yba ff5 fs6 fc0 sc0 ls0 ws0">譯者注:對<span class="_ _34"> </span><span class="ff1b">N<span class="_ _34"> </span><span class="ff1c">×<span class="_ _4b"> </span></span>N<span class="_ _a"> </span></span>維矩陣<span class="_ _34"> </span><span class="ff1d">A<span class="_ _34"> </span></span>和<span class="_ _34"> </span><span class="ff1b">N<span class="_ _a"> </span></span>維非零向量<span class="_ _34"> </span><span class="ff1d">v</span>,若滿足<span class="_ _34"> </span><span class="ff1d">Av<span class="_ _36"> </span><span class="ff1e">=<span class="_ _36"> </span><span class="ff1b">λ</span></span>v</span>,其中<span class="_ _34"> </span><span class="ff1b">λ<span class="_ _36"> </span></span>為一標量,則稱<span class="_ _34"> </span><span class="ff1b">λ<span class="_ _34"> </span></span>為<span class="_ _34"> </span><span class="ff1d">v<span class="_ _34"> </span></span>對應的</div><div class="t m0 x5 he y40 ff5 fs6 fc0 sc0 ls0 ws0">特徵值。即特徵向量被施以線性變換<span class="_ _34"> </span><span class="ff1d">A<span class="_ _36"> </span></span>只會使向量伸長或縮短而其方向不被改變。</div><a class="l" href="#pf67" data-dest-detail='[103,"XYZ",88.59,98.3,null]'><div class="d m1" style="border-style:none;position:absolute;left:465.091000px;bottom:237.906000px;width:4.234000px;height:5.136000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf6c" data-dest-detail='[108,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:293.969000px;bottom:108.890000px;width:10.909000px;height:6.869000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf6c" data-dest-detail='[108,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:323.060000px;bottom:105.779000px;width:65.455000px;height:12.528000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf68" class="pf w0 h0" data-page-no="68"><div class="pc pc68 w0 h0"><img class="bi x5 y810 w1 h59" alt="" src="images/bg68.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">16.2.<span class="_ _16"> </span>P<span class="_ _2"></span>AND<span class="_ _4"></span>AS<span class="ff6">:標籤化的面向行資料儲存(Labeled Column-oriented Data)<span class="_ _93"> </span><span class="ff7">97</span></span></div><div class="t m0 x5 h8 ybb ff1 fs1 fc0 sc0 ls0 ws0">16.2<span class="_ _39"> </span>P<span class="_ _8"></span>andas<span class="ff2">:標籤化的面向行資料儲存(Labeled Column-oriented Data)</span></div><div class="t m0 x4 h4 ybc ff7 fs3 fc0 sc0 ls0 ws0">P<span class="_ _4"></span>andas<span class="_ _34"> </span><span class="ff5">是一個比<span class="_ _34"> </span></span>NumPy<span class="_ _36"> </span><span class="ff5">新很多的函式庫,<span class="_ _40"></span>並且實際上是基於後者建立的。<span class="_ _42"></span><span class="ff7">P<span class="_ _8"></span>andas<span class="_ _34"> </span><span class="ff5">提供了一個</span></span></span></div><div class="t m0 x5 h4 ybd ff5 fs3 fc0 sc0 ls0 ws0">標籤化的介面來訪問多維資料,並且以<span class="ff6">資料幀</span>(<span class="ff7">data<span class="_ _3"> </span>frame</span>)物件的形式呈現。如果你是<span class="_ _3"> </span><span class="ff7">R<span class="_ _9"> </span></span>語</div><div class="t m0 x5 h4 ybe ff5 fs3 fc0 sc0 ls0 ws0">言或者相關語言的使用者,你對此一定會感到很熟悉。<span class="ff7">P<span class="_ _4"></span>andas<span class="_ _9"> </span><span class="ff5">中的資料幀像下面這段程式碼一樣:</span></span></div><div class="t m0 x11 hc y811 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[8]:<span class="_ _2a"> </span><span class="ff11 fc4">import<span class="_ _2a"> </span><span class="fc5">pandas<span class="_ _2a"> </span></span>as<span class="_ _2a"> </span><span class="fc5">pd</span></span></div><div class="t m0 x24 hc y812 ffa fs3 fc0 sc0 ls0 ws0">df<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>pd<span class="fc6">.</span>DataFrame({<span class="ff12 fcc">'<span class="ffa">label</span>'</span>:<span class="_ _2a"> </span>[<span class="ff12 fcc">'<span class="ffa">A</span>'</span>,<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">B</span>'</span>,<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">C</span>'</span>,<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">A</span>'</span>,<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">B</span>'</span>,<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">C</span>'</span>],</div><div class="t m0 x46 hc y813 ff12 fs3 fcc sc0 ls0 ws0">'<span class="ffa">value</span>'<span class="ffa fc0">:<span class="_ _2a"> </span>[<span class="fc6">1</span>,<span class="_ _2a"> </span><span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">3</span>,<span class="_ _2a"> </span><span class="fc6">4</span>,<span class="_ _2a"> </span><span class="fc6">5</span>,<span class="_ _2a"> </span><span class="fc6">6</span>]})</span></div><div class="t m0 x24 hc y814 ffa fs3 fc0 sc0 ls0 ws0">df</div><div class="t m0 x11 hc y815 ffa fs3 fcf sc0 ls0 ws0">Out[8]:<span class="_ _49"> </span><span class="fc0">label<span class="_ _27"> </span>value</span></div><div class="t m0 x24 hc y816 ffa fs3 fc0 sc0 ls0 ws0">0<span class="_ _4f"> </span>A<span class="_ _48"> </span>1</div><div class="t m0 x24 hc y817 ffa fs3 fc0 sc0 ls0 ws0">1<span class="_ _4f"> </span>B<span class="_ _48"> </span>2</div><div class="t m0 x24 hc y818 ffa fs3 fc0 sc0 ls0 ws0">2<span class="_ _4f"> </span>C<span class="_ _48"> </span>3</div><div class="t m0 x24 hc y819 ffa fs3 fc0 sc0 ls0 ws0">3<span class="_ _4f"> </span>A</div><div class="t m0 x3d hc y81a ffa fs3 fc0 sc0 ls0 ws0">4</div><div class="t m0 x24 hc y81b ffa fs3 fc0 sc0 ls0 ws0">4<span class="_ _4f"> </span>B<span class="_ _48"> </span>5</div><div class="t m0 x24 hc y81c ffa fs3 fc0 sc0 ls0 ws0">5<span class="_ _4f"> </span>C<span class="_ _48"> </span>6</div><div class="t m0 x4 h4 y87 ff7 fs3 fc0 sc0 ls0 ws0">P<span class="_ _4"></span>andas<span class="_ _9"> </span><span class="ff5">的介面允許你直接以名字選中列並進行操作:</span></div><div class="t m0 x11 hc y81d ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[9]:<span class="_ _2a"> </span><span class="fc0">df[<span class="ff12 fcc">'<span class="ffa">label</span>'</span>]</span></div><div class="t m0 x11 hc y81e ffa fs3 fcf sc0 ls0 ws0">Out[9]:<span class="_ _2a"> </span><span class="fc0">0<span class="_ _52"> </span>A</span></div><div class="t m0 x24 hc y81f ffa fs3 fc0 sc0 ls0 ws0">1<span class="_ _52"> </span>B</div><div class="t m0 x24 hc y820 ffa fs3 fc0 sc0 ls0 ws0">2<span class="_ _52"> </span>C</div><div class="t m0 x24 hc y821 ffa fs3 fc0 sc0 ls0 ws0">3<span class="_ _52"> </span>A</div><div class="t m0 x24 hc y822 ffa fs3 fc0 sc0 ls0 ws0">4<span class="_ _52"> </span>B</div><div class="t m0 x24 hc y823 ffa fs3 fc0 sc0 ls0 ws0">5<span class="_ _52"> </span>C</div><div class="t m0 x24 hc y824 ffa fs3 fc0 sc0 ls0 ws0">Name:<span class="_ _2a"> </span>label,<span class="_ _2a"> </span>dtype:<span class="_ _2a"> </span>object</div><div class="t m0 x4 h4 y825 ff5 fs3 fc0 sc0 ls0 ws0">在字串清單之間套用字串處理函式:</div><div class="t m0 x11 hc y826 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[10]:<span class="_ _2a"> </span><span class="fc0">df[<span class="ff12 fcc">'<span class="ffa">label</span>'</span>]<span class="fc6">.</span>str<span class="fc6">.</span>lower()</span></div><div class="t m0 x11 hc y827 ffa fs3 fcf sc0 ls0 ws0">Out[10]:<span class="_ _2a"> </span><span class="fc0">0<span class="_ _52"> </span>a</span></div><div class="t m0 x31 hc y828 ffa fs3 fc0 sc0 ls0 ws0">1<span class="_ _52"> </span>b</div><div class="t m0 x31 hc y829 ffa fs3 fc0 sc0 ls0 ws0">2<span class="_ _52"> </span>c</div><div class="t m0 x31 hc y82a ffa fs3 fc0 sc0 ls0 ws0">3<span class="_ _52"> </span>a</div><div class="t m0 x31 hc y82b ffa fs3 fc0 sc0 ls0 ws0">4<span class="_ _52"> </span>b</div><div class="t m0 x31 hc y82c ffa fs3 fc0 sc0 ls0 ws0">5<span class="_ _52"> </span>c</div><div class="t m0 x31 hc y82d ffa fs3 fc0 sc0 ls0 ws0">Name:<span class="_ _2a"> </span>label,<span class="_ _2a"> </span>dtype:<span class="_ _2a"> </span>object</div><div class="t m0 x4 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">在數值清單之間套用<span class="ff6">聚集</span>(<span class="ff7">aggregate</span>)函式:</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf69" class="pf w0 h0" data-page-no="69"><div class="pc pc69 w0 h0"><img class="bi x5 y40 w1 h11" alt="" src="images/bg69.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">98<span class="_ _92"> </span><span class="ffc">16.<span class="_ _16"> </span><span class="ff6">資料科學工具概覽</span></span></div><div class="t m0 x11 hc yfa ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[11]:<span class="_ _2a"> </span><span class="fc0">df[<span class="ff12 fcc">'<span class="ffa">value</span>'</span>]<span class="fc6">.</span>sum()</span></div><div class="t m0 x11 hc yfc ffa fs3 fcf sc0 ls0 ws0">Out[11]:<span class="_ _2a"> </span><span class="fc0">21</span></div><div class="t m0 x4 h4 y82e ff5 fs3 fc0 sc0 ls0 ws0">並且可能最重要的一點是你可以直接應用高效的資料庫風格的串接和分組操作:</div><div class="t m0 x11 hc y82f ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[12]:<span class="_ _2a"> </span><span class="fc0">df<span class="fc6">.</span>groupby(<span class="ff12 fcc">'<span class="ffa">label</span>'</span>)<span class="fc6">.</span>sum()</span></div><div class="t m0 x11 hc y830 ffa fs3 fcf sc0 ls0 ws0">Out[12]:<span class="_ _51"> </span><span class="fc0">value</span></div><div class="t m0 x31 hc y831 ffa fs3 fc0 sc0 ls0 ws0">label</div><div class="t m0 x31 hc y832 ffa fs3 fc0 sc0 ls0 ws0">A<span class="_ _94"> </span>5</div><div class="t m0 x31 hc y833 ffa fs3 fc0 sc0 ls0 ws0">B<span class="_ _94"> </span>7</div><div class="t m0 x31 hc y834 ffa fs3 fc0 sc0 ls0 ws0">C<span class="_ _94"> </span>9</div><div class="t m0 x4 h4 y835 ff5 fs3 fc0 sc0 ls0 ws0">在這裡,<span class="_ _4a"></span>我們用<span class="_ _4a"></span>一行代<span class="_ _4a"></span>碼就計<span class="_ _4a"></span>算出了<span class="_ _4a"></span>相同標<span class="_ _4a"></span>籤列的<span class="_ _4a"></span>數值的和。<span class="_ _4a"></span>如果使<span class="_ _4a"></span>用<span class="_ _72"> </span><span class="ff7">NumPy<span class="_ _72"> </span></span>和核</div><div class="t m0 x5 h4 y836 ff5 fs3 fc0 sc0 ls0 ws0">心<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>程式碼,這樣的操作更加繁瑣(也更加低效)<span class="_ _33"></span>。</div><div class="t m0 x4 h4 y837 ff5 fs3 fc0 sc0 ls0 ws0">想了解關於<span class="_ _9"> </span><span class="ff7">Pandas<span class="_ _9"> </span></span>的更多知識,查閱第<span class="_ _9"> </span><span class="ff7 fc1">17<span class="_ _3"> </span></span>章<span class="_ _9"> </span><span class="fc1">更多學習資源</span>。</div><div class="t m0 x5 h8 y838 ff1 fs1 fc0 sc0 ls0 ws0">16.3<span class="_ _39"> </span>Matplotlib<span class="ff2">:</span>MatLab<span class="_ _10"> </span><span class="ff2">風格的科學視覺化</span></div><div class="t m0 x4 h4 y839 ff7 fs3 fc0 sc0 ls0 ws0">Matplotlib<span class="_ _3"> </span><span class="ff5">目前是<span class="_ _3"> </span></span>Python<span class="_ _3"> </span><span class="ff5">中最受歡迎的科學可視化函式庫。盡管它的支持者都會認為它的接</span></div><div class="t m0 x5 h4 y83a ff5 fs3 fc0 sc0 ls0 ws0">口有的時候過於繁瑣,但是它對於繪製各種型別的圖表來說仍然是一個強有力的工具套件。</div><div class="t m0 x4 h4 y83b ff5 fs3 fc0 sc0 ls0 ws0">要使用<span class="_ _a"> </span><span class="ff7">Matplotlib</span>,我們從開啟記事本模式(對於使用<span class="_ _a"> </span><span class="ff7">Jupyter<span class="_ _3"> </span>Notebook<span class="_ _3"> </span></span>的用戶來說)<span class="_ _45"></span>,</div><div class="t m0 x5 h4 y83c ff5 fs3 fc0 sc0 ls0 ws0">然後匯入函式庫並重命名為<span class="_ _9"> </span><span class="ffa">plt</span>。</div><div class="t m0 x11 h6 y83d ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[13]:<span class="_ _2a"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">如果使用<span class="_ _2a"> </span></span>Jupyter<span class="_ _2a"> </span>Notebook<span class="_ _2a"> </span><span class="ff6">需要執行下面這一行</span></span></div><div class="t m0 x31 hc y83e ffa fs3 fc6 sc0 ls0 ws0">%<span class="ff11 fc4">matplotlib<span class="_ _2a"> </span></span><span class="fc0">notebook</span></div><div class="t m0 x11 hc y83f ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[14]:<span class="_ _2a"> </span><span class="ff11 fc4">import<span class="_ _2a"> </span><span class="fc5">matplotlib.pyplot<span class="_ _2a"> </span></span>as<span class="_ _2a"> </span><span class="fc5">plt</span></span></div><div class="t m0 x31 h6 y840 ffa fs3 fc0 sc0 ls0 ws0">plt<span class="fc6">.</span>style<span class="fc6">.</span>use(<span class="ff12 fcc">'<span class="ffa">ggplot</span>'</span>)<span class="_ _27"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">使用<span class="_ _2a"> </span></span>R<span class="_ _2a"> </span><span class="ff6">語言的<span class="_ _2a"> </span></span>ggplot<span class="_ _2a"> </span><span class="ff6">風格畫圖</span></span></div><div class="t m0 x4 h4 y841 ff5 fs3 fc0 sc0 ls0 ws0">現在讓我們建立一些資料(當然是以<span class="_ _9"> </span><span class="ff7">NumPy<span class="_ _3"> </span></span>陣列的形式)<span class="_ _33"></span>,並且將結果繪製出來:</div><div class="t m0 x11 h6 y117 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[15]:<span class="_ _2a"> </span><span class="fc0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>np<span class="fc6">.</span>linspace(<span class="fc6">0</span>,<span class="_ _2a"> </span><span class="fc6">10</span>)<span class="_ _27"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">從<span class="_ _2a"> </span></span>0<span class="_ _2a"> </span><span class="ff6">到<span class="_ _2a"> </span></span>10<span class="_ _2a"> </span><span class="ff6">範圍內取值</span></span></span></div><div class="t m0 x31 h6 y118 ffa fs3 fc0 sc0 ls0 ws0">y<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>np<span class="fc6">.</span>sin(x)<span class="_ _56"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">取這些值的正弦值</span></span></div><div class="t m0 x31 h6 y119 ffa fs3 fc0 sc0 ls0 ws0">plt<span class="fc6">.</span>plot(x,<span class="_ _2a"> </span>y);<span class="_ _47"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">繪製成一條曲線</span></span></div><a class="l" href="#pf6c" data-dest-detail='[108,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:291.909000px;bottom:442.629000px;width:10.909000px;height:6.870000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf6c" data-dest-detail='[108,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:321.000000px;bottom:439.518000px;width:65.454000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf6a" class="pf w0 h0" data-page-no="6a"><div class="pc pc6a w0 h0"><img class="bi x5 y1bd w1 h40" alt="" src="images/bg6a.png"/><div class="t m0 x5 h6 y41 ffc fs3 fc0 sc0 ls0 ws0">16.4.<span class="_ _16"> </span>SCIPY<span class="ff6">:</span>PYTHON<span class="_ _9"> </span><span class="ff6">科學計算<span class="_ _95"> </span><span class="ff7">99</span></span></div><div class="t m0 x4 h4 y842 ff5 fs3 fc0 sc0 ls0 ws0">如果你使用互動模式執行上述程式碼,你可以在一個互動式的視窗中移動、縮放、滾動來瀏</div><div class="t m0 x5 h4 y843 ff5 fs3 fc0 sc0 ls0 ws0">覽資料。</div><div class="t m0 x4 h4 y844 ff5 fs3 fc0 sc0 ls0 ws0">這是<span class="_ _3"> </span><span class="ff7">Matplotlib<span class="_ _a"> </span></span>影象中最簡單的一個例子。想要了解建立其他圖表的知識,請查閱<span class="_ _3"> </span><span class="ff7">Mat-</span></div><div class="t m0 x5 h4 y845 ff7 fs3 fc0 sc0 ls0 ws0">plotlib<span class="_ _9"> </span><span class="ff5">線上畫廊:<span class="ffa fc2">http://matplotlib.org/gallery.html<span class="_ _3"> </span></span>和其他在第<span class="_ _9"> </span></span><span class="fc1">17<span class="_ _3"> </span></span><span class="ff5">章<span class="_ _9"> </span><span class="fc1">更多學習資源</span>中</span></div><div class="t m0 x5 h4 y846 ff5 fs3 fc0 sc0 ls0 ws0">列出的資源。</div><div class="t m0 x5 h8 y847 ff1 fs1 fc0 sc0 ls0 ws0">16.4<span class="_ _39"> </span>SciPy<span class="ff2">:</span>Python<span class="_ _10"> </span><span class="ff2">科學計算</span></div><div class="t m0 x4 h4 y848 ff7 fs3 fc0 sc0 ls0 ws0">SciPy<span class="_ _3"> </span><span class="ff5">是基於<span class="_ _3"> </span></span>NumPy<span class="_ _3"> </span><span class="ff5">建立的一個科學計算功能的集合。最初這個函式庫是一套<span class="_ _3"> </span></span>F<span class="_ _2"></span>ortran<span class="_ _3"> </span><span class="ff5">下著</span></div><div class="t m0 x5 h4 y849 ff5 fs3 fc0 sc0 ls0 ws0">名數值計算函式庫在<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>下的封裝,並慢慢發展壯大。這個函式庫被組織成一些子模組,每個子</div><div class="t m0 x5 h4 y84a ff5 fs3 fc0 sc0 ls0 ws0">模組實現一些數值演算法。下面列出了部分對於資料科學而言重要的模組:</div><div class="t m0 x4 h4 y84b ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ffa">scipy.fftpack<span class="ff5">:<span class="ff6">快速傅立葉變換</span>(<span class="ff7">F<span class="_ _2"></span>ast<span class="_ _3"> </span>F<span class="_ _2"></span>ourier<span class="_ _9"> </span>T<span class="_ _8"></span>ransforms<span class="_ _9"> </span>(FFT)<span class="ff5">)</span></span></span></span></div><div class="t m0 x4 h4 y84c ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ffa">scipy.integrate<span class="ff5">:<span class="ff6">數值積分</span>(<span class="ff7">n<span class="_ _4"></span>umerical<span class="_ _9"> </span>integration<span class="ff5">)</span></span></span></span></div><div class="t m0 x4 h4 y84d ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ffa">scipy.interpolate<span class="ff5">:<span class="ff6">數值插值</span>(<span class="ff7">n<span class="_ _4"></span>umerical<span class="_ _9"> </span>interpolation<span class="ff5">)</span></span></span></span></div><div class="t m0 x4 h4 y84e ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ffa">scipy.linalg<span class="ff5">:線性代數方法</span></span></div><div class="t m0 x4 h4 y84f ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ffa">scipy.optimize<span class="ff5">:<span class="ff6">函式數值優化</span>(<span class="ff7">n<span class="_ _4"></span>umerical<span class="_ _9"> </span>optimization<span class="_ _3"> </span>of<span class="_ _9"> </span>functions<span class="ff5">)</span></span></span></span></div><div class="t m0 x4 h4 y850 ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ffa">scipy.sparse<span class="ff5">:<span class="ff6">稀疏矩陣儲存</span>(<span class="ff7">sparse<span class="_ _9"> </span>matrix<span class="_ _3"> </span>storage</span>)和線性代數</span></span></div><div class="t m0 x4 h4 y851 ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ffa">scipy.stats<span class="ff5">:<span class="ff6">統計分析</span>(<span class="ff7">statistical<span class="_ _9"> </span>analysis</span>)方法</span></span></div><div class="t m0 x4 h4 y852 ff5 fs3 fc0 sc0 ls0 ws0">比如,我們來看看如何在某些資料之間插值從而繪製出平滑的曲線:</div><div class="t m0 x11 hc y346 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[16]:<span class="_ _2a"> </span><span class="ff11 fc4">from<span class="_ _2a"> </span><span class="fc5">scipy<span class="_ _2a"> </span></span>import<span class="_ _2a"> </span></span><span class="fc0">interpolate</span></div><div class="t m0 x31 h6 y172 ff14 fs3 fca sc0 ls0 ws0">#<span class="_ _2a"> </span><span class="ff6">選擇<span class="_ _2a"> </span></span>0<span class="_ _2a"> </span><span class="ff6">到<span class="_ _2a"> </span></span>10<span class="_ _2a"> </span><span class="ff6">之間的<span class="_ _2a"> </span></span>8<span class="_ _2a"> </span><span class="ff6">個點</span></div><div class="t m0 x31 hc y173 ffa fs3 fc0 sc0 ls0 ws0">x<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>np<span class="fc6">.</span>linspace(<span class="fc6">0</span>,<span class="_ _2a"> </span><span class="fc6">10</span>,<span class="_ _2a"> </span><span class="fc6">8</span>)</div><div class="t m0 x31 hc y174 ffa fs3 fc0 sc0 ls0 ws0">y<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>np<span class="fc6">.</span>sin(x)</div><div class="t m0 x31 h6 y176 ff14 fs3 fca sc0 ls0 ws0">#<span class="_ _2a"> </span><span class="ff6">建立一個三次插值函式</span></div><div class="t m0 x31 hc y98 ffa fs3 fc0 sc0 ls0 ws0">func<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>interpolate<span class="fc6">.</span>interp1d(x,<span class="_ _2a"> </span>y,<span class="_ _2a"> </span>kind<span class="fc6">=<span class="ff12 fcc">'<span class="ffa">cubic</span>'</span></span>)</div><div class="t m0 x31 h6 y9a ff14 fs3 fca sc0 ls0 ws0">#<span class="_ _2a"> </span><span class="ff6">在<span class="_ _2a"> </span></span>1,000<span class="_ _2a"> </span><span class="ff6">個點的網格上進行插值運算</span></div><div class="t m0 x31 hc y40 ffa fs3 fc0 sc0 ls0 ws0">x_interp<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>np<span class="fc6">.</span>linspace(<span class="fc6">0</span>,<span class="_ _2a"> </span><span class="fc6">10</span>,<span class="_ _2a"> </span><span class="fc6">1000</span>)</div><a class="l" href="http://matplotlib.org/gallery.html"><div class="d m1" style="border-style:none;position:absolute;left:161.134000px;bottom:521.900000px;width:194.727000px;height:10.000000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf6c" data-dest-detail='[108,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:417.746000px;bottom:524.324000px;width:10.909000px;height:6.870000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf6c" data-dest-detail='[108,"XYZ",72,769.89,null]'><div class="d m1" style="border-style:none;position:absolute;left:446.864000px;bottom:521.213000px;width:65.503000px;height:12.529000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf6b" class="pf w0 h0" data-page-no="6b"><div class="pc pc6b w0 h0"><img class="bi x5 y853 w1 h5a" alt="" src="images/bg6b.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">100<span class="_ _96"> </span><span class="ffc">16.<span class="_ _16"> </span><span class="ff6">資料科學工具概覽</span></span></div><div class="t m0 x31 hc y1d6 ffa fs3 fc0 sc0 ls0 ws0">y_interp<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>func(x_interp)</div><div class="t m0 x31 h6 y348 ff14 fs3 fca sc0 ls0 ws0">#<span class="_ _2a"> </span><span class="ff6">繪製結果</span></div><div class="t m0 x31 h6 y349 ffa fs3 fc0 sc0 ls0 ws0">plt<span class="fc6">.</span>figure()<span class="_ _27"> </span><span class="ff14 fca">#<span class="_ _2a"> </span><span class="ff6">新的圖片</span></span></div><div class="t m0 x31 hc y34a ffa fs3 fc0 sc0 ls0 ws0">plt<span class="fc6">.</span>plot(x,<span class="_ _2a"> </span>y,<span class="_ _2a"> </span><span class="ff12 fcc">'<span class="ffa">o</span>'</span>)</div><div class="t m0 x31 hc y34b ffa fs3 fc0 sc0 ls0 ws0">plt<span class="fc6">.</span>plot(x_interp,<span class="_ _2a"> </span>y_interp);</div><div class="t m0 x4 h4 y854 ff5 fs3 fc0 sc0 ls0 ws0">我們看到的是在點之間平滑的插值。</div><div class="t m0 x5 h8 y855 ff1 fs1 fc0 sc0 ls0 ws0">16.5<span class="_ _39"> </span><span class="ff2">其他資料科學函式庫</span></div><div class="t m0 x4 h4 y856 ff5 fs3 fc0 sc0 ls0 ws0">許多其他資料科學套件建立在這些工具之上,包括一些通用工具,比如機器學習的函式庫<span class="_ _3"> </span><span class="ff7 fc2">Scikit-</span></div><div class="t m0 x5 h4 y857 ff7 fs3 fc2 sc0 ls0 ws0">Learn<span class="ff5 fc0">,圖像處理、分析的工具<span class="_ _3"> </span></span>Scikit-Image<span class="ff5 fc0">,以及統計建模的工具<span class="_ _3"> </span></span>Statsmodels<span class="ff5 fc0">,還有其他一</span></div><div class="t m0 x5 h4 y858 ff5 fs3 fc0 sc0 ls0 ws0">些更專注於領域的函式庫,比如針對天文學和天體物理學的工具<span class="_ _3"> </span><span class="ff7 fc2">AstroPy</span>,神經影像學工具<span class="_ _3"> </span><span class="ff7 fc2">NiPy</span>,</div><div class="t m0 x5 h4 y859 ff5 fs3 fc0 sc0 ls0 ws0">還有更多……</div><div class="t m0 x4 h4 y85a ff5 fs3 fc0 sc0 ls0 ws0">無論你遇到什麼樣的科學、數值或統計問題,都可能有一個<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>套件可以幫助你解決它。</div><a class="l" href="http://scikit-learn.org"><div class="d m1" style="border-style:none;position:absolute;left:492.669000px;bottom:381.550000px;width:30.607000px;height:7.515000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="http://scikit-learn.org"><div class="d m1" style="border-style:none;position:absolute;left:72.000000px;bottom:365.291000px;width:27.455000px;height:7.515000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="http://scikit-image.org"><div class="d m1" style="border-style:none;position:absolute;left:225.193000px;bottom:363.169000px;width:59.394000px;height:9.637000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="http://statsmodels.sourceforge.net/"><div class="d m1" style="border-style:none;position:absolute;left:399.417000px;bottom:365.291000px;width:57.393000px;height:7.515000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="http://astropy.org"><div class="d m1" style="border-style:none;position:absolute;left:363.384000px;bottom:346.910000px;width:39.637000px;height:9.637000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="http://nipy.org/"><div class="d m1" style="border-style:none;position:absolute;left:495.345000px;bottom:346.910000px;width:24.394000px;height:9.637000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf6c" class="pf w0 h0" data-page-no="6c"><div class="pc pc6c w0 h0"><img class="bi x5 y85b w2 h12" alt="" src="images/bg6c.png"/><div class="t m0 x61 h1 y3 ff2 fs0 fc0 sc0 ls0 ws0">第<span class="_ _0"> </span><span class="ff1">17<span class="_ _0"> </span></span>章<span class="_ _32"> </span>更多學習資源</div><div class="t m0 x4 h4 y204 ff5 fs3 fc0 sc0 ls0 ws0">讓<span class="_ _4a"></span>我<span class="_ _55"> </span>們<span class="_ _55"></span>用<span class="_ _4a"></span>這<span class="_ _55"> </span>一<span class="_ _55"></span>章<span class="_ _4a"></span>來<span class="_ _55"> </span>總<span class="_ _4a"></span>結<span class="_ _55"> </span>我<span class="_ _55"> </span>們<span class="_ _4a"></span>的<span class="_ _10"> </span><span class="ff7">Python<span class="_ _10"> </span></span>旋<span class="_ _4a"></span>風<span class="_ _55"> </span>之<span class="_ _55"> </span>旅!<span class="_ _4a"></span>我<span class="_ _55"> </span>希<span class="_ _55"></span>望<span class="_ _4a"></span>當<span class="_ _55"> </span>你<span class="_ _55"></span>閱<span class="_ _4a"></span>讀<span class="_ _55"> </span>到<span class="_ _4a"></span>這<span class="_ _55"> </span>裡<span class="_ _55"> </span>時,<span class="_ _4a"></span>你<span class="_ _55"> </span>能</div><div class="t m0 x5 h4 y205 ff5 fs3 fc0 sc0 ls0 ws0">對<span class="_ _34"> </span><span class="ff7">Python<span class="_ _9"> </span></span>的基本的語法、<span class="_ _2"></span>語意、<span class="_ _2"></span>運算子和功能有一定的了解,<span class="_ _2"></span>並且能掌握一些<span class="_ _9"> </span><span class="ff7">Python<span class="_ _9"> </span></span>的工具</div><div class="t m0 x5 h4 y206 ff5 fs3 fc0 sc0 ls0 ws0">和程式碼塊來幫助你探索更多<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>的知識。</div><div class="t m0 x4 h4 y6a9 ff5 fs3 fc0 sc0 ls0 ws0">在全書中我們已經嘗試向你介紹<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>在一些在資料科學領域中最有用的知識,但這並</div><div class="t m0 x5 h4 y6aa ff5 fs3 fc0 sc0 ls0 ws0">不意味著這是一個全面、完整的<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>介紹。如果你需要進一步理解<span class="_ _3"> </span><span class="ff7">Python<span class="_ _3"> </span></span>語言本身以及</div><div class="t m0 x5 h4 y6ab ff5 fs3 fc0 sc0 ls0 ws0">如何高效使用<span class="_ _9"> </span><span class="ff7">Python</span>,下面是一些我推薦的資源:</div><div class="t m0 x4 h7 y85c ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff8 fc2">Fluent<span class="_ _13"> </span>Python</span></div><div class="t m0 x76 hb y85d ffe fs4 fc1 sc0 ls0 ws0">1</div><div class="t m0 x33 h4 y85c ff5 fs3 fc0 sc0 ls0 ws0">,作者:<span class="ff7">Luciano<span class="_ _a"> </span>Ramalho</span>。這是一本非常不錯的<span class="_ _a"> </span><span class="ff7">Python<span class="_ _a"> </span></span>教材,探索</div><div class="t m0 xd h4 y85e ff5 fs3 fc0 sc0 ls0 ws0">了<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>中的最佳實踐以及常見的用法,包括標準函式庫中大多數的用法。</div><div class="t m0 x4 h4 y85f ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff8 fc2">Dive<span class="_ _3"> </span>Into<span class="_ _9"> </span>Python</span><span class="ff5">,<span class="_ _8"></span>作者:<span class="_ _8"></span><span class="ff7">Mark<span class="_ _34"> </span>Pilgrim<span class="ff5">。<span class="_ _8"></span>這是一本免費的線上書籍。<span class="_ _8"></span>介紹了<span class="_ _9"> </span><span class="ff7">Python<span class="_ _9"> </span></span>語</span></span></span></div><div class="t m0 xd h4 y860 ff5 fs3 fc0 sc0 ls0 ws0">言的詳細用法。</div><div class="t m0 x4 h7 y861 ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff8 fc2">L<span class="_ _8"></span>e<span class="_ _8"></span>arn<span class="_ _3"> </span>Python<span class="_ _9"> </span>the<span class="_ _3"> </span>Har<span class="_ _8"></span>d<span class="_ _3"> </span>W<span class="_ _2"></span>ay</span></div><div class="t m0 x44 hb y862 ffe fs4 fc1 sc0 ls0 ws0">2</div><div class="t m0 x77 h4 y861 ff5 fs3 fc0 sc0 ls0 ws0">,<span class="_ _8"></span>作者:<span class="_ _8"></span><span class="ff7">Zed<span class="_ _9"> </span>Sha<span class="_ _8"></span>w<span class="ff5">。<span class="_ _8"></span>這本書通過<span class="_ _4"></span>“在嘗試中學習”<span class="_ _8"></span>的方法,</span></span></div><div class="t m0 xd h4 y863 ff5 fs3 fc0 sc0 ls0 ws0">著重強調了一個程式設計師可能最重要的一種技能:上<span class="_ _9"> </span><span class="ff7">Google<span class="_ _3"> </span></span>搜尋自己不清楚的問題。</div><div class="t m0 x4 h7 y864 ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff8 fc2">Python<span class="_ _3"> </span>Essential<span class="_ _3"> </span>R<span class="_ _8"></span>efer<span class="_ _8"></span>enc<span class="_ _8"></span>e</span></div><div class="t m0 x18 hb y865 ffe fs4 fc1 sc0 ls0 ws0">3</div><div class="t m0 x44 h4 y864 ff5 fs3 fc0 sc0 ls0 ws0">,<span class="_ _4"></span>作者:<span class="_ _4"></span><span class="ff7">Da<span class="_ _4"></span>vid<span class="_ _9"> </span>Beazley<span class="ff5">。這本精心寫作的<span class="_ _34"> </span></span>700<span class="_ _3"> </span><span class="ff5">頁的<span class="_ _8"></span>“怪獸”</span></span></div><div class="t m0 xd h4 y866 ff5 fs3 fc0 sc0 ls0 ws0">包括了幾乎所有需要知道的<span class="_ _3"> </span><span class="ff7">Python<span class="_ _9"> </span></span>語言知識和它內建的函式庫。如果你需要更專注於具體</div><div class="t m0 xd h4 y867 ff5 fs3 fc0 sc0 ls0 ws0">應用的知識,你可以看這位作者的另外一本書:<span class="ff8 fc2">Python<span class="_ _3"> </span>Co<span class="_ _8"></span>okb<span class="_ _8"></span>o<span class="_ _8"></span>ok</span></div><div class="t m0 x1b hb y868 ffe fs4 fc1 sc0 ls0 ws0">4</div><div class="t m0 x78 h4 y867 ff5 fs3 fc0 sc0 ls0 ws0">。</div><div class="t m0 x4 h4 y869 ff5 fs3 fc0 sc0 ls0 ws0">在資料科學和科學計算領域,你可以閱讀以下書籍來挖掘更多<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>的知識:</div><div class="t m0 x4 h7 y86a ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff8 fc2">The<span class="_ _9"> </span>Python<span class="_ _3"> </span>Data<span class="_ _9"> </span>Scienc<span class="_ _4"></span>e<span class="_ _9"> </span>Handb<span class="_ _8"></span>o<span class="_ _4"></span>ok</span></div><div class="t m0 x79 hb y86b ffe fs4 fc1 sc0 ls0 ws0">5</div><div class="t m0 x4d h4 y86a ff5 fs3 fc0 sc0 ls0 ws0">,<span class="_ _2"></span>作者:<span class="_ _2"></span><span class="ff7">Jake<span class="_ _34"> </span>V<span class="_ _2"></span>anderPlas<span class="ff5">。<span class="_ _8"></span>這本書從我們這次旋風</span></span></div><div class="t m0 xd h4 y86c ff5 fs3 fc0 sc0 ls0 ws0">之旅結束的地方開始,為<span class="_ _3"> </span><span class="ff7">Python<span class="_ _9"> </span></span>資料科學的技術棧中的基本工具(從資料管理、操作</div><div class="t m0 xd h4 y86d ff5 fs3 fc0 sc0 ls0 ws0">到機器學習等方面)提供了全面的指南。</div><div class="t m0 x4 h7 y86e ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff8 fc2">Effe<span class="_ _8"></span>ctive<span class="_ _3"> </span>Computation<span class="_ _3"> </span>in<span class="_ _3"> </span>Physics</span></div><div class="t m0 x3 hb y86f ffe fs4 fc1 sc0 ls0 ws0">6</div><div class="t m0 x19 h4 y86e ff5 fs3 fc0 sc0 ls0 ws0">,作者:<span class="_ _8"></span><span class="ff7">Katie<span class="_ _3"> </span>Huff<span class="ff5">、<span class="_ _4"></span><span class="ff7">An<span class="_ _4"></span>thon<span class="_ _4"></span>y<span class="_ _9"> </span>Scopatz<span class="ff5">。這本書適用</span></span></span></span></div><div class="t m0 xd h4 y870 ff5 fs3 fc0 sc0 ls0 ws0">於需要進行物理研究的人。這本書逐步介紹了科學計算的內容,還介紹了本書中提到的</div><div class="t m0 xd h4 y871 ff5 fs3 fc0 sc0 ls0 ws0">許多工具的使用方法。</div><div class="t m0 x4 h7 y872 ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff8 fc2">Python<span class="_ _3"> </span>for<span class="_ _3"> </span>Data<span class="_ _3"> </span>A<span class="_ _8"></span>nalysis</span></div><div class="t m0 x46 hb y873 ffe fs4 fc1 sc0 ls0 ws0">7</div><div class="t m0 x7a h4 y872 ff5 fs3 fc0 sc0 ls0 ws0">,作者:<span class="ff7">W<span class="_ _2"></span>es<span class="_ _3"> </span>McKinney<span class="ff5">。這本書的作者是<span class="_ _3"> </span></span>P<span class="_ _8"></span>andas<span class="_ _3"> </span><span class="ff5">函式庫的創始</span></span></div><div class="t m0 xd h4 y874 ff5 fs3 fc0 sc0 ls0 ws0">人。這本書涵蓋了<span class="_ _9"> </span><span class="ff7">Pandas<span class="_ _9"> </span></span>函式庫的詳盡知識,也提供了許多其他工具的有用知識。</div><div class="t m0 x4 h4 y875 ff5 fs3 fc0 sc0 ls0 ws0">最後,為了更廣泛地了解<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>世界,我推薦如下資源:</div><div class="t m0 x12 hd y876 fff fs5 fc0 sc0 ls0 ws0">1</div><div class="t m0 xa he y877 ff5 fs6 fc0 sc0 ls0 ws0">譯者注:中文版《流暢的<span class="_ _34"> </span><span class="ff10">Python</span>》已由人民郵電出版社於<span class="_ _36"> </span><span class="ff10">2017<span class="_ _34"> </span></span>年<span class="_ _34"> </span><span class="ff10">5<span class="_ _34"> </span></span>月出版。</div><div class="t m0 x12 hd y878 fff fs5 fc0 sc0 ls0 ws0">2</div><div class="t m0 xa he y879 ff5 fs6 fc0 sc0 ls0 ws0">譯者注:本書翻譯時該書作者<span class="_ _9"> </span><span class="ff10">Zed<span class="_ _3"> </span>Sha<span class="_ _8"></span>w<span class="_ _3"> </span><span class="ff5">已經為<span class="_ _9"> </span></span>Python<span class="_ _9"> </span>3<span class="_ _3"> </span><span class="ff5">出版新版<span class="_ _3"> </span><span class="ff1f">L<span class="_ _8"></span>e<span class="_ _8"></span>arn<span class="_ _3"> </span>Python<span class="_ _3"> </span>3<span class="_ _3"> </span>the<span class="_ _3"> </span>Har<span class="_ _8"></span>d<span class="_ _3"> </span>W<span class="_ _8"></span>ay<span class="ff5">。原書</span></span></span></span></div><div class="t m0 x5 he y87a ff1f fs6 fc0 sc0 ls0 ws0">L<span class="_ _8"></span>e<span class="_ _8"></span>arn<span class="_ _9"> </span>Python<span class="_ _34"> </span>the<span class="_ _9"> </span>Har<span class="_ _8"></span>d<span class="_ _34"> </span>W<span class="_ _8"></span>ay<span class="_ _9"> </span><span class="ff5">中文版《<span class="_ _44"></span>“笨辦法”學<span class="_ _34"> </span><span class="ff10">Python</span>》已由人民郵電出版社於<span class="_ _36"> </span><span class="ff10">2014<span class="_ _34"> </span></span>年<span class="_ _34"> </span><span class="ff10">11<span class="_ _34"> </span></span>月出版。</span></div><div class="t m0 x12 hd y87b fff fs5 fc0 sc0 ls0 ws0">3</div><div class="t m0 xa he y87c ff5 fs6 fc0 sc0 ls0 ws0">譯者注:中文版《<span class="ff10">Python<span class="_ _34"> </span></span>參考手冊(第四版)<span class="_ _97"></span>》已由人民郵電出版社於<span class="_ _34"> </span><span class="ff10">2016<span class="_ _36"> </span></span>年<span class="_ _34"> </span><span class="ff10">10<span class="_ _34"> </span></span>月出版。</div><div class="t m0 x12 hd y87d fff fs5 fc0 sc0 ls0 ws0">4</div><div class="t m0 xa he y87e ff5 fs6 fc0 sc0 ls0 ws0">譯者注:中文版《<span class="ff10">Python<span class="_ _34"> </span>Cookbook<span class="_ _34"> </span></span>中文版(第三版)<span class="_ _97"></span>》已由人民郵電出版社於<span class="_ _34"> </span><span class="ff10">2015<span class="_ _34"> </span></span>年<span class="_ _34"> </span><span class="ff10">5<span class="_ _36"> </span></span>月出版。</div><div class="t m0 x12 hd y87f fff fs5 fc0 sc0 ls0 ws0">5</div><div class="t m0 xa he y880 ff5 fs6 fc0 sc0 ls0 ws0">譯者注:中文版《<span class="ff10">Python<span class="_ _34"> </span></span>資料科學手冊》已由人民郵電出版社於<span class="_ _36"> </span><span class="ff10">2018<span class="_ _34"> </span></span>年<span class="_ _34"> </span><span class="ff10">1<span class="_ _34"> </span></span>月出版。</div><div class="t m0 x12 hd y881 fff fs5 fc0 sc0 ls0 ws0">6</div><div class="t m0 xa he y882 ff5 fs6 fc0 sc0 ls0 ws0">譯者注:中文版《<span class="ff10">Python<span class="_ _34"> </span></span>物理學高效計算》已由人民郵電出版社於<span class="_ _36"> </span><span class="ff10">2018<span class="_ _34"> </span></span>年<span class="_ _34"> </span><span class="ff10">2<span class="_ _34"> </span></span>月出版。</div><div class="t m0 x12 hd yb9 fff fs5 fc0 sc0 ls0 ws0">7</div><div class="t m0 xa he yba ff5 fs6 fc0 sc0 ls0 ws0">譯者注:<span class="_ _8"></span>本書翻譯時,該書已經更新<span class="fc2">第<span class="_ _36"> </span><span class="ff10">2<span class="_ _36"> </span></span>版</span>,仍然由<span class="_ _36"> </span><span class="ff10">O’Reilly<span class="_ _36"> </span></span>公司出版。原書<span class="_ _36"> </span><span class="ff1f">Python<span class="_ _34"> </span>for<span class="_ _34"> </span>Data<span class="_ _34"> </span>A<span class="_ _4"></span>nalysis<span class="_ _34"> </span><span class="ff5">中文</span></span></div><div class="t m0 x5 he y40 ff5 fs6 fc0 sc0 ls0 ws0">版《利用<span class="_ _34"> </span><span class="ff10">Python<span class="_ _36"> </span></span>進行資料分析》已由機械工業出版社於<span class="_ _34"> </span><span class="ff10">2013<span class="_ _34"> </span></span>年<span class="_ _34"> </span><span class="ff10">11<span class="_ _36"> </span></span>月出版。</div><div class="t m0 x7b h7 y20 ff7 fs3 fc0 sc0 ls0 ws0">101</div><a class="l" href="http://shop.oreilly.com/product/0636920032519.do"><div class="d m1" style="border-style:none;position:absolute;left:107.758000px;bottom:509.968000px;width:68.621000px;height:9.636000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf6c" data-dest-detail='[108,"XYZ",88.59,181.15,null]'><div class="d m1" style="border-style:none;position:absolute;left:176.945000px;bottom:516.048000px;width:4.234000px;height:5.136000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="http://www.diveintopython.net/"><div class="d m1" style="border-style:none;position:absolute;left:107.758000px;bottom:477.450000px;width:82.291000px;height:9.636000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="http://learnpythonthehardway.org/"><div class="d m1" style="border-style:none;position:absolute;left:107.758000px;bottom:444.932000px;width:134.229000px;height:9.636000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf6c" data-dest-detail='[108,"XYZ",88.59,169.53,null]'><div class="d m1" style="border-style:none;position:absolute;left:242.617000px;bottom:451.012000px;width:4.234000px;height:5.136000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="http://www.dabeaz.com/per.html"><div class="d m1" style="border-style:none;position:absolute;left:107.758000px;bottom:412.414000px;width:129.544000px;height:9.636000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf6c" data-dest-detail='[108,"XYZ",88.59,144.77,null]'><div class="d m1" style="border-style:none;position:absolute;left:237.692000px;bottom:418.494000px;width:4.234000px;height:5.136000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="http://shop.oreilly.com/product/0636920027072.do"><div class="d m1" style="border-style:none;position:absolute;left:336.849000px;bottom:379.896000px;width:80.999000px;height:9.636000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf6c" data-dest-detail='[108,"XYZ",88.59,133.15,null]'><div class="d m1" style="border-style:none;position:absolute;left:418.612000px;bottom:385.976000px;width:4.234000px;height:5.136000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="http://shop.oreilly.com/product/0636920034919.do"><div class="d m1" style="border-style:none;position:absolute;left:107.758000px;bottom:325.984000px;width:170.407000px;height:9.636000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf6c" data-dest-detail='[108,"XYZ",88.59,121.53,null]'><div class="d m1" style="border-style:none;position:absolute;left:278.930000px;bottom:332.064000px;width:4.234000px;height:5.136000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="http://shop.oreilly.com/product/0636920033424.do"><div class="d m1" style="border-style:none;position:absolute;left:107.758000px;bottom:277.207000px;width:158.812000px;height:9.636000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf6c" data-dest-detail='[108,"XYZ",88.59,109.92,null]'><div class="d m1" style="border-style:none;position:absolute;left:266.984000px;bottom:283.287000px;width:4.234000px;height:5.136000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="http://shop.oreilly.com/product/0636920023784.do"><div class="d m1" style="border-style:none;position:absolute;left:107.758000px;bottom:228.430000px;width:121.547000px;height:9.636000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="#pf6c" data-dest-detail='[108,"XYZ",88.59,98.3,null]'><div class="d m1" style="border-style:none;position:absolute;left:229.719000px;bottom:234.510000px;width:4.234000px;height:5.136000px;background-color:rgba(255,255,255,0.000001);"></div></a><a class="l" href="http://shop.oreilly.com/product/0636920050896.do"><div class="d m1" style="border-style:none;position:absolute;left:231.295000px;bottom:82.594000px;width:28.511000px;height:10.297000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf6d" class="pf w0 h0" data-page-no="6d"><div class="pc pc6d w0 h0"><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">102<span class="_ _79"> </span><span class="ffc">17.<span class="_ _16"> </span><span class="ff6">更多學習資源</span></span></div><div class="t m0 x4 h4 y42 ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff8 fc2">O’R<span class="_ _8"></span>eilly<span class="_ _3"> </span>Python<span class="_ _9"> </span>R<span class="_ _8"></span>esour<span class="_ _8"></span>c<span class="_ _8"></span>es<span class="ff5 fc0">:<span class="_ _3e"></span><span class="ff7">O’Reilly<span class="_ _9"> </span><span class="ff5">出版公司提供了許多關於<span class="_ _34"> </span></span>Python<span class="_ _9"> </span><span class="ff5">的優秀書籍,<span class="_ _35"></span>並</span></span></span></span></div><div class="t m0 xd h4 y77 ff5 fs3 fc0 sc0 ls0 ws0">且提供了許多專門的主題,讓你在<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>世界中遨遊。</div><div class="t m0 x4 h4 y78 ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff7">PyCon<span class="ff5">、<span class="_ _8"></span><span class="ff7">SciPy<span class="ff5">、<span class="_ _8"></span>和<span class="_ _9"> </span><span class="ff7">PyData</span>:<span class="_ _4"></span><span class="ff7">PyCon<span class="ff5">、<span class="_ _8"></span><span class="ff7">SciPy<span class="ff5">、<span class="_ _8"></span><span class="ff7">PyData<span class="_ _3"> </span><span class="ff5">大會每年吸引了數以千計的參會</span></span></span></span></span></span></span></span></span></span></div><div class="t m0 xd h4 y262 ff5 fs3 fc0 sc0 ls0 ws0">者,並且每年將其大部分節目存檔為免費線上視訊。這些已經成為一套學習<span class="_ _3"> </span><span class="ff7">Python<span class="_ _9"> </span></span>語</div><div class="t m0 xd h4 y883 ff5 fs3 fc0 sc0 ls0 ws0">言、<span class="ff7">Python<span class="_ _3"> </span></span>套件和函式庫函數以及有關領域非常好的資料。你可以搜尋訪談和課程的免費資</div><div class="t m0 xd h4 y884 ff5 fs3 fc0 sc0 ls0 ws0">源:其中訪談通常比較短,涉及到了新的函式庫或者對以前的函式庫的新鮮的觀點;而課程可能</div><div class="t m0 xd h4 y885 ff5 fs3 fc0 sc0 ls0 ws0">長達數小時,涉及到如何使用討論的這些工具以及其他內容。</div><a class="l" href="http://shop.oreilly.com/category/browse-subjects/programming/python.do"><div class="d m1" style="border-style:none;position:absolute;left:107.758000px;bottom:756.810000px;width:126.326000px;height:9.636000px;background-color:rgba(255,255,255,0.000001);"></div></a></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf6e" class="pf w0 h0" data-page-no="6e"><div class="pc pc6e w0 h0"><img class="bi x5 y40 w1 h25" alt="" src="images/bg6e.png"/><div class="t m0 x76 h1 y3 ff2 fs0 fc0 sc0 ls0 ws0">第<span class="_ _0"> </span><span class="ff1">A<span class="_ _0"> </span></span>章<span class="_ _32"> </span>附錄:範例程式碼</div><div class="t m0 x4 h4 y204 ff5 fs3 fc0 sc0 ls0 ws0">本章包含產生本書中出現的範例程式碼。</div><div class="t m0 x11 hc y886 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[1]:<span class="_ _2a"> </span><span class="fc6">%<span class="ff11 fc4">matplotlib<span class="_ _2a"> </span></span><span class="fc0">inline</span></span></div><div class="t m0 x24 h31 y887 ff11 fs3 fc4 sc0 ls0 ws0">import<span class="_ _2a"> </span><span class="fc5">matplotlib.pyplot<span class="_ _2a"> </span></span>as<span class="_ _2a"> </span><span class="fc5">plt</span></div><div class="t m0 x11 hc y888 ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[2]:<span class="_ _2a"> </span><span class="ff11 fc4">import<span class="_ _2a"> </span><span class="fc5">os</span></span></div><div class="t m0 x24 hc y889 ff11 fs3 fc4 sc0 ls0 ws0">if<span class="_ _2a"> </span><span class="fcb">not<span class="_ _2a"> </span><span class="ffa fc0">os<span class="fc6">.</span>path<span class="fc6">.</span>exists(<span class="ff12 fcc">'<span class="ffa">fig</span>'</span>):</span></span></div><div class="t m0 x5d hc y88a ffa fs3 fc0 sc0 ls0 ws0">os<span class="fc6">.</span>makedirs(<span class="ff12 fcc">'<span class="ffa">fig</span>'</span>)</div><div class="t m0 x5 h8 y88b ff1 fs1 fc0 sc0 ls0 ws0">A.1<span class="_ _39"> </span><span class="ff2">第<span class="_ _10"> </span></span>7<span class="_ _10"> </span><span class="ff2">章:列表索引</span></div><div class="t m0 x4 h4 y88c ff5 fs3 fc0 sc0 ls0 ws0">這段程式能幫助你更好的理解<span class="_ _9"> </span><span class="ff7">Python<span class="_ _3"> </span></span>索引的工作方式。</div><div class="t m0 x11 hc y88d ffa fs3 fc3 sc0 ls0 ws0">In<span class="_ _2a"> </span>[3]:<span class="_ _2a"> </span><span class="fc0">L<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>[<span class="fc6">2</span>,<span class="_ _2a"> </span><span class="fc6">3</span>,<span class="_ _2a"> </span><span class="fc6">5</span>,<span class="_ _2a"> </span><span class="fc6">7</span>,<span class="_ _2a"> </span><span class="fc6">11</span>]</span></div><div class="t m0 x24 hc y88e ffa fs3 fc0 sc0 ls0 ws0">fig<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>plt<span class="fc6">.</span>figure(figsize<span class="fc6">=</span>(<span class="fc6">10</span>,<span class="_ _2a"> </span><span class="fc6">4</span>))</div><div class="t m0 x24 hc y88f ffa fs3 fc0 sc0 ls0 ws0">ax<span class="_ _2a"> </span><span class="fc6">=<span class="_ _2a"> </span></span>fig<span class="fc6">.</span>add_axes([<span class="fc6">0</span>,<span class="_ _2a"> </span><span class="fc6">0</span>,<span class="_ _2a"> </span><span class="fc6">1</span>,<span class="_ _2a"> </span><span class="fc6">1</span>],<span class="_ _2a"> </span>xticks<span class="fc6">=</span>[],<span class="_ _2a"> </span>yticks<span class="fc6">=</span>[],<span class="_ _2a"> </span>frameon<span class="fc6">=<span class="ff11 fc4">False</span></span>,</div><div class="t m0 x57 hc y890 ffa fs3 fc0 sc0 ls0 ws0">aspect<span class="fc6">=<span class="ff12 fcc">'<span class="ffa">equal</span>'</span></span>)</div><div class="t m0 x24 hc y891 ff11 fs3 fc4 sc0 ls0 ws0">for<span class="_ _2a"> </span><span class="ffa fc0">i<span class="_ _2a"> </span></span><span class="fcb">in<span class="_ _2a"> </span></span><span class="ffa">range<span class="fc0">(<span class="fc6">5</span>):</span></span></div><div class="t m0 x5d hc y892 ffa fs3 fc0 sc0 ls0 ws0">ax<span class="fc6">.</span>add_patch(plt<span class="fc6">.</span>Rectangle([i<span class="_ _2a"> </span><span class="fc6">-<span class="_ _2a"> </span>0.5</span>,<span class="_ _2a"> </span><span class="fc6">-0.5</span>],<span class="_ _2a"> </span><span class="fc6">1</span>,<span class="_ _2a"> </span><span class="fc6">1</span>,<span class="_ _2a"> </span>fc<span class="fc6">=<span class="ff12 fcc">'<span class="ffa">none</span>'</span></span>,</div><div class="t m0 x7c hc y893 ffa fs3 fc0 sc0 ls0 ws0">ec<span class="fc6">=<span class="ff12 fcc">'<span class="ffa">black</span>'</span></span>))</div><div class="t m0 x5d hc y894 ffa fs3 fc0 sc0 ls0 ws0">ax<span class="fc6">.</span>text(i,<span class="_ _2a"> </span><span class="fc6">-0.05</span>,<span class="_ _2a"> </span>L[i],<span class="_ _2a"> </span>size<span class="fc6">=100</span>,</div><div class="t m0 xc hc y4bf ffa fs3 fc0 sc0 ls0 ws0">ha<span class="fc6">=<span class="ff12 fcc">'<span class="ffa">center</span>'</span></span>,<span class="_ _2a"> </span>va<span class="fc6">=<span class="ff12 fcc">'<span class="ffa">center</span>'</span></span>,<span class="_ _2a"> </span>family<span class="fc6">=<span class="ff12 fcc">'<span class="ffa">monospace</span>'</span></span>)</div><div class="t m0 x24 hc y4c1 ff11 fs3 fc4 sc0 ls0 ws0">for<span class="_ _2a"> </span><span class="ffa fc0">i<span class="_ _2a"> </span></span><span class="fcb">in<span class="_ _2a"> </span></span><span class="ffa">range<span class="fc0">(<span class="fc6">6</span>):</span></span></div><div class="t m0 x5d hc y4c2 ffa fs3 fc0 sc0 ls0 ws0">ax<span class="fc6">.</span>text(i<span class="_ _2a"> </span><span class="fc6">-<span class="_ _2a"> </span>0.5</span>,<span class="_ _2a"> </span><span class="fc6">0.55</span>,<span class="_ _2a"> </span><span class="fc4">str</span>(i),<span class="_ _2a"> </span>size<span class="fc6">=20</span>,</div><div class="t m0 xc hc y4c3 ffa fs3 fc0 sc0 ls0 ws0">ha<span class="fc6">=<span class="ff12 fcc">'<span class="ffa">center</span>'</span></span>,<span class="_ _2a"> </span>va<span class="fc6">=<span class="ff12 fcc">'<span class="ffa">bottom</span>'</span></span>,<span class="_ _2a"> </span>family<span class="fc6">=<span class="ff12 fcc">'<span class="ffa">monospace</span>'</span></span>)</div><div class="t m0 x24 hc y328 ff11 fs3 fc4 sc0 ls0 ws0">for<span class="_ _2a"> </span><span class="ffa fc0">i<span class="_ _2a"> </span></span><span class="fcb">in<span class="_ _2a"> </span></span><span class="ffa">range<span class="fc0">(<span class="fc6">5</span>):</span></span></div><div class="t m0 x5d hc y3fe ffa fs3 fc0 sc0 ls0 ws0">ax<span class="fc6">.</span>text(i<span class="_ _2a"> </span><span class="fc6">-<span class="_ _2a"> </span>0.5</span>,<span class="_ _2a"> </span><span class="fc6">-0.58</span>,<span class="_ _2a"> </span><span class="fc4">str</span>(<span class="fc6">-5<span class="_ _2a"> </span>+<span class="_ _2a"> </span></span>i),<span class="_ _2a"> </span>size<span class="fc6">=20</span>,</div><div class="t m0 xc hc y329 ffa fs3 fc0 sc0 ls0 ws0">ha<span class="fc6">=<span class="ff12 fcc">'<span class="ffa">center</span>'</span></span>,<span class="_ _2a"> </span>va<span class="fc6">=<span class="ff12 fcc">'<span class="ffa">top</span>'</span></span>,<span class="_ _2a"> </span>family<span class="fc6">=<span class="ff12 fcc">'<span class="ffa">monospace</span>'</span></span>)</div><div class="t m0 x24 hc y117 ffa fs3 fc0 sc0 ls0 ws0">ax<span class="fc6">.</span>axis([<span class="fc6">-0.7</span>,<span class="_ _2a"> </span><span class="fc6">4.7</span>,<span class="_ _2a"> </span><span class="fc6">-0.7</span>,<span class="_ _2a"> </span><span class="fc6">0.7</span>]);</div><div class="t m0 x24 hc y119 ffa fs3 fc0 sc0 ls0 ws0">fig<span class="fc6">.</span>savefig(<span class="ff12 fcc">'<span class="ffa">fig/list-indexing.png</span>'</span>);</div><div class="t m0 x7b h7 y20 ff7 fs3 fc0 sc0 ls0 ws0">103</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf6f" class="pf w0 h0" data-page-no="6f"><div class="pc pc6f w0 h0"><img class="bi x4 y895 w3 h5b" alt="" src="images/bg6f.png"/><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">104<span class="_ _98"> </span><span class="ffc">A.<span class="_ _1a"> </span><span class="ff6">附錄:插圖程式碼</span></span></div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf70" class="pf w0 h0" data-page-no="70"><div class="pc pc70 w0 h0"><div class="t m0 x0 h1 y3 ff2 fs0 fc0 sc0 ls0 ws0">中英文術語對照表</div><div class="t m0 x5 h5 y896 ff2 fs3 fc0 sc0 ls0 ws0">聚集<span class="_ _9"> </span><span class="ff7">aggregate<span class="_ _3"> </span>97</span></div><div class="t m0 x5 h5 y897 ff2 fs3 fc0 sc0 ls0 ws0">參數<span class="_ _72"> </span><span class="ff7">argumen<span class="_ _4"></span>t<span class="_ _13"> </span>12,<span class="_ _1"> </span>13,<span class="_ _1"> </span>44,<span class="_ _72"> </span>45,<span class="_ _1"> </span>47–50,<span class="_ _1"> </span>57,<span class="_ _1"> </span>71,</span></div><div class="t m0 x7d h7 y898 ff7 fs3 fc0 sc0 ls0 ws0">83,<span class="_ _9"> </span>87</div><div class="t m0 x5 h5 y899 ff2 fs3 fc0 sc0 ls0 ws0">算術運算子<span class="_ _9"> </span><span class="ff7">arithmetic<span class="_ _3"> </span>operator<span class="_ _3"> </span>20–22,<span class="_ _9"> </span>86</span></div><div class="t m0 x5 h5 y89a ff2 fs3 fc0 sc0 ls0 ws0">賦值運算子<span class="_ _9"> </span><span class="ff7">assignment<span class="_ _9"> </span>operator<span class="_ _9"> </span>10,<span class="_ _3"> </span>21,<span class="_ _9"> </span>22</span></div><div class="t m0 x5 h5 y89b ff2 fs3 fc0 sc0 ls0 ws0">屬性<span class="_ _9"> </span><span class="ff7">attribute<span class="_ _3"> </span>17,<span class="_ _9"> </span>18,<span class="_ _9"> </span>30</span></div><div class="t m0 x5 h5 y89c ff2 fs3 fc0 sc0 ls0 ws0">二元算術運算子<span class="_ _34"> </span><span class="ff7">binary<span class="_ _9"> </span>arithmetic<span class="_ _34"> </span>operator<span class="_ _9"> </span>19</span></div><div class="t m0 x5 h5 y89d ff2 fs3 fc0 sc0 ls0 ws0">二元運算子<span class="_ _9"> </span><span class="ff7">binary<span class="_ _3"> </span>operator<span class="_ _3"> </span>12,<span class="_ _9"> </span>21</span></div><div class="t m0 x5 h5 y89e ff2 fs3 fc0 sc0 ls0 ws0">二進位制表示<span class="_ _9"> </span><span class="ff7">binary<span class="_ _3"> </span>represen<span class="_ _8"></span>tation<span class="_ _3"> </span>20</span></div><div class="t m0 x5 h5 y89f ff2 fs3 fc0 sc0 ls0 ws0">位運算<span class="_ _9"> </span><span class="ff7">bitwise<span class="_ _9"> </span>logical<span class="_ _9"> </span>operation<span class="_ _3"> </span>20</span></div><div class="t m0 x5 h5 y8a0 ff2 fs3 fc0 sc0 ls0 ws0">位元運算子<span class="_ _9"> </span><span class="ff7">bitwise<span class="_ _9"> </span>operator<span class="_ _9"> </span>20–23</span></div><div class="t m0 x5 h5 y8a1 ff2 fs3 fc0 sc0 ls0 ws0">程式碼塊<span class="_ _9"> </span><span class="ff7">block<span class="_ _9"> </span>6,<span class="_ _9"> </span>11,<span class="_ _3"> </span>12,<span class="_ _9"> </span>43,<span class="_ _3"> </span>47,<span class="_ _9"> </span>55,<span class="_ _9"> </span>101</span></div><div class="t m0 x5 h5 y8a2 ff2 fs3 fc0 sc0 ls0 ws0">布林代數<span class="_ _9"> </span><span class="ff7">boolean<span class="_ _3"> </span>algebra<span class="_ _9"> </span>23</span></div><div class="t m0 x5 h5 y8a3 ff2 fs3 fc0 sc0 ls0 ws0">布林運算子<span class="_ _9"> </span><span class="ff7">boolean<span class="_ _3"> </span>operator<span class="_ _9"> </span>23</span></div><div class="t m0 x5 h5 y8a4 ff2 fs3 fc0 sc0 ls0 ws0">笛卡爾積<span class="_ _9"> </span><span class="ff7">Cartesian<span class="_ _3"> </span>Product<span class="_ _3"> </span>66</span></div><div class="t m0 x5 h5 y8a5 ff2 fs3 fc0 sc0 ls0 ws0">比較運算子<span class="_ _9"> </span><span class="ff7">comparison<span class="_ _3"> </span>operator<span class="_ _3"> </span>22,<span class="_ _9"> </span>32</span></div><div class="t m0 x5 h5 y8a6 ff2 fs3 fc0 sc0 ls0 ws0">編譯<span class="_ _9"> </span><span class="ff7">compile<span class="_ _3"> </span>7,<span class="_ _9"> </span>88</span></div><div class="t m0 x5 h5 y8a7 ff2 fs3 fc0 sc0 ls0 ws0">編譯型<span class="_ _9"> </span><span class="ff7">compiled<span class="_ _3"> </span>5</span></div><div class="t m0 x5 h5 y8a8 ff2 fs3 fc0 sc0 ls0 ws0">複數<span class="_ _9"> </span><span class="ff7">complex<span class="_ _3"> </span>n<span class="_ _8"></span>umber<span class="_ _3"> </span>18,<span class="_ _9"> </span>30,<span class="_ _9"> </span>79</span></div><div class="t m0 x5 h5 y8a9 ff2 fs3 fc0 sc0 ls0 ws0">複合物件<span class="_ _9"> </span><span class="ff7">compound<span class="_ _3"> </span>object<span class="_ _3"> </span>17,<span class="_ _9"> </span>24,<span class="_ _3"> </span>48</span></div><div class="t m0 x5 h5 y8aa ff2 fs3 fc0 sc0 ls0 ws0">複合型別<span class="_ _9"> </span><span class="ff7">compound<span class="_ _3"> </span>t<span class="_ _8"></span>ype<span class="_ _9"> </span>27,<span class="_ _9"> </span>35,<span class="_ _3"> </span>36,<span class="_ _9"> </span>38</span></div><div class="t m0 x5 h5 y8ab ff2 fs3 fc0 sc0 ls0 ws0">條件語句<span class="_ _9"> </span><span class="ff7">conditional<span class="_ _3"> </span>statemen<span class="_ _8"></span>t<span class="_ _3"> </span>11,<span class="_ _9"> </span>23,<span class="_ _9"> </span>43</span></div><div class="t m0 x5 h5 y8ac ff2 fs3 fc0 sc0 ls0 ws0">建構函式<span class="_ _9"> </span><span class="ff7">constructor<span class="_ _3"> </span>28</span></div><div class="t m0 x5 h5 y8ad ff2 fs3 fc0 sc0 ls0 ws0">控制流程<span class="_ _9"> </span><span class="ff7">control<span class="_ _9"> </span>flo<span class="_ _4"></span>w<span class="_ _9"> </span>43</span></div><div class="t m0 x5 h5 y8ae ff2 fs3 fc0 sc0 ls0 ws0">控制流程語句<span class="_ _9"> </span><span class="ff7">control<span class="_ _9"> </span>flo<span class="_ _4"></span>w<span class="_ _9"> </span>statement<span class="_ _9"> </span>1,<span class="_ _9"> </span>11,<span class="_ _3"> </span>23</span></div><div class="t m0 x5 h5 y8af ff2 fs3 fc0 sc0 ls0 ws0">資料幀<span class="_ _9"> </span><span class="ff7">data<span class="_ _3"> </span>frame<span class="_ _9"> </span>1,<span class="_ _9"> </span>21,<span class="_ _3"> </span>97</span></div><div class="t m0 x5 h5 y8b0 ff2 fs3 fc0 sc0 ls0 ws0">預設值<span class="_ _9"> </span><span class="ff7">default<span class="_ _3"> </span>v<span class="_ _8"></span>alue<span class="_ _9"> </span>31,<span class="_ _9"> </span>37,<span class="_ _3"> </span>42,<span class="_ _9"> </span>48</span></div><div class="t m0 x5 h5 y8b1 ff2 fs3 fc0 sc0 ls0 ws0">字典推導<span class="_ _9"> </span><span class="ff7">dict<span class="_ _3"> </span>comprehension<span class="_ _9"> </span>69</span></div><div class="t m0 x5 h5 y8b2 ff2 fs3 fc0 sc0 ls0 ws0">字典<span class="_ _9"> </span><span class="ff7">dictionary<span class="_ _3"> </span>40–42,<span class="_ _9"> </span>49</span></div><div class="t m0 x5 h5 y8b3 ff2 fs3 fc0 sc0 ls0 ws0">動態型別<span class="_ _9"> </span><span class="ff7">dynamically<span class="_ _3"> </span>t<span class="_ _8"></span>yped<span class="_ _3"> </span>15,<span class="_ _9"> </span>16,<span class="_ _3"> </span>36</span></div><div class="t m0 x5 h5 y9a ff2 fs3 fc0 sc0 ls0 ws0">特徵值分解<span class="_ _9"> </span><span class="ff7">eigenv<span class="_ _8"></span>alue<span class="_ _9"> </span>decomposition<span class="_ _9"> </span>96</span></div><div class="t m0 x5 h5 y40 ff2 fs3 fc0 sc0 ls0 ws0">跳脫<span class="_ _9"> </span><span class="ff7">escape<span class="_ _3"> </span>91,<span class="_ _9"> </span>93</span></div><div class="t m0 x7e h5 y896 ff2 fs3 fc0 sc0 ls0 ws0">異常<span class="_ _9"> </span><span class="ff7">exception<span class="_ _3"> </span>54–57,<span class="_ _9"> </span>59,<span class="_ _9"> </span>84</span></div><div class="t m0 x7e h5 y897 ff2 fs3 fc0 sc0 ls0 ws0">異常處理<span class="_ _9"> </span><span class="ff7">exception<span class="_ _3"> </span>handling<span class="_ _9"> </span>53,<span class="_ _9"> </span>59,<span class="_ _3"> </span>60</span></div><div class="t m0 x7e h5 y898 ff2 fs3 fc0 sc0 ls0 ws0">異或<span class="_ _9"> </span><span class="ff7">exclusive<span class="_ _9"> </span>or<span class="_ _9"> </span>21,<span class="_ _3"> </span>23</span></div><div class="t m0 x7e h5 y899 ff2 fs3 fc0 sc0 ls0 ws0">指數表示法<span class="_ _9"> </span><span class="ff7">exponential<span class="_ _9"> </span>notation<span class="_ _9"> </span>28</span></div><div class="t m0 x7e h5 y8b4 ff2 fs3 fc0 sc0 ls0 ws0">快<span class="_ _60"> </span>速<span class="_ _60"> </span>傅<span class="_ _60"> </span>立<span class="_ _36"> </span>葉<span class="_ _89"> </span>變<span class="_ _36"> </span>換<span class="_ _1a"> </span><span class="ff7">F<span class="_ _2"></span>ast<span class="_ _26"> </span>F<span class="_ _8"></span>ourier<span class="_ _26"> </span>T<span class="_ _8"></span>ransforms</span></div><div class="t m0 x7f h7 y8b5 ff7 fs3 fc0 sc0 ls0 ws0">(FFT)<span class="_ _9"> </span>99</div><div class="t m0 x7e h5 y89c ff2 fs3 fc0 sc0 ls0 ws0">斐波那契數列<span class="_ _9"> </span><span class="ff7">Fibonacci<span class="_ _3"> </span>n<span class="_ _8"></span>umbers<span class="_ _3"> </span>45,<span class="_ _9"> </span>47</span></div><div class="t m0 x7e h5 y89d ff2 fs3 fc0 sc0 ls0 ws0">一等函式<span class="_ _9"> </span><span class="ff7">first-order<span class="_ _3"> </span>function<span class="_ _9"> </span>50</span></div><div class="t m0 x7e h5 y89e ff2 fs3 fc0 sc0 ls0 ws0">固定精度<span class="_ _9"> </span><span class="ff7">fixed-precision<span class="_ _3"> </span>27,<span class="_ _9"> </span>29</span></div><div class="t m0 x7e h5 y89f ff2 fs3 fc0 sc0 ls0 ws0">浮點<span class="_ _4a"></span>數<span class="_ _72"> </span><span class="ff7">floating-point<span class="_ _72"> </span>n<span class="_ _8"></span>umber<span class="_ _1"> </span>18,<span class="_ _10"> </span>20,<span class="_ _72"> </span>28,<span class="_ _10"> </span>29,</span></div><div class="t m0 x7f h7 y8a0 ff7 fs3 fc0 sc0 ls0 ws0">39,<span class="_ _9"> </span>87</div><div class="t m0 x7e h5 y8a1 ff2 fs3 fc0 sc0 ls0 ws0">向下整除<span class="_ _9"> </span><span class="ff7">floor<span class="_ _3"> </span>division<span class="_ _9"> </span>19,<span class="_ _9"> </span>20,<span class="_ _3"> </span>28</span></div><div class="t m0 x7e h5 y8a2 ff2 fs3 fc0 sc0 ls0 ws0">格式化字串<span class="_ _9"> </span><span class="ff7">format<span class="_ _3"> </span>string<span class="_ _9"> </span>81,<span class="_ _9"> </span>87</span></div><div class="t m0 x7e h5 y8a3 ff2 fs3 fc0 sc0 ls0 ws0">全排列<span class="_ _9"> </span><span class="ff7">full<span class="_ _3"> </span>permutation<span class="_ _9"> </span>66</span></div><div class="t m0 x7e h5 y8b6 ff2 fs3 fc0 sc0 ls0 ws0">函數<span class="_ _a"> </span><span class="ff7">function<span class="_ _a"> </span>1,<span class="_ _13"> </span>12,<span class="_ _13"> </span>13,<span class="_ _a"> </span>18,<span class="_ _13"> </span>30–32,<span class="_ _13"> </span>39,<span class="_ _a"> </span>47–51,</span></div><div class="t m0 x7f h7 y8b7 ff7 fs3 fc0 sc0 ls0 ws0">55,<span class="_ _1"> </span>57,<span class="_ _10"> </span>59,<span class="_ _1"> </span>62–66,<span class="_ _10"> </span>74,<span class="_ _1"> </span>78,<span class="_ _10"> </span>81–86,<span class="_ _1"> </span>88,</div><div class="t m0 x7f h7 y8b8 ff7 fs3 fc0 sc0 ls0 ws0">89,<span class="_ _9"> </span>97,<span class="_ _3"> </span>102</div><div class="t m0 x7e h5 y8b9 ff2 fs3 fc0 sc0 ls0 ws0">函數語言程式設計<span class="_ _9"> </span><span class="ff7">functional<span class="_ _3"> </span>programming<span class="_ _9"> </span>65,<span class="_ _9"> </span>79</span></div><div class="t m0 x7e h5 y8a8 ff2 fs3 fc0 sc0 ls0 ws0">產生器<span class="_ _9"> </span><span class="ff7">generator<span class="_ _3"> </span>71–75,<span class="_ _9"> </span>79</span></div><div class="t m0 x7e h5 y8a9 ff2 fs3 fc0 sc0 ls0 ws0">產生器表示式<span class="_ _9"> </span><span class="ff7">generator<span class="_ _3"> </span>expression<span class="_ _9"> </span>69–75</span></div><div class="t m0 x7e h5 y8aa ff2 fs3 fc0 sc0 ls0 ws0">產生器函式<span class="_ _9"> </span><span class="ff7">generator<span class="_ _3"> </span>function<span class="_ _9"> </span>71,<span class="_ _9"> </span>74,<span class="_ _3"> </span>75</span></div><div class="t m0 x7e h5 y8ab ff2 fs3 fc0 sc0 ls0 ws0">分組<span class="_ _9"> </span><span class="ff7">group<span class="_ _3"> </span>1,<span class="_ _9"> </span>93,<span class="_ _9"> </span>94,<span class="_ _3"> </span>98</span></div><div class="t m0 x7e h5 y8ba ff2 fs3 fc0 sc0 ls0 ws0">雜湊表<span class="_ _9"> </span><span class="ff7">hash<span class="_ _3"> </span>table<span class="_ _9"> </span>40</span></div><div class="t m0 x7e h5 y172 ff2 fs3 fc0 sc0 ls0 ws0">身份運算子<span class="_ _9"> </span><span class="ff7">identit<span class="_ _8"></span>y<span class="_ _3"> </span>operator<span class="_ _3"> </span>24</span></div><div class="t m0 x7e h5 y173 ff2 fs3 fc0 sc0 ls0 ws0">虛部<span class="_ _9"> </span><span class="ff7">imaginary<span class="_ _3"> </span>part<span class="_ _9"> </span>18,<span class="_ _9"> </span>30</span></div><div class="t m0 x7e h5 y174 ff2 fs3 fc0 sc0 ls0 ws0">不可變<span class="_ _9"> </span><span class="ff7">immutable<span class="_ _9"> </span>16,<span class="_ _9"> </span>39</span></div><div class="t m0 x7e h5 y175 ff2 fs3 fc0 sc0 ls0 ws0">縮排<span class="_ _9"> </span><span class="ff7">indentation<span class="_ _9"> </span>11</span></div><div class="t m0 x7e h5 y176 ff2 fs3 fc0 sc0 ls0 ws0">索引<span class="_ _72"> </span><span class="ff7">index<span class="_ _72"> </span>36–38,<span class="_ _1"> </span>40,<span class="_ _1"> </span>63,<span class="_ _72"> </span>64,<span class="_ _1"> </span>68,<span class="_ _1"> </span>79,<span class="_ _1"> </span>84,<span class="_ _1"> </span>87,</span></div><div class="t m0 x7f h7 y98 ff7 fs3 fc0 sc0 ls0 ws0">103</div><div class="t m0 x7e h5 y99 ff2 fs3 fc0 sc0 ls0 ws0">整數<span class="_ _9"> </span><span class="ff7">integer<span class="_ _9"> </span>18,<span class="_ _9"> </span>20,<span class="_ _3"> </span>23,<span class="_ _9"> </span>27,<span class="_ _9"> </span>28,<span class="_ _3"> </span>38,<span class="_ _9"> </span>56,<span class="_ _3"> </span>68</span></div><div class="t m0 x7e h5 y9a ff2 fs3 fc0 sc0 ls0 ws0">直譯型<span class="_ _9"> </span><span class="ff7">interpreted<span class="_ _9"> </span>5,<span class="_ _9"> </span>7</span></div><div class="t m0 x7e h5 y40 ff2 fs3 fc0 sc0 ls0 ws0">直譯器<span class="_ _9"> </span><span class="ff7">interpreter<span class="_ _9"> </span>2,<span class="_ _9"> </span>3,<span class="_ _3"> </span>5,<span class="_ _9"> </span>6,<span class="_ _9"> </span>10,<span class="_ _3"> </span>61</span></div><div class="t m0 x7b h7 y20 ff7 fs3 fc0 sc0 ls0 ws0">105</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf71" class="pf w0 h0" data-page-no="71"><div class="pc pc71 w0 h0"><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">106<span class="_ _99"> </span><span class="ff6">中英文術語對照表</span></div><div class="t m0 x5 h5 y42 ff2 fs3 fc0 sc0 ls0 ws0">迭代器<span class="_ _9"> </span><span class="ff7">iterator<span class="_ _3"> </span>44,<span class="_ _9"> </span>61,<span class="_ _9"> </span>63–66,<span class="_ _3"> </span>71,<span class="_ _9"> </span>79</span></div><div class="t m0 x5 h5 y77 ff2 fs3 fc0 sc0 ls0 ws0">迭代器介面<span class="_ _9"> </span><span class="ff7">iterator<span class="_ _3"> </span>in<span class="_ _8"></span>terface<span class="_ _3"> </span>61,<span class="_ _9"> </span>62</span></div><div class="t m0 x5 h5 y8bb ff2 fs3 fc0 sc0 ls0 ws0">鍵值對<span class="_ _9"> </span><span class="ff7">key-v<span class="_ _8"></span>alue<span class="_ _9"> </span>pair<span class="_ _9"> </span>40</span></div><div class="t m0 x5 h5 y8bc ff2 fs3 fc0 sc0 ls0 ws0">關鍵字形式參數<span class="_ _9"> </span><span class="ff7">keyw<span class="_ _8"></span>ord<span class="_ _3"> </span>argumen<span class="_ _8"></span>t<span class="_ _3"> </span>47</span></div><div class="t m0 x5 h5 y8bd ff2 fs3 fc0 sc0 ls0 ws0">函式庫<span class="_ _9"> </span><span class="ff7">library<span class="_ _3"> </span>2,<span class="_ _9"> </span>7,<span class="_ _9"> </span>20,<span class="_ _3"> </span>63,<span class="_ _9"> </span>77,<span class="_ _3"> </span>79,<span class="_ _9"> </span>97–102</span></div><div class="t m0 x5 h5 y8be ff2 fs3 fc0 sc0 ls0 ws0">線性代數<span class="_ _9"> </span><span class="ff7">linear<span class="_ _3"> </span>algebra<span class="_ _9"> </span>20,<span class="_ _9"> </span>95,<span class="_ _3"> </span>96,<span class="_ _9"> </span>99</span></div><div class="t m0 x5 h5 y8bf ff2 fs3 fc0 sc0 ls0 ws0">列表<span class="_ _13"> </span><span class="ff7">list<span class="_ _a"> </span>12,<span class="_ _13"> </span>16,<span class="_ _13"> </span>17,<span class="_ _13"> </span>21,<span class="_ _13"> </span>25,<span class="_ _13"> </span>35–41,<span class="_ _13"> </span>48–50,<span class="_ _13"> </span>54,</span></div><div class="t m0 x7d h7 y8c0 ff7 fs3 fc0 sc0 ls0 ws0">61–64,<span class="_ _9"> </span>66–68,<span class="_ _3"> </span>71–75,<span class="_ _9"> </span>85,<span class="_ _9"> </span>88,<span class="_ _3"> </span>94–96</div><div class="t m0 x5 h5 y8c1 ff2 fs3 fc0 sc0 ls0 ws0">列表推導<span class="_ _9"> </span><span class="ff7">list<span class="_ _3"> </span>comprehension<span class="_ _9"> </span>67–71,<span class="_ _9"> </span>74,<span class="_ _3"> </span>95</span></div><div class="t m0 x5 h5 y8c2 ff2 fs3 fc0 sc0 ls0 ws0">迴圈語句<span class="_ _9"> </span><span class="ff7">loop<span class="_ _3"> </span>statemen<span class="_ _8"></span>t<span class="_ _3"> </span>11,<span class="_ _9"> </span>23,<span class="_ _3"> </span>43,<span class="_ _9"> </span>67,<span class="_ _9"> </span>68</span></div><div class="t m0 x5 h5 y8c3 ff2 fs3 fc0 sc0 ls0 ws0">矩陣乘法<span class="_ _9"> </span><span class="ff7">matrix<span class="_ _3"> </span>product<span class="_ _3"> </span>20,<span class="_ _9"> </span>96</span></div><div class="t m0 x5 h5 y8c4 ff2 fs3 fc0 sc0 ls0 ws0">成員運算子<span class="_ _9"> </span><span class="ff7">membership<span class="_ _3"> </span>operator<span class="_ _3"> </span>24</span></div><div class="t m0 x5 h5 y8c5 ff2 fs3 fc0 sc0 ls0 ws0">元資料<span class="_ _9"> </span><span class="ff7">metadata<span class="_ _3"> </span>17</span></div><div class="t m0 x5 h5 y8c6 ff2 fs3 fc0 sc0 ls0 ws0">方法<span class="_ _9"> </span><span class="ff7">method<span class="_ _3"> </span>1,<span class="_ _9"> </span>17,<span class="_ _9"> </span>18,<span class="_ _3"> </span>30,<span class="_ _9"> </span>35–41,<span class="_ _3"> </span>95,<span class="_ _9"> </span>99</span></div><div class="t m0 x5 h5 y8c7 ff2 fs3 fc0 sc0 ls0 ws0">可變<span class="_ _9"> </span><span class="ff7">mutable<span class="_ _9"> </span>16,<span class="_ _9"> </span>21,<span class="_ _3"> </span>35</span></div><div class="t m0 x5 h5 y8c8 ff2 fs3 fc0 sc0 ls0 ws0">命名<span class="_ _9"> </span><span class="ff7">name<span class="_ _3"> </span>94</span></div><div class="t m0 x5 h5 y8c9 ff2 fs3 fc0 sc0 ls0 ws0">名稱空間<span class="_ _9"> </span><span class="ff7">namespace<span class="_ _3"> </span>77,<span class="_ _9"> </span>78</span></div><div class="t m0 x5 h5 y8ca ff2 fs3 fc0 sc0 ls0 ws0">數值積分<span class="_ _9"> </span><span class="ff7">numerical<span class="_ _9"> </span>in<span class="_ _4"></span>tegration<span class="_ _9"> </span>1,<span class="_ _3"> </span>99</span></div><div class="t m0 x5 h5 y8cb ff2 fs3 fc0 sc0 ls0 ws0">數值插值<span class="_ _9"> </span><span class="ff7">numerical<span class="_ _9"> </span>in<span class="_ _4"></span>terpolation<span class="_ _9"> </span>1,<span class="_ _3"> </span>99</span></div><div class="t m0 x5 h5 y8cc ff2 fs3 fc0 sc0 ls0 ws0">函式數值優化<span class="_ _3"> </span><span class="ff7">numerical<span class="_ _9"> </span>optimization<span class="_ _3"> </span>of<span class="_ _3"> </span>func-</span></div><div class="t m0 x7d h7 y8cd ff7 fs3 fc0 sc0 ls0 ws0">tions<span class="_ _9"> </span>99</div><div class="t m0 x5 h5 y8ce ff2 fs3 fc0 sc0 ls0 ws0">物件<span class="_ _9"> </span><span class="ff7">object<span class="_ _9"> </span>1,<span class="_ _9"> </span>15–19,<span class="_ _9"> </span>21–24,<span class="_ _9"> </span>27,<span class="_ _9"> </span>36,<span class="_ _9"> </span>39,<span class="_ _9"> </span>41,<span class="_ _9"> </span>44,</span></div><div class="t m0 x7d h7 y8cf ff7 fs3 fc0 sc0 ls0 ws0">48,<span class="_ _9"> </span>50,<span class="_ _3"> </span>61–64,<span class="_ _9"> </span>66,<span class="_ _9"> </span>67,<span class="_ _3"> </span>70,<span class="_ _9"> </span>79,<span class="_ _3"> </span>86,<span class="_ _9"> </span>97</div><div class="t m0 x5 h5 y8d0 ff2 fs3 fc0 sc0 ls0 ws0">物件相等<span class="_ _9"> </span><span class="ff7">object<span class="_ _3"> </span>equalit<span class="_ _8"></span>y<span class="_ _3"> </span>24</span></div><div class="t m0 x5 h5 y8d1 ff2 fs3 fc0 sc0 ls0 ws0">物件身份<span class="_ _9"> </span><span class="ff7">object<span class="_ _3"> </span>iden<span class="_ _8"></span>tity<span class="_ _9"> </span>24</span></div><div class="t m0 x5 h5 y8d2 ff2 fs3 fc0 sc0 ls0 ws0">物件導向<span class="_ _9"> </span><span class="ff7">object-oriented<span class="_ _9"> </span>17</span></div><div class="t m0 x5 h5 y8d3 ff2 fs3 fc0 sc0 ls0 ws0">運算子<span class="_ _9"> </span><span class="ff7">operator<span class="_ _3"> </span>12,<span class="_ _9"> </span>19–23,<span class="_ _9"> </span>28,<span class="_ _3"> </span>41,<span class="_ _9"> </span>101</span></div><div class="t m0 x5 h5 y8d4 ff2 fs3 fc0 sc0 ls0 ws0">套件<span class="_ _9"> </span><span class="ff7">package<span class="_ _9"> </span>1,<span class="_ _9"> </span>2,<span class="_ _3"> </span>38,<span class="_ _9"> </span>77,<span class="_ _9"> </span>79,<span class="_ _3"> </span>95,<span class="_ _9"> </span>98,<span class="_ _3"> </span>100,<span class="_ _9"> </span>102</span></div><div class="t m0 x5 h5 y8d5 ff2 fs3 fc0 sc0 ls0 ws0">模式匹配</div><div class="t m0 x6a h7 y8d6 ff7 fs3 fc0 sc0 ls0 ws0">pattern<span class="_ _9"> </span>matching<span class="_ _9"> </span>88</div><div class="t m0 x5 h5 y8d7 ff2 fs3 fc0 sc0 ls0 ws0">指標<span class="_ _9"> </span><span class="ff7">pointer<span class="_ _9"> </span>15–17,<span class="_ _9"> </span>24</span></div><div class="t m0 x5 h5 y8d8 ff2 fs3 fc0 sc0 ls0 ws0">原始字串<span class="_ _9"> </span><span class="ff7">raw<span class="_ _9"> </span>string<span class="_ _9"> </span>91</span></div><div class="t m0 x5 h5 y8d9 ff2 fs3 fc0 sc0 ls0 ws0">實部<span class="_ _9"> </span><span class="ff7">real<span class="_ _3"> </span>part<span class="_ _9"> </span>18,<span class="_ _9"> </span>30</span></div><div class="t m0 x5 h5 y8da ff2 fs3 fc0 sc0 ls0 ws0">引用<span class="_ _9"> </span><span class="ff7">reference<span class="_ _3"> </span>15</span></div><div class="t m0 x7e h5 y42 ff2 fs3 fc0 sc0 ls0 ws0">正則表示式<span class="_ _9"> </span><span class="ff7">regular<span class="_ _3"> </span>expression<span class="_ _9"> </span>81,<span class="_ _9"> </span>85,<span class="_ _3"> </span>88–94</span></div><div class="t m0 x7e h5 y8db ff2 fs3 fc0 sc0 ls0 ws0">表示<span class="_ _9"> </span><span class="ff7">representation<span class="_ _9"> </span>28–30,<span class="_ _9"> </span>86,<span class="_ _3"> </span>96</span></div><div class="t m0 x7e h5 y8dc ff2 fs3 fc0 sc0 ls0 ws0">執行時錯誤<span class="_ _9"> </span><span class="ff7">runtime<span class="_ _9"> </span>error<span class="_ _9"> </span>53</span></div><div class="t m0 x7e h5 y8dd ff2 fs3 fc0 sc0 ls0 ws0">自我包含指令碼<span class="_ _9"> </span><span class="ff7">self-contained<span class="_ _9"> </span>script<span class="_ _9"> </span>5,<span class="_ _3"> </span>7</span></div><div class="t m0 x7e h5 y8de ff2 fs3 fc0 sc0 ls0 ws0">語意錯誤<span class="_ _9"> </span><span class="ff7">semantic<span class="_ _9"> </span>error<span class="_ _9"> </span>53</span></div><div class="t m0 x7e h5 y8df ff2 fs3 fc0 sc0 ls0 ws0">語意<span class="_ _9"> </span><span class="ff7">semantics<span class="_ _9"> </span>1,<span class="_ _9"> </span>9,<span class="_ _3"> </span>15,<span class="_ _9"> </span>101</span></div><div class="t m0 x7e h5 y8e0 ff2 fs3 fc0 sc0 ls0 ws0">集合<span class="_ _9"> </span><span class="ff7">set<span class="_ _3"> </span>41,<span class="_ _9"> </span>66,<span class="_ _9"> </span>69,<span class="_ _3"> </span>92</span></div><div class="t m0 x7e h5 y8e1 ff2 fs3 fc0 sc0 ls0 ws0">集合推導<span class="_ _9"> </span><span class="ff7">set<span class="_ _3"> </span>comprehension<span class="_ _9"> </span>69</span></div><div class="t m0 x7e h5 y8e2 ff2 fs3 fc0 sc0 ls0 ws0">愛拉託遜斯篩法<span class="_ _3"> </span><span class="ff7">Sieve<span class="_ _3"> </span>of<span class="_ _3"> </span>Eratosthenes<span class="_ _a"> </span>46,<span class="_ _a"> </span>72,</span></div><div class="t m0 x7f h7 y8e3 ff7 fs3 fc0 sc0 ls0 ws0">74,<span class="_ _9"> </span>75</div><div class="t m0 x7e h5 y8e4 ff2 fs3 fc0 sc0 ls0 ws0">簡單型別<span class="_ _9"> </span><span class="ff7">simple<span class="_ _3"> </span>t<span class="_ _8"></span>ype<span class="_ _3"> </span>16,<span class="_ _9"> </span>17,<span class="_ _3"> </span>27</span></div><div class="t m0 x7e h5 y8e5 ff2 fs3 fc0 sc0 ls0 ws0">切片<span class="_ _9"> </span><span class="ff7">slice<span class="_ _3"> </span>36–38,<span class="_ _9"> </span>40,<span class="_ _9"> </span>44</span></div><div class="t m0 x7e h5 y8e6 ff2 fs3 fc0 sc0 ls0 ws0">稀疏矩陣儲存<span class="_ _9"> </span><span class="ff7">sparse<span class="_ _3"> </span>matrix<span class="_ _9"> </span>storage<span class="_ _9"> </span>99</span></div><div class="t m0 x7e h5 y8e7 ff2 fs3 fc0 sc0 ls0 ws0">靜態型別<span class="_ _9"> </span><span class="ff7">statically<span class="_ _3"> </span>t<span class="_ _8"></span>yped<span class="_ _3"> </span>16,<span class="_ _9"> </span>28,<span class="_ _3"> </span>36</span></div><div class="t m0 x7e h5 y8e8 ff2 fs3 fc0 sc0 ls0 ws0">統計分析<span class="_ _9"> </span><span class="ff7">statistical<span class="_ _3"> </span>analysis<span class="_ _9"> </span>99</span></div><div class="t m0 x7e h5 y8e9 ff2 fs3 fc0 sc0 ls0 ws0">字串內插<span class="_ _9"> </span><span class="ff7">string<span class="_ _3"> </span>in<span class="_ _8"></span>terpolation<span class="_ _3"> </span>87</span></div><div class="t m0 x7e h5 y8ea ff2 fs3 fc0 sc0 ls0 ws0">對等差分<span class="_ _9"> </span><span class="ff7">symmetric<span class="_ _3"> </span>difference<span class="_ _9"> </span>41</span></div><div class="t m0 x7e h5 y8eb ff2 fs3 fc0 sc0 ls0 ws0">語法<span class="_ _13"> </span><span class="ff7">syn<span class="_ _4"></span>tax<span class="_ _a"> </span>1,<span class="_ _13"> </span>2,<span class="_ _a"> </span>9–11,<span class="_ _13"> </span>13,<span class="_ _13"> </span>15,<span class="_ _13"> </span>19,<span class="_ _13"> </span>36–38,<span class="_ _13"> </span>44,</span></div><div class="t m0 x7f h7 y8ec ff7 fs3 fc0 sc0 ls0 ws0">46,<span class="_ _72"> </span>49–51,<span class="_ _72"> </span>53,<span class="_ _1"> </span>58,<span class="_ _72"> </span>61,<span class="_ _1"> </span>63,<span class="_ _72"> </span>64,<span class="_ _1"> </span>67,<span class="_ _72"> </span>69,</div><div class="t m0 x7f h7 y8ed ff7 fs3 fc0 sc0 ls0 ws0">75,<span class="_ _9"> </span>87–90,<span class="_ _3"> </span>92,<span class="_ _9"> </span>94,<span class="_ _9"> </span>95,<span class="_ _3"> </span>101</div><div class="t m0 x7e h5 y8ee ff2 fs3 fc0 sc0 ls0 ws0">語法錯誤<span class="_ _9"> </span><span class="ff7">syntax<span class="_ _9"> </span>error<span class="_ _9"> </span>53</span></div><div class="t m0 x7e h5 y8ef ff2 fs3 fc0 sc0 ls0 ws0">表格<span class="_ _9"> </span><span class="ff7">table<span class="_ _3"> </span>94</span></div><div class="t m0 x7e h5 y8f0 ff2 fs3 fc0 sc0 ls0 ws0">終端<span class="_ _9"> </span><span class="ff7">terminal<span class="_ _3"> </span>5</span></div><div class="t m0 x7e h5 y8f1 ff2 fs3 fc0 sc0 ls0 ws0">三元運算子<span class="_ _9"> </span><span class="ff7">ternary<span class="_ _3"> </span>operator<span class="_ _3"> </span>68</span></div><div class="t m0 x7e h5 y8f2 ff2 fs3 fc0 sc0 ls0 ws0">轉置矩陣<span class="_ _9"> </span><span class="ff7">transpose<span class="_ _3"> </span>matrix<span class="_ _9"> </span>96</span></div><div class="t m0 x7e h5 y8f3 ff2 fs3 fc0 sc0 ls0 ws0">真除<span class="_ _9"> </span><span class="ff7">true<span class="_ _3"> </span>division<span class="_ _9"> </span>19,<span class="_ _9"> </span>20</span></div><div class="t m0 x7e h5 y8f4 ff2 fs3 fc0 sc0 ls0 ws0">Tuple<span class="_ _9"> </span><span class="ff7">tuple<span class="_ _3"> </span>38–42,<span class="_ _9"> </span>48,<span class="_ _9"> </span>85</span></div><div class="t m0 x7e h5 y8f5 ff2 fs3 fc0 sc0 ls0 ws0">補碼<span class="_ _9"> </span><span class="ff7">tw<span class="_ _8"></span>o’s<span class="_ _3"> </span>complemen<span class="_ _8"></span>t<span class="_ _3"> </span>23</span></div><div class="t m0 x7e h5 y8f6 ff2 fs3 fc0 sc0 ls0 ws0">型別轉換<span class="_ _9"> </span><span class="ff7">type<span class="_ _3"> </span>con<span class="_ _8"></span>version<span class="_ _9"> </span>32</span></div><div class="t m0 x7e h5 y8f7 ff2 fs3 fc0 sc0 ls0 ws0">一元運算子<span class="_ _9"> </span><span class="ff7">unary<span class="_ _3"> </span>operator<span class="_ _3"> </span>12,<span class="_ _9"> </span>19</span></div><div class="t m0 x7e h5 y8f8 ff2 fs3 fc0 sc0 ls0 ws0">變數<span class="_ _a"> </span><span class="ff7">v<span class="_ _8"></span>ariable<span class="_ _a"> </span>5,<span class="_ _13"> </span>10,<span class="_ _a"> </span>12,<span class="_ _13"> </span>15–17,<span class="_ _13"> </span>19,<span class="_ _a"> </span>21,<span class="_ _13"> </span>24,<span class="_ _a"> </span>27,</span></div><div class="t m0 x7f h7 y8f9 ff7 fs3 fc0 sc0 ls0 ws0">32,<span class="_ _9"> </span>36,<span class="_ _3"> </span>44,<span class="_ _9"> </span>49,<span class="_ _9"> </span>53,<span class="_ _3"> </span>67</div><div class="t m0 x7e h5 y8fa ff2 fs3 fc0 sc0 ls0 ws0">可變精度<span class="_ _9"> </span><span class="ff7">v<span class="_ _8"></span>ariable-precision<span class="_ _3"> </span>27</span></div><div class="t m0 x7e h5 y8da ff2 fs3 fc0 sc0 ls0 ws0">萬用字元<span class="_ _9"> </span><span class="ff7">wildcard<span class="_ _3"> </span>88,<span class="_ _9"> </span>92</span></div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf72" class="pf w0 h0" data-page-no="72"><div class="pc pc72 w0 h0"><div class="t m0 x73 h1 y3 ff2 fs0 fc0 sc0 ls0 ws0">同濟大學蘋果開發俱樂部介紹</div><div class="t m0 x4 h4 y204 ff5 fs3 fc0 sc0 ls0 ws0">同濟大學蘋果俱樂部成立於<span class="_ _13"> </span><span class="ff7">2003<span class="_ _13"> </span></span>年<span class="_ _a"> </span><span class="ff7">9<span class="_ _13"> </span></span>月<span class="_ _13"> </span><span class="ff7">24<span class="_ _13"> </span></span>日,是亞太地區第一個和<span class="_ _13"> </span><span class="ff7">Apple<span class="_ _a"> </span></span>公司合作</div><div class="t m0 x5 h4 y205 ff5 fs3 fc0 sc0 ls0 ws0">的學生俱樂部,旨在傳播<span class="_ _13"> </span><span class="ff7">Apple<span class="_ _13"> </span></span>的先進技術以及培養學生的創新意識,進行<span class="_ _13"> </span><span class="ff7">macOS</span>、<span class="ff7">iOS</span>、</div><div class="t m0 x5 h4 y206 ff7 fs3 fc0 sc0 ls0 ws0">w<span class="_ _4"></span>atc<span class="_ _4"></span>hOS<span class="ff5">、</span>tvOS<span class="_ _9"> </span><span class="ff5">等平台的軟體開發。</span></div><div class="t m0 x4 h4 y6a9 ff5 fs3 fc0 sc0 ls0 ws0">自俱樂部成立以來,軟體學院、<span class="ff7">Apple<span class="_ _3"> </span></span>公司和廣大學生通力合作,積極努力。在俱樂部中,</div><div class="t m0 x5 h4 y6aa ff5 fs3 fc0 sc0 ls0 ws0">學生任何創造性的想法都會被珍視、鼓勵、去蕪存菁並在指導下實施,俱樂部成為了來自不同</div><div class="t m0 x5 h4 y6ab ff5 fs3 fc0 sc0 ls0 ws0">院系學生髮揮創造力的舞臺。在十四年的發展中,經過幾任指導教師和廣大同學的奮鬥,俱樂</div><div class="t m0 x5 h4 y8fb ff5 fs3 fc0 sc0 ls0 ws0">部取得了豐碩的成果,極大地促進了學生的實踐與創新能力。</div><div class="t m0 x4 h4 y8fc ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff5">國內第一本蘋果軟體開發課程《蘋果電腦開發技術基礎課程》由機械工業出版社出版,</span></div><div class="t m0 xd h4 y8fd ff5 fs3 fc0 sc0 ls0 ws0">該教材完全由俱樂部的學生編寫。</div><div class="t m0 x4 h4 y8fe ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff5">國內第一門蘋果軟體開發類別課程《<span class="ff7">Apple<span class="_ _9"> </span></span>平台軟體開發》在同濟大學開設。</span></div><div class="t m0 x4 h4 y8ff ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff5">國內第一個學生開發的開源,且受到蘋果總部高層讚賞的免費軟體。</span></div><div class="t m0 x4 h4 y900 ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff5">國內第一批赴美國參加蘋果全球開發者大會(<span class="ff7">WWDC</span>)的學生開發者。</span></div><div class="t m0 x4 h4 y901 ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff5">國內第一個獲得蘋果平台學生開發競賽所有重要獎項的俱樂部。</span></div><div class="t m0 x4 h4 y902 ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff5">國內第一個學生開發者受邀到蘋果全球開發者大會(<span class="ff7">WWDC</span>)的專門環節做技術演講</span></div><div class="t m0 xd h4 y903 ff5 fs3 fc0 sc0 ls0 ws0">和展示。</div><div class="t m0 x4 h4 y904 ffb fs3 fc0 sc0 ls0 ws0">•<span class="_ _10"> </span><span class="ff5">國內第一個衝擊蘋果年度全球軟體設計最高榮譽——蘋果設計大獎的學生作品。</span></div><div class="t m0 x4 h4 y344 ff5 fs3 fc0 sc0 ls0 ws0">在<span class="_ _3"> </span><span class="ff7">Apple<span class="_ _a"> </span></span>公司舉辦的<span class="_ _a"> </span><span class="ff7">6<span class="_ _3"> </span></span>次蘋果中國高校學生作品大賽中,同濟大學蘋果俱樂部共計獲得</div><div class="t m0 x5 h4 y345 ff5 fs3 fc0 sc0 ls0 ws0">了<span class="_ _a"> </span><span class="ff7">5<span class="_ _a"> </span></span>項金獎,<span class="ff7">1<span class="_ _13"> </span></span>項特別獎,<span class="ff7">16<span class="_ _a"> </span></span>項銀獎,<span class="ff7">7<span class="_ _3"> </span></span>項銅獎的好成績,獲得了該項賽事“五連冠”的記</div><div class="t m0 x5 h4 y905 ff5 fs3 fc0 sc0 ls0 ws0">錄。在<span class="_ _a"> </span><span class="ff7">2016<span class="_ _3"> </span></span>年“中國高校計算機大賽<span class="ff7">-</span>移動應用創新賽”中,俱樂部的學生再次獲得一等獎。</div><div class="t m0 x5 h4 y346 ff5 fs3 fc0 sc0 ls0 ws0">從<span class="_ _9"> </span><span class="ff7">2005<span class="_ _3"> </span></span>年開始,同濟大學蘋果俱樂部每年均有十餘名師生受邀前往參加在<span class="_ _9"> </span><span class="ff7">San<span class="_ _3"> </span>F<span class="_ _2"></span>rancisco<span class="_ _9"> </span><span class="ff5">舉辦</span></span></div><div class="t m0 x5 h4 y4ee ff5 fs3 fc0 sc0 ls0 ws0">的<span class="_ _9"> </span><span class="ff7">WWDC</span>(<span class="ff7">W<span class="_ _8"></span>orld<span class="_ _9"> </span>Wide<span class="_ _9"> </span>Developmen<span class="_ _8"></span>t<span class="_ _3"> </span>Conference<span class="ff5">)活動,成為第一批參與該活動的中國開發</span></span></div><div class="t m0 x5 h4 y172 ff5 fs3 fc0 sc0 ls0 ws0">者。在<span class="_ _3"> </span><span class="ff7">2005<span class="_ _3"> </span></span>年<span class="_ _3"> </span><span class="ff7">WWDC<span class="_ _3"> </span></span>上,<span class="ff7">Steve<span class="_ _3"> </span>Jobs<span class="_ _3"> </span></span>特別在演講中歡迎了來自中國的俱樂部開發者,同濟</div><div class="t m0 x5 h4 y173 ff5 fs3 fc0 sc0 ls0 ws0">大學蘋果俱樂部的師生也成為<span class="_ _3"> </span><span class="ff7">WWDC<span class="_ _3"> </span></span>上來自中國的一道靚麗的風景線。從<span class="_ _3"> </span><span class="ff7">2009<span class="_ _3"> </span></span>年開始,蘋</div><div class="t m0 x5 h4 y174 ff5 fs3 fc0 sc0 ls0 ws0">果俱樂部的學生作品紛紛進入<span class="_ _a"> </span><span class="ff7">App<span class="_ _a"> </span>Store<span class="_ _a"> </span></span>進行銷售。俱樂部學生完成的<span class="_ _3"> </span><span class="ff7">Animal<span class="_ _a"> </span>Kingdom<span class="_ _a"> </span></span>作</div><div class="t m0 x5 h4 y175 ff5 fs3 fc0 sc0 ls0 ws0">為蘋果中國官方廣告的作品,受到了中國用戶的一致好評。目前蘋果俱樂部已經有<span class="_ _3"> </span><span class="ff7">60<span class="_ _a"> </span></span>餘個作</div><div class="t m0 x5 h4 y176 ff5 fs3 fc0 sc0 ls0 ws0">品在<span class="_ _3"> </span><span class="ff7">App<span class="_ _3"> </span>Store<span class="_ _3"> </span></span>進行銷售,累計下載次數超過<span class="_ _3"> </span><span class="ff7">5,000,000<span class="_ _3"> </span></span>次,下載使用者遍布中國、美國、加拿</div><div class="t m0 x5 h4 y98 ff5 fs3 fc0 sc0 ls0 ws0">大、英國、法國、日本等多個國家和地區,作品受到了中國、日本、美國多家風險投資和代理</div><div class="t m0 x5 h4 y99 ff5 fs3 fc0 sc0 ls0 ws0">商的關注。蘋果俱樂部的學生也獲得了用人單位的一致好評,俱樂部的歷屆畢業生在<span class="_ _a"> </span><span class="ff7">Apple</span>、</div><div class="t m0 x5 h4 y9a ff7 fs3 fc0 sc0 ls0 ws0">F<span class="_ _2"></span>acebook<span class="ff5">、<span class="_ _8"></span><span class="ff7">IBM<span class="ff5">、<span class="_ _2"></span><span class="ff7">Microsoft<span class="ff5">、<span class="_ _8"></span><span class="ff7">Google<span class="ff5">、<span class="_ _8"></span><span class="ff7">A<span class="_ _8"></span>utodesk<span class="ff5">、<span class="_ _2"></span><span class="ff7">SAP<span class="ff5">、<span class="_ _8"></span><span class="ff7">K<span class="_ _8"></span>odak<span class="ff5">、<span class="_ _8"></span><span class="ff7">EA<span class="ff5">、<span class="_ _2"></span><span class="ff7">Gameloft<span class="_ _9"> </span><span class="ff5">等多家企業在</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></div><div class="t m0 x5 h4 y40 ff5 fs3 fc0 sc0 ls0 ws0">美國、中國從事蘋果及其相關產品開發,並有多名學生自主創業。</div><div class="t m0 x7b h7 y20 ff7 fs3 fc0 sc0 ls0 ws0">107</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
<div id="pf73" class="pf w0 h0" data-page-no="73"><div class="pc pc73 w0 h0"><div class="t m0 x5 h6 y41 ff7 fs3 fc0 sc0 ls0 ws0">108<span class="_ _9a"> </span><span class="ff6">同濟大學蘋果開發俱樂部介紹</span></div><div class="t m0 x4 h4 y42 ff5 fs3 fc0 sc0 ls0 ws0">十多年來,<span class="_ _40"></span>同濟大學蘋果俱樂部獲得了<span class="_ _35"></span>“蘋果傑出貢獻獎”<span class="_ _9b"></span>“蘋果先驅俱樂部獎”<span class="_ _9b"></span>“蘋果優秀</div><div class="t m0 x5 h4 y77 ff5 fs3 fc0 sc0 ls0 ws0">俱樂部獎”<span class="_ _9c"></span>“蘋果傑出實驗室獎”<span class="_ _43"></span>等榮譽;<span class="_ _43"></span>指導教師先後獲得了<span class="_ _42"></span>“蘋果傑出教育專家<span class="_ _2"></span>(<span class="ff7">ADE</span>)<span class="_ _33"></span>”<span class="_ _9c"></span>“蘋</div><div class="t m0 x5 h4 y78 ff5 fs3 fc0 sc0 ls0 ws0">果傑出教師獎”等獎項。</div><div class="t m0 x4 h4 y262 ff5 fs3 fc0 sc0 ls0 ws0">俱樂部也獲得了廣大媒體的多方報道,接受了<span class="_ _a"> </span><span class="ff7">CCTV</span>、新浪網、搜狐網、網易、<span class="ff7">CSDN</span>、</div><div class="t m0 x5 h4 y883 ff7 fs3 fc0 sc0 ls0 ws0">T<span class="_ _2"></span>encent<span class="_ _9"> </span><span class="ff5">等多個媒體的採訪,同時也接受了包括《第一財經週刊》<span class="_ _33"></span>《解放日報》等在內的多個平</span></div><div class="t m0 x5 h4 y884 ff5 fs3 fc0 sc0 ls0 ws0">面媒體的採訪,登上了<span class="_ _3"> </span><span class="ff7">2<span class="_ _3"> </span></span>期《第一財經週刊》封面故事。同時作為高校與跨國<span class="_ _3"> </span><span class="ff7">IT<span class="_ _3"> </span></span>企業的成功</div><div class="t m0 x5 h4 y885 ff5 fs3 fc0 sc0 ls0 ws0">合作案例,<span class="_ _8"></span>入選教育部組織編寫的<span class="_ _4"></span>《中國高校與大型企業合作經典案例集》<span class="_ _33"></span>。<span class="_ _8"></span>全國政協副主席董</div><div class="t m0 x5 h4 y906 ff5 fs3 fc0 sc0 ls0 ws0">建華、科技部部長萬鋼、教育部副部長吳啟迪、蘋果公司全球高階副總裁<span class="_ _a"> </span><span class="ff7">Philip<span class="_ _3"> </span>W.<span class="_ _a"> </span>Schiller</span>、</div><div class="t m0 x5 h4 y907 ff7 fs3 fc0 sc0 ls0 ws0">Ron<span class="_ _9"> </span>Okamoto <span class="ff5">、法國參眾議員訪華代表團、眾多國內外大學校長教授等均參觀和指導過同濟大</span></div><div class="t m0 x5 h4 y908 ff5 fs3 fc0 sc0 ls0 ws0">學蘋果俱樂部。</div><div class="t m0 x4 h4 y909 ff5 fs3 fc0 sc0 ls0 ws0">同濟大學蘋果俱樂部在十多年的建設過程中,始終堅持以學生為本,以創新為本,堅持以</div><div class="t m0 x5 h4 y90a ff5 fs3 fc0 sc0 ls0 ws0">工程實際需求為導向,和國內外相關企業積極合作,鼓勵學生自主創新,以專業教師進行指導,</div><div class="t m0 x5 h4 y90b ff5 fs3 fc0 sc0 ls0 ws0">為學生創造良好的軟硬件環境,為學生的成長、成才提供有力的保障,體現了同濟大學軟體學</div><div class="t m0 x5 h4 y90c ff5 fs3 fc0 sc0 ls0 ws0">院創新人才培養和前沿化、工程化、國際化,理論基礎和實踐技能協調發展的培養理念。</div></div><div class="pi" data-data='{"ctm":[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000]}'></div></div>
</div>
<div class="loading-indicator">
<img alt="" src="images/pdf2htmlEX-64x64.png"/>
</div>
</body>
</html>