Python學習筆記第3章 操作列表 1

2021-09-11 02:30:16 字數 4423 閱讀 8674

一、遍歷整個列表

我們經常需要遍歷列表的所有元素,為了避免大量的重複**,這裡我麼使用for迴圈來解決這些問題。

**實現:

name = ["chenchen","weipeng","jiangnan","suqin"]

for person in name:

print(person)

執行結果如下:

chenchen

weipeng

jiangnan

suqin

這裡我們定義了乙個for()迴圈,這行**讓python從列表name中取出乙個名字,並將其儲存在變數person中。列印變數的值,這樣python便會執行for()迴圈,直到列印出最後乙個元素為止,輸出很簡單,就是列表中所有的姓名。

1.1深入的研究迴圈

我們在編寫for()迴圈時,對於用於儲存列表中每個值的臨時變數,可指定任何名稱。

1.2在for()迴圈中執行更多的操作

**演示:

name = ["chenchen","weipeng","jiangnan","suqin"]

for person in name:

print(person.title()+" ,welcome to here!")

print("good luck! "+person.title())

執行結果如下:

chenchen ,welcome to here!

good luck! chenchen

weipeng ,welcome to here!

good luck! weipeng

jiangnan ,welcome to here!

good luck! jiangnan

suqin ,welcome to here!

good luck! suqin

在for()迴圈中,想包含多少**都可以。

1.3在for()迴圈結束後執行一些操作

在for()迴圈後面,沒有縮排的**只執行一次,不會重複執行。

**實現:

name = ["chenchen","weipeng","jiangnan","suqin"]

for person in name:

print(person.title()+" ,welcome to here!")

print("good luck! "+person.title())

print("\n"+"thank you , everyone.")

執行結果如下:

chenchen ,welcome to here!

good luck! chenchen

weipeng ,welcome to here!

good luck! weipeng

jiangnan ,welcome to here!

good luck! jiangnan

suqin ,welcome to here!

good luck! suqin

thank you , everyone.

使用for()迴圈處理資料是一種對資料集執行整條操作的不錯的方式。

二、避免縮排錯誤

python通過使用縮排讓**更容易閱讀,簡單的說,它要求你使用縮排讓**更加整潔而清晰。在較長的python程式中,我們將看到不同縮排程度的**塊,這讓我們對程式的組織結構有大致的認識。下面我們將認識一些常見的縮排錯誤。

2.1忘記縮排

如果我們在編寫**的過程中忘記縮排,演示**:

name = ["chenchen","weipeng","jiangnan","suqin"]

for person in name:

print(person)

執行此程式便會報錯expected an indented block(預期會有乙個縮排塊)。通常,將緊跟在for語句後面的**進行縮排,就可以消除這種縮排錯誤。

2.2忘記縮排額外的**行

有時候,迴圈能過執行而不會報錯,但結果可能會出乎意料,我們看下面程式。

name = ["chenchen","weipeng","jiangnan","suqin"]

for person in name:

print(person.title()+" ,welcome to here!")

print("good luck! "+person.title())

print("\n"+"thank you , everyone.")

執行結果如下:

chenchen ,welcome to here!

weipeng ,welcome to here!

jiangnan ,welcome to here!

suqin ,welcome to here!

good luck! suqin

thank you , everyone.

第二條print語句原本需要縮排,但是這裡忘記縮排。這裡執行後的結果,忘記縮排的語句只會在迴圈結束後執行一次,所以只會在列表終值後出現一次,而達不到我們預期的結果。

這是乙個邏輯錯誤,從語法上看,這些python**是合法的,但是由於邏輯錯誤,結果不符合預期。

2.3不必要的縮排

如果我們不小心縮排了無需縮排的**行,python將指出這一點:

name = ["chenchen","weipeng","jiangnan","suqin"]

print(name)

執行此程式便會報錯unexpected indent(不必要的縮排)

為避免意外縮排錯誤,細心謹慎,只縮排需要縮排的**行。在我們前面編寫的**中,只有要在for迴圈中對每個元素執行的**需要縮排。

2.4迴圈後不必要的縮排

如果我麼不小心將應在迴圈結束後執行的**縮排,這些**將會針對每個列表元素重複執行。

**演示:

name = ["chenchen","weipeng","jiangnan","suqin"]

for person in name:

print(person.title()+" ,welcome to here!")

print("good luck! "+person.title())

print("\n"+"thank you , everyone.")

執行結果如下:

chenchen ,welcome to here!

good luck! chenchen

thank you , everyone.

weipeng ,welcome to here!

good luck! weipeng

thank you , everyone.

jiangnan ,welcome to here!

good luck! jiangnan

thank you , everyone.

suqin ,welcome to here!

good luck! suqin

thank you , everyone.

這也是乙個邏輯錯誤,python不知道我們的本意,只要**符合語法,它就會執行。

2.5遺漏了冒號

for語句末尾的冒號告訴python,下一行是迴圈的第一行。

name = ["chenchen","weipeng","jiangnan","suqin"]

for person in name

print(person.title()+" ,welcome to here!")

print("good luck! "+person.title())

print("\n"+"thank you , everyone.")

這裡便會報錯invalid syntax(無效的語法)

如果我們不小心遺漏了冒號,將導致語法錯誤,因為python不知道我們意欲何為,這種錯誤雖然易於消除,但並不容易發現,從而浪費我們大量的時間去尋找錯誤。

PMBOK學習筆記 第3章

第3章 單個專案的專案管理過程 專案管理就是將知識 技能 工具和技術應用於專案活動,以滿足專案的要求。過程是為完成預定的產品 成果或服務而執行的一系列相互關聯的行動和活動。42個專案管理過程歸入5大專案管理過程組和9大專案管理知識領域。各專案管理過程都被歸入其大多數活動所在的那個過程組。啟動過程組 ...

Cpp Primer 學習筆記 第3章

編號說明1 using 宣告 標頭檔案中一般不應該使用using宣告 2標準庫型別string include using std string 初始化方式 string s1 string s2 s1 string s2 s1 string s3 value string s3 value str...

Python學習筆記 第3天

1.字典的定義 字典是一系列鍵 值對。每個鍵都與乙個值相關聯,可以通過使用鍵來訪問與之相關聯的值。字典可以理解為列表的公升級版 列表bicycles trek cannondale specialize 字典 aline 0 如果需要修改 trek 則需要先找到它的下標,而字典是根據鍵名找,下標沒有...