在这个例子中for (int i = 0; i < max; i++) {}
i用于帮助确定循环何时应结束/停止迭代。在
Python会自动地在幕后为您完成这项工作。在
本例中的变量i:
^{pr2}$
是一个占位符,用于保存正在迭代的值。在
通常,i变量的调用方式如下:目标变量
迭代变量
在后台,当for循环用完要迭代的项时,将引发StopIteration条件,for循环将退出。在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.
注意:target_list并不意味着该项是实际列表。在