python循环函数

步履不停
Lepaskan: 2019-07-01 13:50:00
asal
4185 orang telah melayarinya

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视频教程

Atas ialah kandungan terperinci python循环函数. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan