Skip to content

Commit 8b77681

Browse files
bpo-39971: Change examples to be runnable (GH-32172)
(cherry picked from commit c57a1c7) Co-authored-by: Stanley <46876382+slateny@users.noreply.github.com>
1 parent 0a1517d commit 8b77681

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

Doc/howto/functional.rst

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,15 @@ line of a file like this::
315315
Sets can take their contents from an iterable and let you iterate over the set's
316316
elements::
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
335341
functional programming language Haskell (https://www.haskell.org/). You can strip
336342
all 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

346352
You 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

351357
With a list comprehension, you get back a Python list; ``stripped_list`` is a
352358
list 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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy