Skip to content

bpo-39971: Change examples to be runnable #32172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 25, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add interactive prompts
  • Loading branch information
slateny committed Mar 29, 2022
commit d3ce63191eb502bb39bc7dd4b1b546941b484473
26 changes: 16 additions & 10 deletions Doc/howto/functional.rst
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,15 @@ line of a file like this::
Sets can take their contents from an iterable and let you iterate over the set's
elements::

S = {2, 3, 5, 7, 11, 13}
for i in S:
print(i)
>>> S = {2, 3, 5, 7, 11, 13}
>>> for i in S:
... print(i)
2
3
5
7
11
13



Expand All @@ -335,18 +341,18 @@ List comprehensions and generator expressions (short form: "listcomps" and
functional programming language Haskell (https://www.haskell.org/). You can strip
all the whitespace from a stream of strings with the following code::

line_list = [' line 1\n', 'line 2 \n', ' \n', '']
>>> line_list = [' line 1\n', 'line 2 \n', ' \n', '']

# Generator expression -- returns iterator
stripped_iter = (line.strip() for line in line_list)
>>> # Generator expression -- returns iterator
>>> stripped_iter = (line.strip() for line in line_list)

# List comprehension -- returns list
stripped_list = [line.strip() for line in line_list]
>>> # List comprehension -- returns list
>>> stripped_list = [line.strip() for line in line_list]

You can select only certain elements by adding an ``"if"`` condition::

stripped_list = [line.strip() for line in line_list
if line != ""]
>>> stripped_list = [line.strip() for line in line_list
... if line != ""]

With a list comprehension, you get back a Python list; ``stripped_list`` is a
list containing the resulting lines, not an iterator. Generator expressions
Expand Down
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