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">movie1</span>
<span class="pln">movie2</span>
<span class="pln">movie3</span>
python中的for循环类似Java中的foreach循环,固定格式见注释
其中: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])
<span class="pln"> count </span><span class="pun">=</span><span class="pln"> count </span><span class="pun">+</span><span class="pln"> </span><span class="lit">1</span>
<span class="pln"></span>
<span class="pln">movie1</span>
<span class="pln">movie2</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的函数
<span class="pun">>>></span><span class="pln"> new_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="str">"movie4_1"</span><span class="pun">,</span><span class="str">"movie4_2"</span><span class="pun">,[</span><span class="str">"movie_4_3_1"</span><span class="pun">,</span><span class="str">"movie_4_3_2"</span><span class="pun">]],</span><span class="str">"movie5"</span><span class="pun">]</span>
<span class="pun">>>></span><span class="pln"> </span><span class="kwd">def</span><span class="pln"> listItems</span><span class="pun">(</span><span class="pln">items</span><span class="pun">)</span><span class="pln"> </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"> items </span><span class="pun">:</span>
<span class="pln"> </span><span class="kwd">if</span><span class="pln"> isinstance</span><span class="pun">(</span><span class="pln">item</span><span class="pun">,</span><span class="pln">list</span><span class="pun">)</span><span class="pln"> </span><span class="pun">:</span>
<span class="pln"> listItems</span><span class="pun">(</span><span class="pln">item</span><span class="pun">)</span>
<span class="kwd">else</span><span class="pln"> </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="pun">>>></span><span class="pln"> listItems</span><span class="pun">(</span><span class="pln">new_movies</span><span class="pun">)</span>
<span class="pln">movie1</span>
<span class="pln">movie2</span>
<span class="pln">movie3</span>
<span class="pln">movie4_1</span>
<span class="pln">movie4_2</span>
<span class="pln">movie_4_3_1</span>
<span class="pln">movie_4_3_2</span>
<span class="pln">movie5</span>
相关教程推荐:Python视频教程
Atas ialah kandungan terperinci python循环函数. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!