From eca7410ab44fb932f7dcc8933c8c611d74044cb6 Mon Sep 17 00:00:00 2001 From: slateny <46876382+slateny@users.noreply.github.com> Date: Mon, 28 Mar 2022 19:20:44 -0700 Subject: [PATCH 1/2] Change examples to be runnable --- Doc/howto/functional.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Doc/howto/functional.rst b/Doc/howto/functional.rst index c7f8bc8f17f43b..105926dd398ae6 100644 --- a/Doc/howto/functional.rst +++ b/Doc/howto/functional.rst @@ -335,7 +335,7 @@ 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', ...] + line_list = [' line 1\n', 'line 2 \n', ' \n', ''] # Generator expression -- returns iterator stripped_iter = (line.strip() for line in line_list) @@ -363,7 +363,8 @@ have the form:: if condition1 for expr2 in sequence2 if condition2 - for expr3 in sequence3 ... + for expr3 in sequence3 + ... if condition3 for exprN in sequenceN if conditionN ) From d3ce63191eb502bb39bc7dd4b1b546941b484473 Mon Sep 17 00:00:00 2001 From: slateny <46876382+slateny@users.noreply.github.com> Date: Mon, 28 Mar 2022 19:36:19 -0700 Subject: [PATCH 2/2] Add interactive prompts --- Doc/howto/functional.rst | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/Doc/howto/functional.rst b/Doc/howto/functional.rst index 105926dd398ae6..3d4bf8399da6c2 100644 --- a/Doc/howto/functional.rst +++ b/Doc/howto/functional.rst @@ -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 @@ -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