mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
bpo-39971: Change examples to be runnable (GH-32172)
(cherry picked from commit c57a1c76d7
)
Co-authored-by: Stanley <46876382+slateny@users.noreply.github.com>
This commit is contained in:
parent
0a1517dc41
commit
8b77681469
1 changed files with 18 additions and 11 deletions
|
@ -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
|
Sets can take their contents from an iterable and let you iterate over the set's
|
||||||
elements::
|
elements::
|
||||||
|
|
||||||
S = {2, 3, 5, 7, 11, 13}
|
>>> S = {2, 3, 5, 7, 11, 13}
|
||||||
for i in S:
|
>>> for i in S:
|
||||||
print(i)
|
... 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
|
functional programming language Haskell (https://www.haskell.org/). You can strip
|
||||||
all the whitespace from a stream of strings with the following code::
|
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
|
>>> # Generator expression -- returns iterator
|
||||||
stripped_iter = (line.strip() for line in line_list)
|
>>> stripped_iter = (line.strip() for line in line_list)
|
||||||
|
|
||||||
# List comprehension -- returns list
|
>>> # List comprehension -- returns list
|
||||||
stripped_list = [line.strip() for line in line_list]
|
>>> stripped_list = [line.strip() for line in line_list]
|
||||||
|
|
||||||
You can select only certain elements by adding an ``"if"`` condition::
|
You can select only certain elements by adding an ``"if"`` condition::
|
||||||
|
|
||||||
stripped_list = [line.strip() for line in line_list
|
>>> stripped_list = [line.strip() for line in line_list
|
||||||
if line != ""]
|
... if line != ""]
|
||||||
|
|
||||||
With a list comprehension, you get back a Python list; ``stripped_list`` is a
|
With a list comprehension, you get back a Python list; ``stripped_list`` is a
|
||||||
list containing the resulting lines, not an iterator. Generator expressions
|
list containing the resulting lines, not an iterator. Generator expressions
|
||||||
|
@ -363,7 +369,8 @@ have the form::
|
||||||
if condition1
|
if condition1
|
||||||
for expr2 in sequence2
|
for expr2 in sequence2
|
||||||
if condition2
|
if condition2
|
||||||
for expr3 in sequence3 ...
|
for expr3 in sequence3
|
||||||
|
...
|
||||||
if condition3
|
if condition3
|
||||||
for exprN in sequenceN
|
for exprN in sequenceN
|
||||||
if conditionN )
|
if conditionN )
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue