@@ -315,9 +315,15 @@ line of a file like this::
315
315
Sets can take their contents from an iterable and let you iterate over the set's
316
316
elements::
317
317
318
- S = {2, 3, 5, 7, 11, 13}
319
- for i in S:
320
- print(i)
318
+ >>> S = {2, 3, 5, 7, 11, 13}
319
+ >>> for i in S:
320
+ ... print(i)
321
+ 2
322
+ 3
323
+ 5
324
+ 7
325
+ 11
326
+ 13
321
327
322
328
323
329
@@ -335,18 +341,18 @@ List comprehensions and generator expressions (short form: "listcomps" and
335
341
functional programming language Haskell (https://www.haskell.org/). You can strip
336
342
all the whitespace from a stream of strings with the following code::
337
343
338
- line_list = [' line 1\n', 'line 2 \n', ... ]
344
+ >>> line_list = [' line 1\n', 'line 2 \n', ' \n', '' ]
339
345
340
- # Generator expression -- returns iterator
341
- stripped_iter = (line.strip() for line in line_list)
346
+ >>> # Generator expression -- returns iterator
347
+ >>> stripped_iter = (line.strip() for line in line_list)
342
348
343
- # List comprehension -- returns list
344
- stripped_list = [line.strip() for line in line_list]
349
+ >>> # List comprehension -- returns list
350
+ >>> stripped_list = [line.strip() for line in line_list]
345
351
346
352
You can select only certain elements by adding an ``"if" `` condition::
347
353
348
- stripped_list = [line.strip() for line in line_list
349
- if line != ""]
354
+ >>> stripped_list = [line.strip() for line in line_list
355
+ ... if line != ""]
350
356
351
357
With a list comprehension, you get back a Python list; ``stripped_list `` is a
352
358
list containing the resulting lines, not an iterator. Generator expressions
@@ -363,7 +369,8 @@ have the form::
363
369
if condition1
364
370
for expr2 in sequence2
365
371
if condition2
366
- for expr3 in sequence3 ...
372
+ for expr3 in sequence3
373
+ ...
367
374
if condition3
368
375
for exprN in sequenceN
369
376
if conditionN )
0 commit comments