forked from sixu05202004/pythontutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
introduction.html
587 lines (575 loc) · 52.9 KB
/
introduction.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>3. Python 简介 — Python tutorial 2.7 documentation</title>
<link rel="stylesheet" href="_static/default.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '2.7',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="Python tutorial 2.7 documentation" href="index.html" />
<link rel="next" title="4. 深入 Python 流程控制" href="controlflow.html" />
<link rel="prev" title="2. 使用 Python 解释器" href="interpreter.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="controlflow.html" title="4. 深入 Python 流程控制"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="interpreter.html" title="2. 使用 Python 解释器"
accesskey="P">previous</a> |</li>
<li><a href="index.html">Python tutorial 2.7 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="python">
<span id="tut-informal"></span><h1>3. Python 简介<a class="headerlink" href="#python" title="Permalink to this headline">¶</a></h1>
<p>下面的例子中,输入和输出分别由大于号和句号提示符( <tt class="docutils literal"><span class="pre">>>></span></tt> 和 <tt class="docutils literal"><span class="pre">...</span></tt> )标注:如果想重现这些例子,就要在解释器的提示符后,输入(提示符后面的)那些不包含提示符的代码行。需要注意的是在练习中遇到的从属提示符表示你需要在最后多输入一个空行,解释器才能知道这是一个多行命令的结束。</p>
<p>本手册中的很多示例——包括那些带有交互提示符的——都含有注释。Python 中的注释以 <tt class="docutils literal"><span class="pre">#</span></tt> 字符起始,直至实际的行尾(译注——这里原作者用了 physical line 以表示实际的换行而非编辑器的自动换行)。注释可以从行首开始,也可以在空白或代码之后,但是不出现在字符串中。文本字符串中的 <tt class="docutils literal"><span class="pre">#</span></tt> 字符仅仅表示 <tt class="docutils literal"><span class="pre">#</span></tt> 。代码中的注释不会被 Python 解释,录入示例的时候可以忽略它们。</p>
<p>如下示例:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># this is the first comment</span>
<span class="n">SPAM</span> <span class="o">=</span> <span class="mi">1</span> <span class="c"># and this is the second comment</span>
<span class="c"># ... and now a third!</span>
<span class="n">STRING</span> <span class="o">=</span> <span class="s">"# This is not a comment."</span>
</pre></div>
</div>
<div class="section" id="tut-calculator">
<span id="id1"></span><h2>3.1. 将 Python 当做计算器<a class="headerlink" href="#tut-calculator" title="Permalink to this headline">¶</a></h2>
<p>我们来尝试一些简单的 Python 命令。启动解释器然后等待主提示符 <tt class="docutils literal"><span class="pre">>>></span></tt> 出现。(不需要很久。)</p>
<div class="section" id="tut-numbers">
<span id="id2"></span><h3>3.1.1. 数字<a class="headerlink" href="#tut-numbers" title="Permalink to this headline">¶</a></h3>
<p>解释器表现得就像一个简单的计算器:可以向其录入一些表达式,它会给出返回值。表达式语法很直白:运算符 <tt class="docutils literal"><span class="pre">+</span></tt> , <tt class="docutils literal"><span class="pre">-</span></tt> , <tt class="docutils literal"><span class="pre">*</span></tt> 和 <tt class="docutils literal"><span class="pre">/</span></tt> 与其它语言一样(例如: Pascal 或 C);括号用于分组。例如:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="mi">2</span><span class="o">+</span><span class="mi">2</span>
<span class="go">4</span>
<span class="gp">>>> </span><span class="c"># This is a comment</span>
<span class="gp">... </span><span class="mi">2</span><span class="o">+</span><span class="mi">2</span>
<span class="go">4</span>
<span class="gp">>>> </span><span class="mi">2</span><span class="o">+</span><span class="mi">2</span> <span class="c"># and a comment on the same line as code</span>
<span class="go">4</span>
<span class="gp">>>> </span><span class="p">(</span><span class="mi">50</span><span class="o">-</span><span class="mi">5</span><span class="o">*</span><span class="mi">6</span><span class="p">)</span><span class="o">/</span><span class="mi">4</span>
<span class="go">5</span>
<span class="gp">>>> </span><span class="c"># Integer division returns the floor:</span>
<span class="gp">... </span><span class="mi">7</span><span class="o">/</span><span class="mi">3</span>
<span class="go">2</span>
<span class="gp">>>> </span><span class="mi">7</span><span class="o">/-</span><span class="mi">3</span>
<span class="go">-3</span>
</pre></div>
</div>
<p>等号( <tt class="docutils literal"><span class="pre">'='</span></tt> )用于给变量赋值:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">width</span> <span class="o">=</span> <span class="mi">20</span>
<span class="gp">>>> </span><span class="n">height</span> <span class="o">=</span> <span class="mi">5</span><span class="o">*</span><span class="mi">9</span>
<span class="gp">>>> </span><span class="n">width</span> <span class="o">*</span> <span class="n">height</span>
<span class="go">900</span>
</pre></div>
</div>
<p>一个值可以同时赋给几个变量:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">x</span> <span class="o">=</span> <span class="n">y</span> <span class="o">=</span> <span class="n">z</span> <span class="o">=</span> <span class="mi">0</span> <span class="c"># Zero x, y and z</span>
<span class="gp">>>> </span><span class="n">x</span>
<span class="go">0</span>
<span class="gp">>>> </span><span class="n">y</span>
<span class="go">0</span>
<span class="gp">>>> </span><span class="n">z</span>
<span class="go">0</span>
</pre></div>
</div>
<p>变量在使用前必须“定义”(赋值),否则会出错:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="c"># try to access an undefined variable</span>
<span class="gp">... </span><span class="n">n</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></span>
<span class="gr">NameError</span>: <span class="n">name 'n' is not defined</span>
</pre></div>
</div>
<p>浮点数有完整的支持;与整型混合计算时会自动转为浮点数:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="mi">3</span> <span class="o">*</span> <span class="mf">3.75</span> <span class="o">/</span> <span class="mf">1.5</span>
<span class="go">7.5</span>
<span class="gp">>>> </span><span class="mf">7.0</span> <span class="o">/</span> <span class="mi">2</span>
<span class="go">3.5</span>
</pre></div>
</div>
<p>复数也得到支持;带有后缀 <tt class="docutils literal"><span class="pre">j</span></tt> 或 <tt class="docutils literal"><span class="pre">J</span></tt> 就被视为虚数。带有非零实部的复数写为 <tt class="docutils literal"><span class="pre">(real+imagj)</span></tt> ,或者可以用 <tt class="docutils literal"><span class="pre">complex(real,</span> <span class="pre">imag)</span></tt> 函数创建。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="mi">1j</span> <span class="o">*</span> <span class="mi">1</span><span class="n">J</span>
<span class="go">(-1+0j)</span>
<span class="gp">>>> </span><span class="mi">1j</span> <span class="o">*</span> <span class="nb">complex</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">(-1+0j)</span>
<span class="gp">>>> </span><span class="mi">3</span><span class="o">+</span><span class="mi">1j</span><span class="o">*</span><span class="mi">3</span>
<span class="go">(3+3j)</span>
<span class="gp">>>> </span><span class="p">(</span><span class="mi">3</span><span class="o">+</span><span class="mi">1j</span><span class="p">)</span><span class="o">*</span><span class="mi">3</span>
<span class="go">(9+3j)</span>
<span class="gp">>>> </span><span class="p">(</span><span class="mi">1</span><span class="o">+</span><span class="mi">2j</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="mi">1</span><span class="o">+</span><span class="mi">1j</span><span class="p">)</span>
<span class="go">(1.5+0.5j)</span>
</pre></div>
</div>
<p>复数的实部和虚部总是记为两个浮点数。要从复数 z 中提取实部和虚部,使用 <tt class="docutils literal"><span class="pre">z.real</span></tt> 和 <tt class="docutils literal"><span class="pre">z.imag</span></tt> 。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">a</span><span class="o">=</span><span class="mf">1.5</span><span class="o">+</span><span class="mf">0.5j</span>
<span class="gp">>>> </span><span class="n">a</span><span class="o">.</span><span class="n">real</span>
<span class="go">1.5</span>
<span class="gp">>>> </span><span class="n">a</span><span class="o">.</span><span class="n">imag</span>
<span class="go">0.5</span>
</pre></div>
</div>
<p>浮点数和整数之间的转换函数( <tt class="xref py py-func docutils literal"><span class="pre">float()</span></tt> 和 <tt class="xref py py-func docutils literal"><span class="pre">int()</span></tt> 以及 <tt class="xref py py-func docutils literal"><span class="pre">long()</span></tt> ) 不能用于复数。没有什么正确方法可以把一个复数转成一个实数。函数 <tt class="docutils literal"><span class="pre">abs(z)</span></tt> 用于获取其模(浮点数)或 <tt class="docutils literal"><span class="pre">z.real</span></tt> 获取其实部:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">a</span><span class="o">=</span><span class="mf">3.0</span><span class="o">+</span><span class="mf">4.0j</span>
<span class="gp">>>> </span><span class="nb">float</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n">?</span>
<span class="gr">TypeError</span>: <span class="n">can't convert complex to float; use abs(z)</span>
<span class="gp">>>> </span><span class="n">a</span><span class="o">.</span><span class="n">real</span>
<span class="go">3.0</span>
<span class="gp">>>> </span><span class="n">a</span><span class="o">.</span><span class="n">imag</span>
<span class="go">4.0</span>
<span class="gp">>>> </span><span class="nb">abs</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="c"># sqrt(a.real**2 + a.imag**2)</span>
<span class="go">5.0</span>
</pre></div>
</div>
<p>交互模式中,最近一个表达式的值赋给变量 <tt class="docutils literal"><span class="pre">_</span></tt> 。这样我们就可以把它当作一个桌面计算器,很方便的用于连续计算,例如:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">tax</span> <span class="o">=</span> <span class="mf">12.5</span> <span class="o">/</span> <span class="mi">100</span>
<span class="gp">>>> </span><span class="n">price</span> <span class="o">=</span> <span class="mf">100.50</span>
<span class="gp">>>> </span><span class="n">price</span> <span class="o">*</span> <span class="n">tax</span>
<span class="go">12.5625</span>
<span class="gp">>>> </span><span class="n">price</span> <span class="o">+</span> <span class="n">_</span>
<span class="go">113.0625</span>
<span class="gp">>>> </span><span class="nb">round</span><span class="p">(</span><span class="n">_</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">113.06</span>
</pre></div>
</div>
<p>此变量对于用户是只读的。不要尝试给它赋值 —— 你只会创建一个独立的同名局部变量,它屏蔽了系统内置变量的魔术效果。</p>
</div>
<div class="section" id="tut-strings">
<span id="id3"></span><h3>3.1.2. 字符串<a class="headerlink" href="#tut-strings" title="Permalink to this headline">¶</a></h3>
<p>相比数值,Python 也提供了可以通过几种不同方式传递的字符串。它们可以用单引号或双引号标识:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="s">'spam eggs'</span>
<span class="go">'spam eggs'</span>
<span class="gp">>>> </span><span class="s">'doesn</span><span class="se">\'</span><span class="s">t'</span>
<span class="go">"doesn't"</span>
<span class="gp">>>> </span><span class="s">"doesn't"</span>
<span class="go">"doesn't"</span>
<span class="gp">>>> </span><span class="s">'"Yes," he said.'</span>
<span class="go">'"Yes," he said.'</span>
<span class="gp">>>> </span><span class="s">"</span><span class="se">\"</span><span class="s">Yes,</span><span class="se">\"</span><span class="s"> he said."</span>
<span class="go">'"Yes," he said.'</span>
<span class="gp">>>> </span><span class="s">'"Isn</span><span class="se">\'</span><span class="s">t," she said.'</span>
<span class="go">'"Isn\'t," she said.'</span>
</pre></div>
</div>
<p>Python 解释器按照字符串被输入的方式打印字符串结果:为了显示准确的值,字符串包含在成对的引号中,引号和其他特殊字符要用反斜线( \ )转译。 如果字符串只包含单引号( ‘ )而没有双引号( ” )就可以用双引号( ” )包围,反之用单引号( ‘ )包围。 再强调一下, <tt class="xref std std-keyword docutils literal"><span class="pre">print</span></tt> 语句可以生成可读性更好的输出。</p>
<p>字符串文本有几种方法分行。可以使用反斜杠为行结尾的连续字符串,它表示下一行在逻辑上是本行的后续内容:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">hello</span> <span class="o">=</span> <span class="s">"This is a rather long string containing</span><span class="se">\n\</span>
<span class="s">several lines of text just as you would do in C.</span><span class="se">\n\</span>
<span class="s"> Note that whitespace at the beginning of the line is</span><span class="se">\</span>
<span class="s"> significant."</span>
<span class="k">print</span> <span class="n">hello</span>
</pre></div>
</div>
<p>需要注意的是,还是需要在字符串中写入 <tt class="docutils literal"><span class="pre">\n</span></tt> ——结尾的反斜杠会被忽略。前例会打印为如下形式:</p>
<div class="highlight-text"><div class="highlight"><pre>This is a rather long string containing
several lines of text just as you would do in C.
Note that whitespace at the beginning of the line is significant.
</pre></div>
</div>
<p>另外,字符串可以标识在一对三引号中: <tt class="docutils literal"><span class="pre">"""</span></tt> 或 <tt class="docutils literal"><span class="pre">'''</span></tt> 。三引号中,不需要行属转义,它们已经包含在字符串中:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">print</span> <span class="s">"""</span><span class="se">\</span>
<span class="s">Usage: thingy [OPTIONS]</span>
<span class="s"> -h Display this usage message</span>
<span class="s"> -H hostname Hostname to connect to</span>
<span class="s">"""</span>
</pre></div>
</div>
<p>得到如下输出:</p>
<div class="highlight-text"><div class="highlight"><pre>Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
</pre></div>
</div>
<p>如果我们生成一个“原始”字符串, <tt class="docutils literal"><span class="pre">\n</span></tt> 序列不会被转义,而且行尾的反斜杠,源码中的换行符,都成为字符串中的一部分数据,因此下例:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">hello</span> <span class="o">=</span> <span class="s">r"This is a rather long string containing\n</span><span class="se">\</span>
<span class="s">several lines of text much as you would do in C."</span>
<span class="k">print</span><span class="p">(</span><span class="n">hello</span><span class="p">)</span>
</pre></div>
</div>
<p>会打印:</p>
<div class="highlight-text"><div class="highlight"><pre>This is a rather long string containing\n\
several lines of text much as you would do in C.
</pre></div>
</div>
<p>字符串可以由 <tt class="docutils literal"><span class="pre">+</span></tt> 操作符连接(粘到一起),可以由 <tt class="docutils literal"><span class="pre">*</span></tt> 重复:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">word</span> <span class="o">=</span> <span class="s">'Help'</span> <span class="o">+</span> <span class="s">'A'</span>
<span class="gp">>>> </span><span class="n">word</span>
<span class="go">'HelpA'</span>
<span class="gp">>>> </span><span class="s">'<'</span> <span class="o">+</span> <span class="n">word</span><span class="o">*</span><span class="mi">5</span> <span class="o">+</span> <span class="s">'>'</span>
<span class="go">'<HelpAHelpAHelpAHelpAHelpA>'</span>
</pre></div>
</div>
<p>相邻的两个字符串文本自动连接在一起,前面那行代码也可以写为 <tt class="docutils literal"><span class="pre">word</span> <span class="pre">='Help'</span> <span class="pre">'A'</span></tt>;它只用于两个字符串文本,不能用于字符串表达式:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="s">'str'</span> <span class="s">'ing'</span> <span class="c"># <- This is ok</span>
<span class="go">'string'</span>
<span class="gp">>>> </span><span class="s">'str'</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="o">+</span> <span class="s">'ing'</span> <span class="c"># <- This is ok</span>
<span class="go">'string'</span>
<span class="gp">>>> </span><span class="s">'str'</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="s">'ing'</span> <span class="c"># <- This is invalid</span>
<span class="go"> File "<stdin>", line 1, in ?</span>
<span class="go"> 'str'.strip() 'ing'</span>
<span class="go"> ^</span>
<span class="go">SyntaxError: invalid syntax</span>
</pre></div>
</div>
<p>字符串也可以被截取(检索)。类似于 C ,字符串的第一个字符索引为 0 。没有独立的字符类型,字符就是长度为 1 的字符串。类似 Icon ,可以用 <em>切片标注</em> 法截取字符串:由两个索引分割的复本。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">word</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span>
<span class="go">'A'</span>
<span class="gp">>>> </span><span class="n">word</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">2</span><span class="p">]</span>
<span class="go">'He'</span>
<span class="gp">>>> </span><span class="n">word</span><span class="p">[</span><span class="mi">2</span><span class="p">:</span><span class="mi">4</span><span class="p">]</span>
<span class="go">'lp'</span>
</pre></div>
</div>
<p>索引切片可以有默认值,切片时,忽略第一个索引的话,默认为 0,忽略第二个索引,默认为字符串的长度:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">word</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span> <span class="c"># The first two characters</span>
<span class="go">'He'</span>
<span class="gp">>>> </span><span class="n">word</span><span class="p">[</span><span class="mi">2</span><span class="p">:]</span> <span class="c"># Everything except the first two characters</span>
<span class="go">'lpA'</span>
</pre></div>
</div>
<p>不同于 C 字符串,Python 字符串不可变。向字符串文本的某一个索引赋值会引发错误:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">word</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="s">'x'</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n">?</span>
<span class="gr">TypeError</span>: <span class="n">'str' object does not support item assignment</span>
<span class="gp">>>> </span><span class="n">word</span><span class="p">[:</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="s">'Splat'</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n">?</span>
<span class="gr">TypeError</span>: <span class="n">'str' object does not support slice assignment</span>
</pre></div>
</div>
<p>不过,组合文本内容生成一个新字符串简单而高效:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="s">'x'</span> <span class="o">+</span> <span class="n">word</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
<span class="go">'xelpA'</span>
<span class="gp">>>> </span><span class="s">'Splat'</span> <span class="o">+</span> <span class="n">word</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span>
<span class="go">'SplatA'</span>
</pre></div>
</div>
<p>切片操作有个有用的不变性: <tt class="docutils literal"><span class="pre">s[:i]</span> <span class="pre">+</span> <span class="pre">s[i:]</span></tt> 等于 <tt class="docutils literal"><span class="pre">s</span></tt>。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">word</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="n">word</span><span class="p">[</span><span class="mi">2</span><span class="p">:]</span>
<span class="go">'HelpA'</span>
<span class="gp">>>> </span><span class="n">word</span><span class="p">[:</span><span class="mi">3</span><span class="p">]</span> <span class="o">+</span> <span class="n">word</span><span class="p">[</span><span class="mi">3</span><span class="p">:]</span>
<span class="go">'HelpA'</span>
</pre></div>
</div>
<p>Python 能够优雅地处理那些没有意义的切片索引:一个过大的索引值(即下标值大于字符串实际长度)将被字符串实际长度所代替,当上边界比下边界大时(即切片左值大于右值)就返回空字符串。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">word</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">100</span><span class="p">]</span>
<span class="go">'elpA'</span>
<span class="gp">>>> </span><span class="n">word</span><span class="p">[</span><span class="mi">10</span><span class="p">:]</span>
<span class="go">''</span>
<span class="gp">>>> </span><span class="n">word</span><span class="p">[</span><span class="mi">2</span><span class="p">:</span><span class="mi">1</span><span class="p">]</span>
<span class="go">''</span>
</pre></div>
</div>
<p>索引也可以是负数,这将导致从右边开始计算。 例如:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">word</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="c"># The last character</span>
<span class="go">'A'</span>
<span class="gp">>>> </span><span class="n">word</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span> <span class="c"># The last-but-one character</span>
<span class="go">'p'</span>
<span class="gp">>>> </span><span class="n">word</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">:]</span> <span class="c"># The last two characters</span>
<span class="go">'pA'</span>
<span class="gp">>>> </span><span class="n">word</span><span class="p">[:</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span> <span class="c"># Everything except the last two characters</span>
<span class="go">'Hel'</span>
</pre></div>
</div>
<p>请注意 -0 实际上就是 0 ,所以它不会导致从右边开始计算!</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">word</span><span class="p">[</span><span class="o">-</span><span class="mi">0</span><span class="p">]</span> <span class="c"># (since -0 equals 0)</span>
<span class="go">'H'</span>
</pre></div>
</div>
<p>负索引切片越界会被截断,不要尝试将它用于单元素(非切片)检索:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">word</span><span class="p">[</span><span class="o">-</span><span class="mi">100</span><span class="p">:]</span>
<span class="go">'HelpA'</span>
<span class="gp">>>> </span><span class="n">word</span><span class="p">[</span><span class="o">-</span><span class="mi">10</span><span class="p">]</span> <span class="c"># error</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n">?</span>
<span class="gr">IndexError</span>: <span class="n">string index out of range</span>
</pre></div>
</div>
<p>有个办法可以很容易地记住切片的工作方式:切片时的索引是在两个字符 <em>之间</em> 。左边第一个字符的索引为0,,而长度为 <em>n</em> 的字符串其最后一个字符的右界索引为 <em>n</em> 。例如:</p>
<div class="highlight-python"><div class="highlight"><pre> +---+---+---+---+---+
| H | e | l | p | A |
+---+---+---+---+---+
0 1 2 3 4 5
-5 -4 -3 -2 -1
</pre></div>
</div>
<p>文本中的第一行数字给出字符串中的索引点 0...5 。第二行给出相应的负索引。切片是从 <em>i</em> 到 <em>j</em> 两个数值标示的边界之间的所有字符。</p>
<p>对于非负索引,如果上下都在边界内,切片长度就是两个索引之差。例如, <tt class="docutils literal"><span class="pre">word[1:3]</span></tt> 是 2 。</p>
<p>内置函数 <tt class="xref py py-func docutils literal"><span class="pre">len()</span></tt> 返回字符串长度:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">s</span> <span class="o">=</span> <span class="s">'supercalifragilisticexpialidocious'</span>
<span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
<span class="go">34</span>
</pre></div>
</div>
</div>
<div class="section" id="unicode">
<span id="tut-unicodestrings"></span><h3>3.1.3. 关于 Unicode<a class="headerlink" href="#unicode" title="Permalink to this headline">¶</a></h3>
<p>从 Python 2.0 起,程序员们有了一个新的,用来存储文本数据的类型:Unicode 对象。它可以用于存储和维护 Unicode 数据(参见 <a class="reference external" href="http://www.unicode.org/">http://www.unicode.org/</a> ),并且与现有的字符串对象有良好的集成,必要时提供自动转换。</p>
<p>Unicode 的先进之处在于为每一种现代或古代使用的文字系统中出现的每一个字符都提供了统一的序列号。之前,文字系统中的字符只能有 256 种可能的顺序。通过代码页分界映射。文本绑定到映射文字系统的代码页。这在软件国际化的时候尤其麻烦 (通常写作 <tt class="docutils literal"><span class="pre">i18n</span></tt> —— <tt class="docutils literal"><span class="pre">'i'</span></tt> + 18 个字符 + <tt class="docutils literal"><span class="pre">'n'</span></tt> )。Unicode 解决了为所有的文字系统设置一个独立代码页的难题。</p>
<p>在 Python 中创建 Unicode 字符串和创建普通的字符串一样简单:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="s">u'Hello World !'</span>
<span class="go">u'Hello World !'</span>
</pre></div>
</div>
<p>引号前的 <tt class="docutils literal"><span class="pre">'u'</span></tt> 表示这会创建一个 Unicode 字符串。如果想要在字符串中包含特殊字符,可以使用 Python 的 <em>Unicode-Escape</em> 。请看下面的例子:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="s">u'Hello</span><span class="se">\u0020</span><span class="s">World !'</span>
<span class="go">u'Hello World !'</span>
</pre></div>
</div>
<p>转码序列 <tt class="docutils literal"><span class="pre">\u0020</span></tt> 表示在指定位置插入编码为 0x0020 的 Unicode 字符(空格)。</p>
<p>其他字符就像 Unicode 编码一样被直接解释为对应的编码值。 如果你有在许多西方国家使用的标准 Latin-1 编码的字符串,你会发现编码小于 256 的 Unicode 字符和在 Latin-1 编码中的一样。</p>
<p>特别的,和普通字符串一样, Unicode 字符串也有原始模式。可以在引号前加 “ur”,Python 会采用 <em>Raw-Unicode-Escape</em> 编码(原始 Unicode 转义——译者)。如果有前缀为 ‘u’ 的数值,它也只会显示为 <tt class="docutils literal"><span class="pre">uXXXX</span></tt> 。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="s">ur'Hello\u0020World !'</span>
<span class="go">u'Hello World !'</span>
<span class="gp">>>> </span><span class="s">ur'Hello</span><span class="se">\\</span><span class="s">u0020World !'</span>
<span class="go">u'Hello\\\\u0020World !'</span>
</pre></div>
</div>
<p>如果你需要大量输入反斜杠,原始模式非常有用,这在正则表达式中几乎是必须的。</p>
<p>作为这些编码标准的一部分,Python 提供了基于已知编码来创建 Unicode 字符串的整套方法。</p>
<p id="index-0">内置函数 <tt class="xref py py-func docutils literal"><span class="pre">unicode()</span></tt> 可以使用所有注册的 Unicode 编码( COders 和 DECoders )。 众所周知, <em>Latin-1</em> , <em>ASCII</em> , <em>UTF-8</em> 和 <em>UTF-16</em> 之类的编码可以互相转换(Latin-1 表示一个很小的拉丁语言符号集,与 ASCII 基本一致,其实不能用来表示庞大的东方语言字符集——译者)。后两个是变长编码,将每一个 Unicode 字符存储为一到多个字节。通常默认编码为 ASCII,此编码接受 0 到 127 这个范围的编码,否则报错。将一个 Unicode 字符串打印或写入到文件中,或者使用 <tt class="xref py py-func docutils literal"><span class="pre">str()</span></tt> 转换时,转换操作以此为默认编码。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="s">u"abc"</span>
<span class="go">u'abc'</span>
<span class="gp">>>> </span><span class="nb">str</span><span class="p">(</span><span class="s">u"abc"</span><span class="p">)</span>
<span class="go">'abc'</span>
<span class="gp">>>> </span><span class="s">u"盲枚眉"</span>
<span class="go">u'\xe4\xf6\xfc'</span>
<span class="gp">>>> </span><span class="nb">str</span><span class="p">(</span><span class="s">u"盲枚眉"</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n">?</span>
<span class="gr">UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2</span>: <span class="n">ordinal not in range(128)</span>
</pre></div>
</div>
<p>为了将一个 Unicode 字符串转换为一个使用特定编码的 8 位字符串, Unicode 对象提供一个 <tt class="xref py py-func docutils literal"><span class="pre">encode()</span></tt> 方法,它接受编码名作为参数。编码名应该小写。:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="s">u"盲枚眉"</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">'utf-8'</span><span class="p">)</span>
<span class="go">'\xc3\xa4\xc3\xb6\xc3\xbc'</span>
</pre></div>
</div>
<p>如果有一个其它编码的数据,希望可以从中生成一个 Unicode 字符串,你可以使用 <tt class="xref py py-func docutils literal"><span class="pre">unicode()</span></tt> 函数,它接受编码名作为第二参数。:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="nb">unicode</span><span class="p">(</span><span class="s">'</span><span class="se">\xc3\xa4\xc3\xb6\xc3\xbc</span><span class="s">'</span><span class="p">,</span> <span class="s">'utf-8'</span><span class="p">)</span>
<span class="go">u'\xe4\xf6\xfc'</span>
</pre></div>
</div>
</div>
<div class="section" id="tut-lists">
<span id="id4"></span><h3>3.1.4. 列表<a class="headerlink" href="#tut-lists" title="Permalink to this headline">¶</a></h3>
<p>Python 有几个 <em>复合</em> 数据类型,用于表示其它的值。最通用的是 <em>list</em> (列表) ,它可以写作中括号之间的一列逗号分隔的值。列表的元素不必是同一类型。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="s">'spam'</span><span class="p">,</span> <span class="s">'eggs'</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">1234</span><span class="p">]</span>
<span class="gp">>>> </span><span class="n">a</span>
<span class="go">['spam', 'eggs', 100, 1234]</span>
</pre></div>
</div>
<p>就像字符串索引,列表从 0 开始检索。列表可以被切片和连接:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="go">'spam'</span>
<span class="gp">>>> </span><span class="n">a</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>
<span class="go">1234</span>
<span class="gp">>>> </span><span class="n">a</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span>
<span class="go">100</span>
<span class="gp">>>> </span><span class="n">a</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="go">['eggs', 100]</span>
<span class="gp">>>> </span><span class="n">a</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span><span class="s">'bacon'</span><span class="p">,</span> <span class="mi">2</span><span class="o">*</span><span class="mi">2</span><span class="p">]</span>
<span class="go">['spam', 'eggs', 'bacon', 4]</span>
<span class="gp">>>> </span><span class="mi">3</span><span class="o">*</span><span class="n">a</span><span class="p">[:</span><span class="mi">3</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span><span class="s">'Boo!'</span><span class="p">]</span>
<span class="go">['spam', 'eggs', 100, 'spam', 'eggs', 100, 'spam', 'eggs', 100, 'Boo!']</span>
</pre></div>
</div>
<p>所有的切片操作都会返回新的列表,包含求得的元素。这意味着以下的切片操作返回列表 <em>a</em> 的一个浅拷贝的副本:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">a</span><span class="p">[:]</span>
<span class="go">['spam', 'eggs', 100, 1234]</span>
</pre></div>
</div>
<p>不像 <em>不可变的</em> 字符串,列表允许修改元素</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">a</span>
<span class="go">['spam', 'eggs', 100, 1234]</span>
<span class="gp">>>> </span><span class="n">a</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">a</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="mi">23</span>
<span class="gp">>>> </span><span class="n">a</span>
<span class="go">['spam', 'eggs', 123, 1234]</span>
</pre></div>
</div>
<p>也可以对切片赋值,此操作可以改变列表的尺寸,或清空它:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="c"># Replace some items:</span>
<span class="gp">... </span><span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">12</span><span class="p">]</span>
<span class="gp">>>> </span><span class="n">a</span>
<span class="go">[1, 12, 123, 1234]</span>
<span class="gp">>>> </span><span class="c"># Remove some:</span>
<span class="gp">... </span><span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
<span class="gp">>>> </span><span class="n">a</span>
<span class="go">[123, 1234]</span>
<span class="gp">>>> </span><span class="c"># Insert some:</span>
<span class="gp">... </span><span class="n">a</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="s">'bletch'</span><span class="p">,</span> <span class="s">'xyzzy'</span><span class="p">]</span>
<span class="gp">>>> </span><span class="n">a</span>
<span class="go">[123, 'bletch', 'xyzzy', 1234]</span>
<span class="gp">>>> </span><span class="c"># Insert (a copy of) itself at the beginning</span>
<span class="gp">>>> </span><span class="n">a</span><span class="p">[:</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">a</span>
<span class="gp">>>> </span><span class="n">a</span>
<span class="go">[123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]</span>
<span class="gp">>>> </span><span class="c"># Clear the list: replace all items with an empty list</span>
<span class="gp">>>> </span><span class="n">a</span><span class="p">[:]</span> <span class="o">=</span> <span class="p">[]</span>
<span class="gp">>>> </span><span class="n">a</span>
<span class="go">[]</span>
</pre></div>
</div>
<p>内置函数 <tt class="xref py py-func docutils literal"><span class="pre">len()</span></tt> 同样适用于列表:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="s">'a'</span><span class="p">,</span> <span class="s">'b'</span><span class="p">,</span> <span class="s">'c'</span><span class="p">,</span> <span class="s">'d'</span><span class="p">]</span>
<span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">4</span>
</pre></div>
</div>
<p>允许嵌套列表(创建一个包含其它列表的列表),例如:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">q</span> <span class="o">=</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
<span class="gp">>>> </span><span class="n">p</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="n">q</span><span class="p">,</span> <span class="mi">4</span><span class="p">]</span>
<span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
<span class="go">3</span>
<span class="gp">>>> </span><span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="go">[2, 3]</span>
<span class="gp">>>> </span><span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>
<span class="go">2</span>
</pre></div>
</div>
<p>你可以在列表末尾添加内容:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">'xtra'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">p</span>
<span class="go">[1, [2, 3, 'xtra'], 4]</span>
<span class="gp">>>> </span><span class="n">q</span>
<span class="go">[2, 3, 'xtra']</span>
</pre></div>
</div>
<p>注意最后一个例子中, <tt class="docutils literal"><span class="pre">p[1]</span></tt> 和 <tt class="docutils literal"><span class="pre">q</span></tt> 实际上指向同一个对象! 我们会在后面的 <em>object semantics</em> 中继续讨论。</p>
</div>
</div>
<div class="section" id="tut-firststeps">
<span id="id5"></span><h2>3.2. 编程的第一步<a class="headerlink" href="#tut-firststeps" title="Permalink to this headline">¶</a></h2>
<p>当然,我们可以使用 Python 完成比二加二更复杂的任务。例如,我们可以写一个生成 <em>菲波那契</em> 子序列的程序,如下所示:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="c"># Fibonacci series:</span>
<span class="gp">... </span><span class="c"># the sum of two elements defines the next</span>
<span class="gp">... </span><span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span>
<span class="gp">>>> </span><span class="k">while</span> <span class="n">b</span> <span class="o"><</span> <span class="mi">10</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">print</span> <span class="n">b</span>
<span class="gp">... </span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">b</span><span class="p">,</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span>
<span class="gp">...</span>
<span class="go">1</span>
<span class="go">1</span>
<span class="go">2</span>
<span class="go">3</span>
<span class="go">5</span>
<span class="go">8</span>
</pre></div>
</div>
<p>这个例子介绍了几个新功能。</p>
<ul>
<li><p class="first">第一行包括了一个 <em>多重赋值</em> :变量 <tt class="docutils literal"><span class="pre">a</span></tt> 和 <tt class="docutils literal"><span class="pre">b</span></tt> 同时获得了新的值 0 和 1,最后一行又使用了一次。在这个演示中,变量赋值前,右边首先完成计算。右边的表达式从左到右计算。</p>
</li>
<li><p class="first">条件(这里是 <tt class="docutils literal"><span class="pre">b</span> <span class="pre"><</span> <span class="pre">10</span></tt> )为 true 时, <tt class="xref std std-keyword docutils literal"><span class="pre">while</span></tt> 循环执行。在 Python 中,类似于 C ,任何非零整数都是 true;0 是 false。判断条件也可以是字符串或列表,实际上可以是任何序列;所有长度不为零的是 true ,空序列是 false。示例中的测试是一个简单的比较。标准比较操作符与 C 相同: <tt class="docutils literal"><span class="pre"><</span></tt> (小于), <tt class="docutils literal"><span class="pre">></span></tt> (大于), <tt class="docutils literal"><span class="pre">==</span></tt> (等于), <tt class="docutils literal"><span class="pre"><=</span></tt> (小于等于), <tt class="docutils literal"><span class="pre">>=</span></tt> (大于等于)和 <tt class="docutils literal"><span class="pre">!=</span></tt> (不等于)。</p>
</li>
<li><p class="first">循环 <em>体</em> 是 <em>缩进</em> 的:缩进是 Python 组织語句的方法。 Python (还) 不提供集成的行编辑功能,所以你要为每一个缩进行输入 TAB 或空格。实践中建议你找个文本编辑来录入复杂的 Python 程序,大多数文本编辑器提供自动缩进。交互式录入复合语句时,必须在最后输入一个空行来标识结束(因为解释器没办法猜测你输入的哪一行是最后一行),需要注意的是同一个语句块中的每一行必须缩进同样数量的空白。</p>
</li>
<li><p class="first">关键字 <tt class="xref std std-keyword docutils literal"><span class="pre">print</span></tt> 语句输出给定表达式的值。它控制多个表达式和字符串输出为你想要字符串(就像我们在前面计算器的例子中那样)。字符串打印时不用引号包围,每两个子项之间插入空间,所以你可以把格式弄得很漂亮,像这样</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">i</span> <span class="o">=</span> <span class="mi">256</span><span class="o">*</span><span class="mi">256</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="s">'The value of i is'</span><span class="p">,</span> <span class="n">i</span>
<span class="go">The value of i is 65536</span>
</pre></div>
</div>
<p>用一个逗号结尾就可以禁止输出换行:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span>
<span class="gp">>>> </span><span class="k">while</span> <span class="n">b</span> <span class="o"><</span> <span class="mi">1000</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">print</span> <span class="n">b</span><span class="p">,</span>
<span class="gp">... </span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">b</span><span class="p">,</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span>
<span class="gp">...</span>
<span class="go">1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987</span>
</pre></div>
</div>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">3. Python 简介</a><ul>
<li><a class="reference internal" href="#tut-calculator">3.1. 将 Python 当做计算器</a><ul>
<li><a class="reference internal" href="#tut-numbers">3.1.1. 数字</a></li>
<li><a class="reference internal" href="#tut-strings">3.1.2. 字符串</a></li>
<li><a class="reference internal" href="#unicode">3.1.3. 关于 Unicode</a></li>
<li><a class="reference internal" href="#tut-lists">3.1.4. 列表</a></li>
</ul>
</li>
<li><a class="reference internal" href="#tut-firststeps">3.2. 编程的第一步</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="interpreter.html"
title="previous chapter">2. 使用 Python 解释器</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="controlflow.html"
title="next chapter">4. 深入 Python 流程控制</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/introduction.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="controlflow.html" title="4. 深入 Python 流程控制"
>next</a> |</li>
<li class="right" >
<a href="interpreter.html" title="2. 使用 Python 解释器"
>previous</a> |</li>
<li><a href="index.html">Python tutorial 2.7 documentation</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2013, D.D.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.1.
</div>
</body>
</html>