美文网首页
浅析Python的for语句

浅析Python的for语句

作者: 40巨盗 | 来源:发表于2018-08-03 23:40 被阅读0次

参考:
Documentation

for语句

for语句是用来迭代一个序列(例如string,tuple,list)或者其他可迭代对象中的元素的。
语法如下:

for_stmt ::= "for" target_list "in" expression_list ":" suite 
            ["else" ":" suite]

The expression_list is evaluated once; it should yield an iterable object. An iterator is created for the result of the expression_list. The suite is then executed once for each item provided by the iterator, in the order returned by the iterator. Each item in turn is assigned to the target_list using the standard rules for assignments (see Assignment statements), and then the suite is executed. When the items are exhausted (which is immediately when the sequence is empty or an iterator raises a StopIteration exception), the suite in the else clause, if present, is executed, and the loop terminates.

A break statement executed in the first suite terminates the loop without executing the else clause’s suite. A continue statement executed in the first suite skips the rest of the suite and continues with the next item, or with the else clause if there is no next item.

The for-loop makes assignments to the variables(s) in the target list. This overwrites all previous assignments to those variables including those made in the suite of the for-loop:

for i in range(10):
    print(i)
    i = 5             # this will not affect the for-loop
                      # because i will be overwritten with the next
                      # index in the range

Note: There is a subtlety when the sequence is being modified by the loop (this can only occur for mutable sequences, e.g. lists). An internal counter is used to keep track of which item is used next, and this is incremented on each iteration. When this counter has reached the length of the sequence the loop terminates. This means that if the suite deletes the current (or a previous) item from the sequence, the next item will be skipped (since it gets the index of the current item which has already been treated). Likewise, if the suite inserts an item in the sequence before the current item, the current item will be treated again the next time through the loop. This can lead to nasty bugs that can be avoided by making a temporary copy using a slice of the whole sequence, e.g.,

for x in a[:]:
    if x < 0: a.remove(x)

Python遍历列表的四种方式

方式一:

app_list = [1234, 5677, 8899]

for app_id in app_list:
    print app_id

方式二:

app_list = [1234, 5677, 8899]

for index, app_id in enumerate(app_list):
    print index, app_id

方式三:

 app_list = [1234, 5677, 8899]

 for i in range(len(app_list)):
     print i, app_list[i]

方式四:

app_list = [1234, 5677, 8899]

for app_id in iter(app_list):
    print app_id

相关文章

  • Python with语句简介

    1.脚本之家关于with的简介深入浅析python with语句简介 2.博客园Python中的with语句pyt...

  • 浅析Python的for语句

    参考:Documentation for语句 for语句是用来迭代一个序列(例如string,tuple,list...

  • 012.Python循环语句

    Python 循环语句 1. 概述 Python中的循环语句有 for 和 while。 Python循环语句的控...

  • Python——控制语句和语法规则

    if语句 Python if语句是选取要执行的操作。 通用格式 Python语法规则 Python语法特性 语句是...

  • python 基础 - 循环语句

    python 循环语句 Python中的循环语句有 for 和 while。Python循环语句的控制结构图如下所...

  • Python3版本问题

    在Python3.7执行这个语句 在Python3.8执行这个语句 在Python2.7执行这个语句 Python...

  • python wsgi+Odoo 的启动

    参考:WSGI初探Odoo web 机制浅析python的 WSGI 简介python wsgi 简介 wsgi的...

  • 2018-07-27

    python 的循环语句: for 语句: #!/usr/bin/env python3 #-*- coding:...

  • 我的python学习笔记-第十天

    循环语句 Python中的循环语句有 for 和 while。 while 循环 Python中while语句的一...

  • Python 语句

    Python 语句包括以下: Python 条件语句if - elseif - elif - ... - else...

网友评论

      本文标题:浅析Python的for语句

      本文链接:https://www.haomeiwen.com/subject/ixenmftx.html