在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Python/ exercise32.循環(huán)和列表
附錄 A-練習(xí) 9:生成一個(gè)空文件(Touch, New-Item)
附錄 A-練習(xí) 10:復(fù)制文件 (cp)
exercise44.繼承 Vs.包含
附錄 A-練習(xí) 14:刪除文件 (rm)
附錄 A-練習(xí) 11:移動(dòng)文件 (mv)
exercise46.項(xiàng)目骨架
附錄 A-練習(xí) 3:如果你迷路了
exercise37.復(fù)習(xí)符號(hào)
exercise47.自動(dòng)化測(cè)試
exercise3.數(shù)字和數(shù)學(xué)計(jì)算
附錄 A-練習(xí) 1:安裝
exercise32.循環(huán)和列表
exercise31.做出決定
exercise42.對(duì)象、類、以及從屬關(guān)系
exercise48.更復(fù)雜的用戶輸入
下一步
簡(jiǎn)介
附錄 A-練習(xí) 7:刪除路徑 (rmdir)
exercise49.寫代碼語句
exercise18.命名, 變量, 代碼, 函數(shù)
exercise12.提示別人
exercise14.提示和傳遞
exercise40.模塊, 類和對(duì)象
附錄 A-練習(xí) 12:查看文件 (less, MORE)
exercise9.打印, 打印, 打印
exercise13.參數(shù), 解包, 變量
exercise30. Else 和 If
exercise28. 布爾表達(dá)式
附錄 A-練習(xí) 4:創(chuàng)建一個(gè)路徑 (mkdir)
附錄 A-練習(xí) 15:退出命令行 (exit)
exercise25. 更多更多的練習(xí)
exercise6.字符串和文本
exercise2.注釋和井號(hào)“#”
exercise21. 函數(shù)的返回值
附錄 A-下一步
exercise1.第一個(gè)程序
exercise23. 閱讀代碼
附錄 A-練習(xí) 5:改變當(dāng)前路徑 (cd)
exercise17.更多文件操作
exercise24. 更多的練習(xí)
exercise19.函數(shù)和變量
exercise51.從瀏覽器獲取輸入
exercise22. 到目前為止你學(xué)到了什么?
exercise41.學(xué)會(huì)說面向?qū)ο?/span>
exercise52.開始你的 web 游戲
exercise20. 函數(shù)和文件
exercise15.讀文件
exercise45.你來制作一個(gè)游戲
exercise10.那是什么?
exercise8.打印, 打印
exercise35.分支和函數(shù)
exercise26. 恭喜你,可以進(jìn)行一次考試了
exercise33.while 循環(huán)
exercise29. IF 語句
exercise36.設(shè)計(jì)和調(diào)試
exercise0.安裝和準(zhǔn)備
exercise50.你的第一個(gè)網(wǎng)站
附錄 A-練習(xí) 2:路徑, 文件夾, 名錄 (pwd)
exercise38.列表操作
附錄 A-練習(xí) 6:列出當(dāng)前路徑 (ls)
exercise16.讀寫文件
exercise4.變量和命名
exercise34.訪問列表元素
exercise11.提問
exercise43.基本的面向?qū)ο蟮姆治龊驮O(shè)計(jì)
附錄 A-簡(jiǎn)介
附錄 A-練習(xí) 8:目錄切換(pushd, popd)
來自老程序員的建議
exercise27. 記住邏輯
exercise5.更多的變量和打印
exercise7.更多的打?。ㄝ敵觯?/span>
附錄 A-練習(xí) 13:輸出文件 (cat)
exercise39.字典,可愛的字典

exercise32.循環(huán)和列表

現(xiàn)在你應(yīng)該有能力寫更有趣的程序出來了。如果你能一直跟得上,你應(yīng)該已經(jīng)看出將“if 語句”和“布爾表達(dá)式”結(jié)合起來可以讓程序作出一些智能化的事情。

