python中的for迴圈
<span class="com">#for迴圈格式(類似Java中的foreach):for 識別符in 列表名稱:</span>
<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>
<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>
<span class="pln"></span><span class="kwd">print</span><span class="pun">(</span><span class="pln">item</span><span class="pun">)</span>
<span class="pln"></span>
<span class="pln"></span>
<span class="pln"></span>
<span class="pln"></span>
其中:for表示循環開始,要出現在標識符前面;in將要循環的列表與標識符分隔開;:放在列表名後面,表示處理列表代碼開始;請注意,列表處理代碼必須放在for迴圈下面並縮排! ! !
當for迴圈開始時,python會將列表中的每一個值分別賦給標識符,這也意味著每次循環標識符的值都不一樣;循環會一直迭代,直到處理完列表中的所有資料。
python中的while迴圈
<span class="com">##while迴圈格式:while 迴圈條件:</span>
<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>
<span class="pln"></span>print(movies#[count])
# count ##=<span class="pln"></span><span class="pln"></span> # count
1
<span class="pln"></span>
<span class="pln"></span>
#movie1<span class="pln"></span>
<span class="pln">movie3</span>
#while迴圈與for迴圈相比,多了條件判斷的語句,既可以自訂循環結束的條件
python的判斷
<span class="kwd">if</span><span class="pln"> </span><span class="pun">滿足某個條件</span><span class="pln"> </span><span class="pun">:</span>
<span class="pln"># </span><span class="pun">#執行某些語句</span>
<span class="kwd">else</span><span class="pln"> </span><span class="pun">:</span>
<span class="pln"> </span><span class="pun">#執行某些語句</span>
注意:if語句與else語句最後都要加上:,if與else後面要執行的程式碼都要縮排
#python的函數
##>>> 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"]
#>> > <span class="pln"></span>def listItems<span class="pln"></span>#(items<span class="pln"></span>) <span class="pln"></span>:
#
<span class="pln"> </span><span class="kwd">#對於</span><span class="pln">專案</span><span class="kwd">在</span><span class="pln">專案</span><span class="pun">#中: </span>
if# isinstance#(<span class="pln"></span> #item,<span class="pln"></span>清單)<span class="pln"></span>:
<span class="pln"></span> listItems(<span class="pln"></span>item)
##item)<span class="pln"></span>1
<span class="pln"></span><span class="pln"></span>
<span class="pln"></span>
##夫足夠############ ##### 打印<span class="pln"></span>(項目<span class="pln"></span>#)
<span class="pln"></span>
<span class="pln">movie2</span>
<span class="pln">#movie3</span>
<span class="pln"></span>
<span class="pln"></span>
<span class="pln"></span>
<span class="pln"></span>
<span class="pln"></span>
以上是Python直環函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!