| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="ref.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="ref.html" title='Python Reference Manual' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="try.html" /> |
| 13 | <link rel="prev" href="while.html" /> |
| 14 | <link rel="parent" href="compound.html" /> |
| 15 | <link rel="next" href="try.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>7.3 The for statement</title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="7.2 The while statement" |
| 25 | href="while.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="7. Compound statements" |
| 28 | href="compound.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="7.4 The try statement" |
| 31 | href="try.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Reference Manual</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><img src='../icons/blank.png' |
| 38 | border='0' height='32' alt='' width='32' /></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="while.html">7.2 The while statement</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="compound.html">7. Compound statements</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="try.html">7.4 The try statement</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H1><A NAME="SECTION009300000000000000000"></A><A NAME="for"></A> |
| 56 | <BR> |
| 57 | 7.3 The <tt class="keyword">for</tt> statement |
| 58 | </H1> |
| 59 | <a id='l2h-581' xml:id='l2h-581'></a><a id='l2h-582' xml:id='l2h-582'></a> |
| 60 | <P> |
| 61 | The <tt class="keyword">for</tt> statement is used to iterate over the elements of a |
| 62 | sequence (such as a string, tuple or list) or other iterable object: |
| 63 | <a id='l2h-583' xml:id='l2h-583'></a> |
| 64 | <P> |
| 65 | <dl><dd class="grammar"> |
| 66 | <div class="productions"> |
| 67 | <table> |
| 68 | <tr> |
| 69 | <td><a id='tok-for_stmt' xml:id='tok-for_stmt'>for_stmt</a></td> |
| 70 | <td>::=</td> |
| 71 | <td>"for" <a class='grammartoken' href="assignment.html#tok-target_list">target_list</a> "in" <a class='grammartoken' href="exprlists.html#tok-expression_list">expression_list</a> |
| 72 | ":" <a class='grammartoken' href="compound.html#tok-suite">suite</a></td></tr> |
| 73 | <tr> |
| 74 | <td></td> |
| 75 | <td></td> |
| 76 | <td><code>["else" ":" <a class='grammartoken' href="compound.html#tok-suite">suite</a>]</code></td></tr> |
| 77 | </table> |
| 78 | </div> |
| 79 | <a class="grammar-footer" |
| 80 | href="grammar.txt" type="text/plain" |
| 81 | >Download entire grammar as text.</a> |
| 82 | </dd></dl> |
| 83 | |
| 84 | <P> |
| 85 | The expression list is evaluated once; it should yield an iterable |
| 86 | object. An iterator is created for the result of the |
| 87 | <code>expression_list</code>. The suite is then executed once for each |
| 88 | item provided by the iterator, in the |
| 89 | order of ascending indices. Each item in turn is assigned to the |
| 90 | target list using the standard rules for assignments, and then the |
| 91 | suite is executed. When the items are exhausted (which is immediately |
| 92 | when the sequence is empty), the suite in the <tt class="keyword">else</tt> clause, if |
| 93 | present, is executed, and the loop terminates. |
| 94 | <a id='l2h-584' xml:id='l2h-584'></a><a id='l2h-585' xml:id='l2h-585'></a><a id='l2h-586' xml:id='l2h-586'></a> |
| 95 | <P> |
| 96 | A <tt class="keyword">break</tt> statement executed in the first suite terminates the |
| 97 | loop without executing the <tt class="keyword">else</tt> clause's suite. A |
| 98 | <tt class="keyword">continue</tt> statement executed in the first suite skips the rest |
| 99 | of the suite and continues with the next item, or with the <tt class="keyword">else</tt> |
| 100 | clause if there was no next item. |
| 101 | <a id='l2h-587' xml:id='l2h-587'></a><a id='l2h-588' xml:id='l2h-588'></a> |
| 102 | <P> |
| 103 | The suite may assign to the variable(s) in the target list; this does |
| 104 | not affect the next item assigned to it. |
| 105 | |
| 106 | <P> |
| 107 | The target list is not deleted when the loop is finished, but if the |
| 108 | sequence is empty, it will not have been assigned to at all by the |
| 109 | loop. Hint: the built-in function <tt class="function">range()</tt> returns a |
| 110 | sequence of integers suitable to emulate the effect of Pascal's |
| 111 | <code>for i := a to b do</code>; |
| 112 | e.g., <code>range(3)</code> returns the list <code>[0, 1, 2]</code>. |
| 113 | <a id='l2h-589' xml:id='l2h-589'></a><a id='l2h-590' xml:id='l2h-590'></a> |
| 114 | <P> |
| 115 | <span class="warning"><b class="label">Warning:</b> |
| 116 | There is a subtlety when the sequence is being modified |
| 117 | by the loop (this can only occur for mutable sequences, i.e. lists). |
| 118 | An internal counter is used to keep track of which item is used next, |
| 119 | and this is incremented on each iteration. When this counter has |
| 120 | reached the length of the sequence the loop terminates. This means that |
| 121 | if the suite deletes the current (or a previous) item from the |
| 122 | sequence, the next item will be skipped (since it gets the index of |
| 123 | the current item which has already been treated). Likewise, if the |
| 124 | suite inserts an item in the sequence before the current item, the |
| 125 | current item will be treated again the next time through the loop. |
| 126 | This can lead to nasty bugs that can be avoided by making a temporary |
| 127 | copy using a slice of the whole sequence, e.g., |
| 128 | <a id='l2h-591' xml:id='l2h-591'></a></span> |
| 129 | |
| 130 | <P> |
| 131 | <div class="verbatim"><pre> |
| 132 | for x in a[:]: |
| 133 | if x < 0: a.remove(x) |
| 134 | </pre></div> |
| 135 | |
| 136 | <P> |
| 137 | |
| 138 | <DIV CLASS="navigation"> |
| 139 | <div class='online-navigation'> |
| 140 | <p></p><hr /> |
| 141 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 142 | <tr> |
| 143 | <td class='online-navigation'><a rel="prev" title="7.2 The while statement" |
| 144 | href="while.html"><img src='../icons/previous.png' |
| 145 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 146 | <td class='online-navigation'><a rel="parent" title="7. Compound statements" |
| 147 | href="compound.html"><img src='../icons/up.png' |
| 148 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 149 | <td class='online-navigation'><a rel="next" title="7.4 The try statement" |
| 150 | href="try.html"><img src='../icons/next.png' |
| 151 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 152 | <td align="center" width="100%">Python Reference Manual</td> |
| 153 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 154 | href="contents.html"><img src='../icons/contents.png' |
| 155 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 156 | <td class='online-navigation'><img src='../icons/blank.png' |
| 157 | border='0' height='32' alt='' width='32' /></td> |
| 158 | <td class='online-navigation'><a rel="index" title="Index" |
| 159 | href="genindex.html"><img src='../icons/index.png' |
| 160 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 161 | </tr></table> |
| 162 | <div class='online-navigation'> |
| 163 | <b class="navlabel">Previous:</b> |
| 164 | <a class="sectref" rel="prev" href="while.html">7.2 The while statement</A> |
| 165 | <b class="navlabel">Up:</b> |
| 166 | <a class="sectref" rel="parent" href="compound.html">7. Compound statements</A> |
| 167 | <b class="navlabel">Next:</b> |
| 168 | <a class="sectref" rel="next" href="try.html">7.4 The try statement</A> |
| 169 | </div> |
| 170 | </div> |
| 171 | <hr /> |
| 172 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 173 | </DIV> |
| 174 | <!--End of Navigation Panel--> |
| 175 | <ADDRESS> |
| 176 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 177 | </ADDRESS> |
| 178 | </BODY> |
| 179 | </HTML> |