out.我們的程序還需要能快速的完成很多重復(fù)的工作。這節(jié)習(xí)題,我們將使用 for 循環(huán)來創(chuàng)建并打印一些列表。在練習(xí)的過程中,你會(huì)逐漸明白它們是怎么回事,我不會(huì)告訴你答案的,你要自己去找出來。

在你開始使用 for 循環(huán)之前,你需要在某個(gè)位置存放循環(huán)的結(jié)果。最好的方法是使用列表(list),顧名思義,列表就是一個(gè)按順序存放東西的容器。它并不復(fù)雜,你只是要學(xué)習(xí)一點(diǎn)新的語法。首先我們看看如何創(chuàng)建列表:

hairs = ['brown', 'blond', 'red']
eyes = ['brown', 'blue', 'green']
weights = [1, 2, 3, 4]

你要做的是以 [(左方括號(hào))開頭“打開”列表,然后寫下你要放入列表的東西,用逗號(hào)隔開,就跟函數(shù)的參數(shù)一樣,最后用](右方括號(hào))結(jié)束列表的定義。然后 Python 接收這個(gè)列表以及里邊所有的內(nèi)容,將其賦給一個(gè)變量。

Warning: 對(duì)于不會(huì)編程的人來說這是一個(gè)難點(diǎn)。習(xí)慣性思維告訴你的大腦大地是平的。記得上一個(gè)練習(xí)中的 if 語句嵌套吧?你可能覺得要理解它有些難度,因?yàn)樯钪幸话闳瞬粫?huì)去像這樣的問題,但這樣的問題在編程中幾乎到處都是。你會(huì)看到一個(gè)函數(shù)調(diào)用另外一個(gè)包含 if 語句的函數(shù),其中又有嵌套列表的列表。如果你看到這樣的東西一時(shí)無法弄懂,就用紙筆記下來,手動(dòng)分割代碼,直到弄懂為止。

現(xiàn)在我們將使用循環(huán)創(chuàng)建一些列表,然后將它們打印出來:

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

你看到的結(jié)果

$ 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 函數(shù)并理解它。 2.在第 22 行,你是否可以直接將 elements 賦值為 range(0,6),而無需使用 for 循環(huán)? 3.在 Python 文檔中找到關(guān)于列表的內(nèi)容,仔細(xì)閱讀以下,除了 append 以外列表還支持哪些操作?

常見問題

Q: 如何定義一個(gè)兩層(2D)的列表?

就是一個(gè)列表在另一個(gè)列表里面,比如[[1,2,3],[4,5,6]]

Q: 列表和數(shù)組不是同一種東西嗎?

依賴于語言和實(shí)現(xiàn)方式。在經(jīng)典設(shè)計(jì)角度,由于數(shù)組列表的實(shí)現(xiàn)方式不同,數(shù)組列表是非常不同的。在 Ruby 中程序員稱之為數(shù)組。在 Python 中,他們稱之為列表。因?yàn)楝F(xiàn)在是 Python 調(diào)用它們,所以我們就稱呼它為列表。

Q: 為什么 for 循環(huán)可以使用一個(gè)沒有定義過的變量?

在 for 循環(huán)開始的時(shí)候,就會(huì)定義這個(gè)變量, 并初始化。

Q: 為什么 for i in range(1, 3):只循環(huán)了兩次?

range()函數(shù)循環(huán)的次數(shù)不包括最后一個(gè)。所以 range(1,3)只循環(huán)到 2,這是這種循環(huán)最常用的方法。

Q: elements.append()實(shí)現(xiàn)什么功能?

它能實(shí)現(xiàn)在列表的末尾追加一個(gè)元素。打開 Python 解析器,自己寫一個(gè)列表做些實(shí)驗(yàn)。當(dāng)你遇到這類問題的時(shí)候,都可以在 Python 的解析器中做些實(shí)驗(yàn),自己找到問題的答案。