@@ -59,6 +59,19 @@ msgid ""
59
59
"...\n"
60
60
"More"
61
61
msgstr ""
62
+ ">>> x = int(input(\" Please enter an integer:\" ))\n"
63
+ "Please enter an integer: 42\n"
64
+ ">>> if x < 0:\n"
65
+ "... x = 0\n"
66
+ "... print('Negative changed to zero')\n"
67
+ "... elif x == 0:\n"
68
+ "... print('Zero')\n"
69
+ "... elif x == 1:\n"
70
+ "... print('Single')\n"
71
+ "... else:\n"
72
+ "... print('More')\n"
73
+ "...\n"
74
+ "More"
62
75
63
76
#: ../../tutorial/controlflow.rst:33
64
77
msgid ""
@@ -112,6 +125,14 @@ msgid ""
112
125
"window 6\n"
113
126
"defenestrate 12"
114
127
msgstr ""
128
+ ">>> # 測量一些字串:\n"
129
+ ">>> words = ['cat', 'window', 'defenestrate']\n"
130
+ ">>> for w in words:\n"
131
+ "... print(w, len(w))\n"
132
+ "...\n"
133
+ "cat 3\n"
134
+ "window 6\n"
135
+ "defenestrate 12"
115
136
116
137
#: ../../tutorial/controlflow.rst:72
117
138
msgid ""
@@ -138,6 +159,19 @@ msgid ""
138
159
" if status == 'active':\n"
139
160
" active_users[user] = status"
140
161
msgstr ""
162
+ "# 建立一個範例集合\n"
163
+ "users = {'Hans': 'active', 'Éléonore': 'inactive', '景太郎': 'active'}\n"
164
+ "\n"
165
+ "# 策略:對副本進行疊代\n"
166
+ "for user, status in users.copy().items():\n"
167
+ " if status == 'inactive':\n"
168
+ " del users[user]\n"
169
+ "\n"
170
+ "# 策略:建立一個新集合\n"
171
+ "active_users = {}\n"
172
+ "for user, status in users.items():\n"
173
+ " if status == 'active':\n"
174
+ " active_users[user] = status"
141
175
142
176
#: ../../tutorial/controlflow.rst:94
143
177
msgid "The :func:`range` Function"
@@ -376,6 +410,9 @@ msgid ""
376
410
"finishes without executing the :keyword:`!break`, the :keyword:`!else` "
377
411
"clause executes."
378
412
msgstr ""
413
+ "在 :keyword:`!for` 或 :keyword:`!while` 迴圈中,:keyword:`!break` "
414
+ "陳述句可能與 :keyword:`!else` 子句配對。如果迴圈完成而沒有執行 "
415
+ ":keyword:`!break`,:keyword:`!else` 子句會被執行。"
379
416
380
417
#: ../../tutorial/controlflow.rst:208
381
418
msgid ""
@@ -427,6 +464,23 @@ msgid ""
427
464
"8 equals 2 * 4\n"
428
465
"9 equals 3 * 3"
429
466
msgstr ""
467
+ ">>> for n in range(2, 10):\n"
468
+ "... for x in range(2, n):\n"
469
+ "... if n % x == 0:\n"
470
+ "... print(n, 'equals', x, '*', n//x)\n"
471
+ "... break\n"
472
+ "... else:\n"
473
+ "... # 迴圈結束但沒有找到因數\n"
474
+ "... print(n, 'is a prime number')\n"
475
+ "...\n"
476
+ "2 is a prime number\n"
477
+ "3 is a prime number\n"
478
+ "4 equals 2 * 2\n"
479
+ "5 is a prime number\n"
480
+ "6 equals 2 * 3\n"
481
+ "7 is a prime number\n"
482
+ "8 equals 2 * 4\n"
483
+ "9 equals 3 * 3"
430
484
431
485
#: ../../tutorial/controlflow.rst:239
432
486
msgid ""
@@ -444,6 +498,9 @@ msgid ""
444
498
"condition is ever true, a ``break`` will happen. If the condition is never "
445
499
"true, the ``else`` clause outside the loop will execute."
446
500
msgstr ""
501
+ "理解 else 子句的一個方法是將它想像成與迴圈內的 ``if`` 配對。當迴圈執行時,它會"
502
+ "運行如 if/if/if/else 的序列。``if`` 在迴圈內,會遇到多次。如果條件曾經為真,"
503
+ "``break`` 就會發生。如果條件從未為真,迴圈外的 ``else`` 子句就會執行。"
447
504
448
505
#: ../../tutorial/controlflow.rst:248
449
506
msgid ""
@@ -478,6 +535,9 @@ msgid ""
478
535
"... pass # Busy-wait for keyboard interrupt (Ctrl+C)\n"
479
536
"..."
480
537
msgstr ""
538
+ ">>> while True:\n"
539
+ "... pass # 忙碌等待鍵盤中斷 (Ctrl+C)\n"
540
+ "..."
481
541
482
542
#: ../../tutorial/controlflow.rst:266
483
543
msgid "This is commonly used for creating minimal classes::"
@@ -509,6 +569,9 @@ msgid ""
509
569
"... pass # Remember to implement this!\n"
510
570
"..."
511
571
msgstr ""
572
+ ">>> def initlog(*args):\n"
573
+ "... pass # 記得要實作這個!\n"
574
+ "..."
512
575
513
576
#: ../../tutorial/controlflow.rst:284
514
577
msgid ":keyword:`!match` Statements"
@@ -604,6 +667,18 @@ msgid ""
604
667
" case _:\n"
605
668
" raise ValueError(\" Not a point\" )"
606
669
msgstr ""
670
+ "# point 是一個 (x, y) 元組\n"
671
+ "match point:\n"
672
+ " case (0, 0):\n"
673
+ " print(\" Origin\" )\n"
674
+ " case (0, y):\n"
675
+ " print(f\" Y={y}\" )\n"
676
+ " case (x, 0):\n"
677
+ " print(f\" X={x}\" )\n"
678
+ " case (x, y):\n"
679
+ " print(f\" X={x}, Y={y}\" )\n"
680
+ " case _:\n"
681
+ " raise ValueError(\" Not a point\" )"
607
682
608
683
#: ../../tutorial/controlflow.rst:331
609
684
msgid ""
@@ -912,6 +987,17 @@ msgid ""
912
987
">>> fib(2000)\n"
913
988
"0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597"
914
989
msgstr ""
990
+ ">>> def fib(n): # 寫出小於 n 的費氏數列\n"
991
+ "...\"\"\" 印出小於 n 的費氏數列。\"\"\" \n"
992
+ "... a, b = 0, 1\n"
993
+ "... while a < n:\n"
994
+ "... print(a, end=' ')\n"
995
+ "... a, b = b, a+b\n"
996
+ "... print()\n"
997
+ "...\n"
998
+ ">>> # 現在呼叫我們剛才定義的函式:\n"
999
+ ">>> fib(2000)\n"
1000
+ "0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597"
915
1001
916
1002
#: ../../tutorial/controlflow.rst:482
917
1003
msgid ""
@@ -1045,6 +1131,18 @@ msgid ""
1045
1131
">>> f100 # write the result\n"
1046
1132
"[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]"
1047
1133
msgstr ""
1134
+ ">>> def fib2(n): # 回傳到 n 為止的費氏數列\n"
1135
+ "...\"\"\" 回傳包含到 n 為止的費氏數列的串列。\"\"\" \n"
1136
+ "... result = []\n"
1137
+ "... a, b = 0, 1\n"
1138
+ "... while a < n:\n"
1139
+ "... result.append(a) # 見下方\n"
1140
+ "... a, b = b, a+b\n"
1141
+ "... return result\n"
1142
+ "...\n"
1143
+ ">>> f100 = fib2(100) # 呼叫它\n"
1144
+ ">>> f100 # 寫出結果\n"
1145
+ "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]"
1048
1146
1049
1147
#: ../../tutorial/controlflow.rst:550
1050
1148
msgid "This example, as usual, demonstrates some new Python features:"
@@ -1301,6 +1399,12 @@ msgid ""
1301
1399
"parrot('a thousand', state='pushing up the daisies') # 1 positional, 1 "
1302
1400
"keyword"
1303
1401
msgstr ""
1402
+ "parrot(1000) # 1 個位置引數\n"
1403
+ "parrot(voltage=1000) # 1 個關鍵字引數\n"
1404
+ "parrot(voltage=1000000, action='VOOOOOM') # 2 個關鍵字引數\n"
1405
+ "parrot(action='VOOOOOM', voltage=1000000) # 2 個關鍵字引數\n"
1406
+ "parrot('a million', 'bereft of life', 'jump') # 3 個位置引數\n"
1407
+ "parrot('a thousand', state='pushing up the daisies') # 1 個位置引數、1 個關鍵字引數\n"
1304
1408
1305
1409
#: ../../tutorial/controlflow.rst:677
1306
1410
msgid "but all the following calls would be invalid::"
@@ -1314,6 +1418,10 @@ msgid ""
1314
1418
"parrot(110, voltage=220) # duplicate value for the same argument\n"
1315
1419
"parrot(actor='John Cleese') # unknown keyword argument"
1316
1420
msgstr ""
1421
+ "parrot() # 缺少必要引數\n"
1422
+ "parrot(voltage=5.0, 'dead') # 關鍵字引數後面的非關鍵字引數\n"
1423
+ "parrot(110, voltage=220) # 同一個引數有重複值\n"
1424
+ "parrot(actor='John Cleese') # 未知的關鍵字引數"
1317
1425
1318
1426
#: ../../tutorial/controlflow.rst:684
1319
1427
msgid ""
@@ -1877,6 +1985,11 @@ msgid ""
1877
1985
"list\n"
1878
1986
"[3, 4, 5]"
1879
1987
msgstr ""
1988
+ ">>> list(range(3, 6)) # 使用分離引數的一般呼叫\n"
1989
+ "[3, 4, 5]\n"
1990
+ ">>> args = [3, 6]\n"
1991
+ ">>> list(range(*args)) # 以從串列解包而得的引數呼叫\n"
1992
+ "[3, 4, 5]"
1880
1993
1881
1994
#: ../../tutorial/controlflow.rst:965
1882
1995
msgid ""
@@ -2041,6 +2154,17 @@ msgid ""
2041
2154
"\n"
2042
2155
"No, really, it doesn't do anything."
2043
2156
msgstr ""
2157
+ ">>> def my_function():\n"
2158
+ "...\"\"\" 不做任何事,但有文件說明。\n"
2159
+ "...\n"
2160
+ "... 不,真的,它什麼都不做。\n"
2161
+ "...\"\"\" \n"
2162
+ "... pass\n"
2163
+ "...\n"
2164
+ ">>> print(my_function.__doc__)\n"
2165
+ "Do nothing, but document it.\n"
2166
+ "\n"
2167
+ "No, really, it doesn't do anything."
2044
2168
2045
2169
#: ../../tutorial/controlflow.rst:1064
2046
2170
msgid "Function Annotations"