首页 >后端开发 >Python教程 >python循环函数

python循环函数

步履不停
步履不停原创
2019-07-01 13:50:004178浏览

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">movie1</span>

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

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

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. <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>

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

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

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

  8. <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. <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>

  2. <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>

  3. <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>

  4. <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>

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

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

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

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

  9. <span class="pun">>>></span><span class="pln"> listItems</span><span class="pun">(</span><span class="pln">new_movies</span><span class="pun">)</span>

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

  11. <span class="pln">movie2</span>

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

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

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

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

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

  17. <span class="pln">movie5</span>

相关教程推荐:Python视频教程

以上是python循环函数的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关文章

查看更多