## 六、Python的程序流程
----From a high schoolstudent's view to learn Python
關鍵字:
python 高中生學編程 MIT公開課視頻 Python語言程序流程 判斷 循環
一、條件和分支
在Python中代碼的邏輯順序有幾種,而條件和分支是很重要的一類。最典型的體現這一邏輯順序的語句就是if語句。
if語句:
在Python中if語句看起來十分的簡單和熟悉,它由三部分組成:關鍵字,用于判斷的條件表達式,以及在表達式為真或假是應該執行的代碼。在之前的內容中已經向大家介紹了簡單的if語句的寫法并且舉了幾個例子,那么下面就向大家介紹一下一些復雜的if語句。
1.多重條件表達式
一般大家在寫程序的時候只會在條件行中寫一個條件,但是通過使用邏輯操作符:and,or,not可以將不同的幾個判斷條件連接在一起從而形成多重判斷條件或是否定判斷條件,這樣做的好處是可以把一些需要用幾個if語句表達的條件和在一個里面從而提高了效率。例如:
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 29.2px; height: 109.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 3.0px 1.0px 1.0px; border-color: #000000 #429287 #000000 #000000"><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">1</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">2</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">3</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">4</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">5</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">6</span></p></td><td valign="top" style="width: 447.4px; height: 109.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 3.0px; border-color: #000000 #000000 #000000 #429287"><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>i=[1,2,3,4]</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>x=3</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>if x<9 and x in i:</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...?<wbr>?<wbr>print('trueanswer')</wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">true answer</span></p></td></tr></tbody></table>
這樣就把需要用兩句話表達的式子連接在了一起從而不用大家在寫第二遍。
2.單一語句的代碼塊
雖然大家一般把if語句的開頭和條件部分寫在兩行中,但是其實可以合起來寫在一行中,但是我并不推薦大家這么寫,雖然這樣可能會省空間(省了也沒用……)但是這很難區分,而且這樣也不能分清邏輯的主次關系,并且如果你要添加新的代碼,還是要把它移動到另一行。
else語句
在Python中,大家可能會發現寫一個if語句只能給條件的一種可能設置該向下執行的代碼,如果要給另一種可能設置該怎么辦?這里就引入了和if語句搭配的else語句。如果if語句中的條件表達式的結果為false,那么就會執行else語句中的代碼,代碼和if語句基本相同:
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 29.2px; height: 109.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 3.0px 1.0px 1.0px; border-color: #000000 #429287 #000000 #000000"><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">1</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">2</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">3</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">4</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">5</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">6</span></p></td><td valign="top" style="width: 447.4px; height: 109.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 3.0px; border-color: #000000 #000000 #000000 #429287"><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>if x<4:</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>print('small')</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...?<wbr>?<wbr>?<wbr>else:</wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>print('big')</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">big</span></p></td></tr></tbody></table>
需要注意的是,一般在else之后并不需要像if語句那樣添加一些條件,因為else的條件就是if語句條件false時的情況。還有就是要注意else語句的擺放,也就是縮進問題。在Python中,else語句應和if語句搭配使用,但是如果有好幾個if語句,而你只想把else語句與其中的一個if語句搭配怎么辦?這其實很簡單,只需要把else語句和你想搭配的if語句對齊,這就是大家不得不再次注意縮進的問題,因為這是一個十分重要的表示代碼的邏輯順序的問題,例如:
第一個
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 29.2px; height: 147.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 3.0px 1.0px 1.0px; border-color: #000000 #429287 #000000 #000000"><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">1</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">2</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">3</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">4</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">5</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">6</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">7</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">8</span></p></td><td valign="top" style="width: 447.4px; height: 147.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 3.0px; border-color: #000000 #000000 #000000 #429287"><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>x=7</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>if x>0:</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>ifx<8:</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>print('small')</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>else:</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>print('big')</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">small</span></p></td></tr></tbody></table>
第二個
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 29.2px; height: 147.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 3.0px 1.0px 1.0px; border-color: #000000 #429287 #000000 #000000"><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">1</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">2</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">3</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">4</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">5</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">6</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">7</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">8</span></p></td><td valign="top" style="width: 447.4px; height: 147.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 3.0px; border-color: #000000 #000000 #000000 #429287"><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>x=7</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>if x>0:</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>ifx<8:</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>print('small')</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...?<wbr>?<wbr>else:</wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>print('big')</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">small</span></p></td></tr></tbody></table>
以上這兩種代碼的含義是不同的,因為第一個代碼中的else語句對應的是第二個語句因為它是和其對其的,但是第二個代碼中else和最上面的if語句對齊,因此這就體現出了縮進的重要性,如果縮進錯了,結果就可能完全不相同。
elif語句
elif顧名思義,就是else-if語句,用它可以檢查多個表達式是否為真,elif語句和else語句一樣,也是可加可不加,但是不同的是,一個if語句中最多有一個else語句,但是卻可以有很多歌elif語句,例如:
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 29.2px; height: 166.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 3.0px 1.0px 1.0px; border-color: #000000 #429287 #000000 #000000"><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">1</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">2</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">3</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">4</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">5</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">6</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">7</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">8</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">9</span></p></td><td valign="top" style="width: 447.4px; height: 166.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 3.0px; border-color: #000000 #000000 #000000 #429287"><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>x=9</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>if x>10:</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>print('big')</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...?<wbr>?<wbr>?<wbr>?<wbr>elifx>6:</wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>print('middle')</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...?<wbr>?<wbr>?<wbr>?<wbr>else:</wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>print('small')</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">middle</span></p></td></tr></tbody></table>
這就是條件分支的主要內容。
二、循環
循環也是在Python中十分常見的邏輯順序,而這里一般是通過while語句和for語句來體現。
1. While語句
While語句實際上是一個條件循環語句,但是和if語句的條件不同,while中的代碼會一直循環執行,直到循環的條件為false。While語句的語法其實和if語句有些相似,但是為了讓程序循環起來,就必須要給一個每次循環結束時的指令來讓下次循環進行,也就是所謂的自增語句。例如:
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 29.2px; height: 223.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 3.0px 1.0px 1.0px; border-color: #000000 #429287 #000000 #000000"><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">1</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">2</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">3</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">4</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">5</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">6</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">7</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">8</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">9</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">10</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">11</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">12</span></p></td><td valign="top" style="width: 447.4px; height: 223.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 3.0px; border-color: #000000 #000000 #000000 #429287"><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>x=0</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>while x<7:</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>print("thenumber is " , x)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>x+=1</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">the numberis?<wbr>?<wbr>0</wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">the numberis?<wbr>?<wbr>1</wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">the numberis?<wbr>?<wbr>2</wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">the numberis?<wbr>?<wbr>3</wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">the numberis?<wbr>?<wbr>4</wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">the numberis?<wbr>?<wbr>5</wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">the numberis?<wbr>?<wbr>6</wbr></wbr></span></p></td></tr></tbody></table>
這就是一個while語句計數循環的例子。當然有計數循環就會有無限循環,在其中你必須相信的使用while循環,因為他有可能無限次的循環下去,所以大家要看清楚。
2.for語句
我們現在要介紹的就是Python中的另一個循環語句:for語句。它是Python中最為實用并且強大的循環語句。For循環不同于while循環,它可以提供一個可循環的對象,并將對象(列表,元組等刻可迭代對象)全部執行完才會結束循環。例如:
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 29.2px; height: 166.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 3.0px 1.0px 1.0px; border-color: #000000 #429287 #000000 #000000"><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">1</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">2</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">3</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">4</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">5</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">6</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">7</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">8</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">9</span></p></td><td valign="top" style="width: 447.4px; height: 166.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 3.0px; border-color: #000000 #000000 #000000 #429287"><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>alist=[1, 2, 3, 4, 5]</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>for eachnumber in alist:</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>print(eachnumber,"is a integer")</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">1 is a integer</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">2 is a integer</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">3 is a integer</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">4 is a integer</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">5 is a integer</span></p></td></tr></tbody></table>
這就是簡單的for語句的例子,而不僅僅可以以列表等為循環對象,連字符串都可以,例如:
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 29.2px; height: 261.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 3.0px 1.0px 1.0px; border-color: #000000 #429287 #000000 #000000"><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">1</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">2</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">3</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">4</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">5</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">6</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">7</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">8</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">9</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">10</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">11</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">12</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">13</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">14</span></p></td><td valign="top" style="width: 447.4px; height: 261.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 3.0px; border-color: #000000 #000000 #000000 #429287"><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">>>>for eachletter in 'HelloPython':</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>?<wbr>print("this is letter", eachletter)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">...</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">this is letter H</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">this is letter e</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">this is letter l</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">this is letter l</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">this is letter o</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">this is letter P</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">this is letter y</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">this is letter t</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">this is letter h</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">this is letter o</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">this is letter n</span></p></td></tr></tbody></table>
3. while與for的區別
來看例子:
1到n得自然數進行累加,求累加和小于10000時的最大自然數n的值?
在循環次數未知時,如果需要使用循環,就需要使用while
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 29.2px; height: 128.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 3.0px 1.0px 1.0px; border-color: #000000 #429287 #000000 #000000"><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">1</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">2</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">3</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">4</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">5</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">6</span></p></td><td valign="top" style="width: 447.4px; height: 128.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 3.0px; border-color: #000000 #000000 #000000 #429287"><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">sum = 0</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">i = 1</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">while sum + i <=10000:</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> sum += i;</wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> i += 1</wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">print "from 1 to %d, the sum is%d(near 10000)" % (i, sum)</span></p><p style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;"><br/></p></td></tr></tbody></table>
再看例子:
從1累加到10000,求累加和:
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tbody><tr><td valign="top" style="width: 29.2px; height: 90.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 3.0px 1.0px 1.0px; border-color: #000000 #429287 #000000 #000000"><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">1</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">2</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">3</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">4</span></p><p style="margin: 0px 0px 5px; text-align: right; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">5</span></p></td><td valign="top" style="width: 447.4px; height: 90.0px; background-color: #ffffff; border-style: solid; border-width: 1.0px 1.0px 1.0px 3.0px; border-color: #000000 #000000 #000000 #429287"><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">sum = 0</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">for i inrange(1,10001):</span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">?<wbr>?<wbr> sum += i</wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144); min-height: 14px;"><span style="letter-spacing: 0.0px">?<wbr>?<wbr>?<wbr/></wbr></wbr></span></p><p style="margin: 0px 0px 5px; font-size: 12px; line-height: normal; font-family: Arial; color: rgb(1, 24, 144);"><span style="letter-spacing: 0.0px">print "sum 1 to 10000 is equal%d" % sum</span></p></td></tr></tbody></table>
4. range的使用說明
range是一個built-in函數,函數定義如下:
**range([start],stop[,step])**
This is a versatile function tocreate lists containing arithmetic progressions. It is most oftenused in?[**for**](http://www.python.org/doc//current/reference/compound_stmts.html)?loops.The arguments must be plain integers. Ifthe?*step*?argument isomitted, it defaults to?1.If the?*start*?argument isomitted, it defaults to?0.The full form returns a list of plainintegers?[start,?start?+?step,?start?+?2?*?step,?...].If?*step*?is positive, thelast element is the largest?start?+?i?*?step?lessthan?*stop*;if?*step*?is negative, thelast element is the smallest?start?+?i?*?step?greaterthan?*stop*.?*step*?mustnot be zero (or else?[**ValueError**](http://www.python.org/doc//current/library/exceptions.html)?israised). Example:
~~~
range(10)?
[0, 1, 2, 3, 4, 5, 6, 7, 8,9]?
range(1, 11)?
[1, 2, 3, 4, 5, 6, 7, 8, 9,10]?
range(0, 30, 5)?
[0, 5, 10, 15, 20,25]?
range(0, 10, 3)?
[0, 3, 6,9]?
range(0, -10, -1)?
[0, -1, -2, -3, -4, -5, -6, -7,-8, -9]?
range(0)?
[]?
range(1, 0)?
[]
~~~
注意以下幾點:
- range()返回的是一個list
~~~
type(range(10))
<type'list'>
~~~
- start的缺省參數為0,step的缺省參數為1(*缺省參數將在函數的章節中介紹)
- 如果step的值為負數,則產生的序列為倒序
這就是Python中的條件分支與循環的主要內容,下節會向大家講解函數的知識。
再次強調,對于循環、條件分支語句在寫程序的時候千萬注意縮進,如果在程序出錯的時候,首先要檢查的也是縮進是否準確,然后再找其他的問題。
我的更多文章:
- [Python程序調試的一些體會](http://blog.sina.com.cn/s/blog_d6cca93e0101ewc9.html)(2013-10-06 22:57:35)
- [十四、Python編程計算24點(之二)](http://blog.sina.com.cn/s/blog_d6cca93e0101euxx.html)(2013-10-03 22:18:28)
- [十三、Python編程計算24點(之一)](http://blog.sina.com.cn/s/blog_d6cca93e0101eukc.html)
(2013-10-02 22:15:46)
- [十二、Python簡單數據結構應用(之二)](http://blog.sina.com.cn/s/blog_d6cca93e0101euk8.html)(2013-10-02 22:10:41)
- [十一、Python簡單數據結構應用(之一)](http://blog.sina.com.cn/s/blog_d6cca93e0101ep9z.html)(2013-09-23 23:31:49)
- [十、Python編程解決組合問題(之二)](http://blog.sina.com.cn/s/blog_d6cca93e0101entc.html)
(2013-09-21 23:37:27)
- [九、Python編程解決組合問題(之一)](http://blog.sina.com.cn/s/blog_d6cca93e0101ent7.html)(2013-09-21 23:32:54)
- [八、Python的函數編程(之二)](http://blog.sina.com.cn/s/blog_d6cca93e0101ekwj.html)
(2013-09-20 23:09:39)
- [七、Python的函數編程(之一)](http://blog.sina.com.cn/s/blog_d6cca93e0101ekwg.html)
(2013-09-20 23:09:10)
- [高中生如何學編程](http://blog.sina.com.cn/s/blog_d6cca93e0101e8fn.html)(2013-09-02 19:26:01)