首頁 > 後端開發 > Python教學 > Python直環函數

Python直環函數

步履不停
發布: 2019-07-01 13:50:00
原創
4187 人瀏覽過

Python直環函數

python中的for迴圈

  1. <span class="com">#for迴圈格式(類似Java中的foreach):for 識別符in 列表名稱:</span>

  2. <span class="pun">#>>></span><span class="pln"># movies </span><span class="pun">=</span><span class="pln"> </span><span class="pun">[</span><span class="str">"movie1"</span><span class="pun">,</span><span class="str">"movie2"</span><span class="pun">,</span><span class="str">"movie3"</span><span class="pun">]</span>

  3. <span class="pun">#>>></span><span class="pln"> </span><span class="kwd">for</span><span class="pln"> item </span><span class="kwd">in</span><span class="pln"># movies </span><span class="pun">:</span>

  4. <span class="pln"></span><span class="kwd">print</span><span class="pun">(</span><span class="pln">item</span><span class="pun">)</span>

  5. <span class="pln"></span>

  6. <span class="pln"></span>

    #movie1
  7. <span class="pln"></span>

    #movie2
  8. <span class="pln"></span>

    movie3

######python中的for迴圈類似Java中的foreach循環,固定格式請參考註解###

其中:for表示循環開始,要出現在標識符前面;in將要循環的列表與標識符分隔開;:放在列表名後面,表示處理列表代碼開始;請注意,列表處理代碼必須放在for迴圈下面並縮排! ! !

當for迴圈開始時,python會將列表中的每一個值分別賦給標識符,這也意味著每次循環標識符的值都不一樣;循環會一直迭代,直到處理完列表中的所有資料。

python中的while迴圈

  1. <span class="com">##while迴圈格式:while 迴圈條件:</span>

  2. <span class="pun">>>></span><span class="pln"> </span><span class="kwd">while</span><span class="pln"> count </span> <span class="pun"><</span><span class="pln"> len</span><span class="pun">(</span><span class="pln">movies</span><span class="pun">)</span><span class="pln"> </span><span class="pun">#:</span>

  3. <span class="pln"></span>print(movies#[count])

  4. # count ##=<span class="pln"></span><span class="pln"></span> # count

  5. 1

  6. <span class="pln"></span>

  7. <span class="pln"></span>

  8. #movie1<span class="pln"></span>

    ############movie2##########
  9. <span class="pln">movie3</span>

#while迴圈與for迴圈相比,多了條件判斷的語句,既可以自訂循環結束的條件

python的判斷

  1. <span class="kwd">if</span><span class="pln"> </span><span class="pun">滿足某個條件</span><span class="pln"> </span><span class="pun">:</span>

  2. <span class="pln">#    </span><span class="pun">#執行某些語句</span>

  3. <span class="kwd">else</span><span class="pln"> </span><span class="pun">:</span>

  4. <span class="pln">    </span><span class="pun">#執行某些語句</span>

注意:if語句與else語句最後都要加上:,if與else後面要執行的程式碼都要縮排

#python的函數

  1. ##>>> new_movies <span class="pln"></span>=<span class="pln"></span>["movie1","movie2","movie3",["movie4_1","movie4_2",["movie_4_3_1","movie_4_3_2" ]],"movie5"]

  2. #>> > <span class="pln"></span>def listItems<span class="pln"></span>#(items<span class="pln"></span>) <span class="pln"></span>:#

  3. <span class="pln">        </span><span class="kwd">#對於</span><span class="pln">專案</span><span class="kwd">在</span><span class="pln">專案</span><span class="pun">#中: </span>

  4.            if# isinstance#(<span class="pln"></span> #item,<span class="pln"></span>清單)<span class="pln"></span>:

  5. <span class="pln"></span>                listItems(<span class="pln"></span>item)

  6. ##item)<span class="pln"></span>1

  7. <span class="pln"></span><span class="pln"></span>

  8. <span class="pln"></span>

  9. ##夫足夠############ ##### 打印<span class="pln"></span>(項目<span class="pln"></span>#)

  10. <span class="pln"></span>

    #######################################> >>> ###### listItems######(######new_movies######)###################################################################################################### ### ##電影1##########
  11. <span class="pln">movie2</span>

  12. <span class="pln">#movie3</span>

  13. <span class="pln"></span>

  14. <span class="pln"></span>

  15. <span class="pln"></span>

  16. <span class="pln"></span>

    #movie4_1
  17. <span class="pln"></span>

    movie4_2

########################################### ###############movie_4_3_1#########################movie_4_3_2####################movie_4_3_2########## ###############movie5###################相關教學推薦:###Python影片教學######

以上是Python直環函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板