@@ -315,9 +315,15 @@ line of a file like this::
315315Sets can take their contents from an iterable and let you iterate over the set's
316316elements::
317317
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
321327
322328
323329
@@ -335,18 +341,18 @@ List comprehensions and generator expressions (short form: "listcomps" and
335341functional programming language Haskell (https://www.haskell.org/). You can strip
336342all the whitespace from a stream of strings with the following code::
337343
338- line_list = [' line 1\n', 'line 2 \n', ... ]
344+ >>> line_list = [' line 1\n', 'line 2 \n', ' \n', '' ]
339345
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)
342348
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]
345351
346352You can select only certain elements by adding an ``"if" `` condition::
347353
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 != ""]
350356
351357With a list comprehension, you get back a Python list; ``stripped_list `` is a
352358list containing the resulting lines, not an iterator. Generator expressions
@@ -363,7 +369,8 @@ have the form::
363369 if condition1
364370 for expr2 in sequence2
365371 if condition2
366- for expr3 in sequence3 ...
372+ for expr3 in sequence3
373+ ...
367374 if condition3
368375 for exprN in sequenceN
369376 if conditionN )
0 commit comments