# 習題 32: 循環和列表
現在你應該有能力寫更有趣的程序出來了。如果你能一直跟得上,你應該已經看出將“if 語句”和“布爾表達式”結合起來可以讓程序作出一些智能化的事情。
然而,我們的程序還需要能很快地完成重復的事情。這節習題中我們將使用 for-loop (for 循環)來創建和打印出各種各樣的列表。在做的過程中,你會逐漸明白它們是怎么回事。現在我不會告訴你,你需要自己找到答案。
在你開始使用 for 循環之前,你需要在某個位置存放循環的結果。最好的方法是使用列表(list),顧名思義,它就是一個按順序存放東西的容器。列表并不復雜,你只是要學習一點新的語法。首先我們看看如何創建列表:
~~~
hairs = ['brown', 'blond', 'red']
eyes = ['brown', 'blue', 'green']
weights = [1, 2, 3, 4]
~~~
你要做的是以 [ (左方括號)開頭“打開”列表,然后寫下你要放入列表的東西,用逗號隔開,就跟函數的參數一樣,最后你需要用 ] (右方括號)結束右方括號的定義。然后 Python 接收這個列表以及里邊所有的內容,將其賦給一個變量。
Warning
對于不會編程的人來說這是一個難點。習慣性思維告訴你的大腦大地是平的。記得上一個練習中的 if 語句嵌套吧,你可能覺得要理解它有些難度,因為生活中一般人不會去像這樣的問題,但這樣的問題在編程中幾乎到處都是。你會看到一個函數調用另外一個包含 if 語句的函數,其中又有嵌套列表的列表。如果你看到這樣的東西一時無法弄懂,就用紙筆記下來,手動分割下去,直到弄懂為止。
現在我們將使用循環創建一些列表,然后將它們打印出來。
<table class="highlighttable"><tbody><tr><td class="linenos"> <div class="linenodiv"> <pre> 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</pre> </div> </td> <td class="code"> <div class="highlight"> <pre>the_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']
# this first kind of for-loop goes through a list
for number in the_count:
print "This is count %d" % number
# same as above
for fruit in fruits:
print "A fruit of type: %s" % fruit
# also we can go through mixed lists too
# notice we have to use %r since we don't know what's in it
for i in change:
print "I got %r" % i
# we can also build lists, first start with an empty one
elements = []
# then use the range function to do 0 to 5 counts
for i in range(0, 6):
print "Adding %d to the list." % i
# append is a function that lists understand
elements.append(i)
# now we can print them out too
for i in elements:
print "Element was: %d" % i
</pre> </div> </td> </tr></tbody></table>
### 你應該看到的結果
~~~
$ python ex32.py
This is count 1
This is count 2
This is count 3
This is count 4
This is count 5
A fruit of type: apples
A fruit of type: oranges
A fruit of type: pears
A fruit of type: apricots
I got 1
I got 'pennies'
I got 2
I got 'dimes'
I got 3
I got 'quarters'
Adding 0 to the list.
Adding 1 to the list.
Adding 2 to the list.
Adding 3 to the list.
Adding 4 to the list.
Adding 5 to the list.
Element was: 0
Element was: 1
Element was: 2
Element was: 3
Element was: 4
Element was: 5
$
~~~
### 加分習題
1. 注意一下 range 的用法。查一下 range 函數并理解它。
1. 在第 22 行,你可以可以直接將 elements 賦值為 range(0,6),而無需使用 for 循環?
1. 在 Python 文檔中找到關于列表的內容,仔細閱讀以下,除了 append 以外列表還支持哪些操作?
- 譯者前言
- 前言:笨辦法更簡單
- 習題 0: 準備工作
- 習題 1: 第一個程序
- 習題 2: 注釋和井號
- 習題 3: 數字和數學計算
- 習題 4: 變量(variable)和命名
- 習題 5: 更多的變量和打印
- 習題 6: 字符串(string)和文本
- 習題 7: 更多打印
- 習題 8: 打印,打印
- 習題 9: 打印,打印,打印
- 習題 10: 那是什么?
- 習題 11: 提問
- 習題 12: 提示別人
- 習題 13: 參數、解包、變量
- 習題 14: 提示和傳遞
- 習題 15: 讀取文件
- 習題 16: 讀寫文件
- 習題 17: 更多文件操作
- 習題 18: 命名、變量、代碼、函數
- 習題 19: 函數和變量
- 習題 20: 函數和文件
- 習題 21: 函數可以返回東西
- 習題 22: 到現在你學到了哪些東西?
- 習題 23: 讀代碼
- 習題 24: 更多練習
- 習題 25: 更多更多的練習
- 習題 26: 恭喜你,現在可以考試了!
- 習題 27: 記住邏輯關系
- 習題 28: 布爾表達式練習
- 習題 29: 如果(if)
- 習題 30: Else 和 If
- 習題 31: 作出決定
- 習題 32: 循環和列表
- 習題 33: While 循環
- 習題 34: 訪問列表的元素
- 習題 35: 分支和函數
- 習題 36: 設計和調試
- 習題 37: 復習各種符號
- 習題 38: 閱讀代碼
- 習題 39: 列表的操作
- 習題 40: 字典, 可愛的字典
- 習題 41: 來自 Percal 25 號行星的哥頓人(Gothons)
- 習題 42: 物以類聚
- 習題 43: 你來制作一個游戲
- 習題 44: 給你的游戲打分
- 習題 45: 對象、類、以及從屬關系
- 習題 46: 一個項目骨架
- 習題 47: 自動化測試
- 習題 48: 更復雜的用戶輸入
- 習題 49: 創建句子
- 習題 50: 你的第一個網站
- 習題 51: 從瀏覽器中獲取輸入
- 習題 52: 創建你的 web 游戲
- 下一步
- 老程序員的建議