@@ -139,18 +139,24 @@ but in fact it isn't. It is an object which returns the successive items of
139
139
the desired sequence when you iterate over it, but it doesn't really make
140
140
the list, thus saving space.
141
141
142
- We say such an object is * iterable * , that is, suitable as a target for
142
+ We say such an object is :term: ` iterable ` , that is, suitable as a target for
143
143
functions and constructs that expect something from which they can
144
- obtain successive items until the supply is exhausted. We have seen that
145
- the :keyword: `for ` statement is such an * iterator *. The function :func: ` list `
146
- is another; it creates lists from iterables ::
144
+ obtain successive items until the supply is exhausted. We have seen that
145
+ the :keyword: `for ` statement is such a construct, while an example of function
146
+ that takes an iterable is :func: ` sum ` ::
147
147
148
+ >>> sum(range(4)) # 0 + 1 + 2 + 3
149
+ 6
148
150
149
- >>> list(range(5))
150
- [0, 1, 2, 3, 4]
151
+ Later we will see more functions that return iterables and take iterables as
152
+ arguments. Lastly, maybe you are curious about how to get a list from a range.
153
+ Here is the solution::
151
154
152
- Later we will see more functions that return iterables and take iterables as argument.
155
+ >>> list(range(4))
156
+ [0, 1, 2, 3]
153
157
158
+ In chapter :ref: `tut-structures `, we will discuss in more detail about
159
+ :func: `list `.
154
160
155
161
.. _tut-break :
156
162
@@ -161,7 +167,7 @@ The :keyword:`break` statement, like in C, breaks out of the innermost enclosing
161
167
:keyword: `for ` or :keyword: `while ` loop.
162
168
163
169
Loop statements may have an :keyword: `!else ` clause; it is executed when the loop
164
- terminates through exhaustion of the list (with :keyword: `for `) or when the
170
+ terminates through exhaustion of the iterable (with :keyword: `for `) or when the
165
171
condition becomes false (with :keyword: `while `), but not when the loop is
166
172
terminated by a :keyword: `break ` statement. This is exemplified by the
167
173
following loop, which searches for prime numbers::
@@ -188,8 +194,8 @@ following loop, which searches for prime numbers::
188
194
the :keyword: `for ` loop, **not ** the :keyword: `if ` statement.)
189
195
190
196
When used with a loop, the ``else `` clause has more in common with the
191
- ``else `` clause of a :keyword: `try ` statement than it does that of
192
- :keyword: `if ` statements: a :keyword: `! try ` statement's ``else `` clause runs
197
+ ``else `` clause of a :keyword: `try ` statement than it does with that of
198
+ :keyword: `if ` statements: a :keyword: `try ` statement's ``else `` clause runs
193
199
when no exception occurs, and a loop's ``else `` clause runs when no ``break ``
194
200
occurs. For more on the :keyword: `!try ` statement and exceptions, see
195
201
:ref: `tut-handling `.
0 commit comments