Tutorial formatting patch by Robin Stocker.
This commit is contained in:
parent
9d4ba3970f
commit
e4ac7504c9
|
@ -769,7 +769,7 @@ Examples::
|
|||
>>> valedictorian = max((student.gpa, student.name) for student in graduates)
|
||||
|
||||
>>> data = 'golf'
|
||||
>>> list(data[i] for i in range(len(data)-1,-1,-1))
|
||||
>>> list(data[i] for i in range(len(data)-1, -1, -1))
|
||||
['f', 'l', 'o', 'g']
|
||||
|
||||
|
||||
|
|
|
@ -210,7 +210,7 @@ boundary::
|
|||
... """Print a Fibonacci series up to n."""
|
||||
... a, b = 0, 1
|
||||
... while b < n:
|
||||
... print(b,end=' ')
|
||||
... print(b, end=' ')
|
||||
... a, b = b, a+b
|
||||
... print()
|
||||
...
|
||||
|
@ -389,7 +389,7 @@ Functions can also be called using keyword arguments of the form ``keyword =
|
|||
value``. For instance, the following function::
|
||||
|
||||
def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
|
||||
print("-- This parrot wouldn't", action, end= ' ')
|
||||
print("-- This parrot wouldn't", action, end=' ')
|
||||
print("if you put", voltage, "volts through it.")
|
||||
print("-- Lovely plumage, the", type)
|
||||
print("-- It's", state, "!")
|
||||
|
@ -481,7 +481,7 @@ Normally, these ``variadic`` arguments will be last in the list of formal
|
|||
parameters, because they scoop up all remaining input arguments that are
|
||||
passed to the function. Any formal parameters which occur after the ``*args``
|
||||
parameter are 'keyword-only' arguments, meaning that they can only be used as
|
||||
keywords rather than positional arguments.::
|
||||
keywords rather than positional arguments. ::
|
||||
|
||||
>>> def concat(*args, sep="/"):
|
||||
... return sep.join(args)
|
||||
|
@ -513,7 +513,7 @@ In the same fashion, dictionaries can deliver keyword arguments with the ``**``\
|
|||
-operator::
|
||||
|
||||
>>> def parrot(voltage, state='a stiff', action='voom'):
|
||||
... print("-- This parrot wouldn't", action,end=' ')
|
||||
... print("-- This parrot wouldn't", action, end=' ')
|
||||
... print("if you put", voltage, "volts through it.", end=' ')
|
||||
... print("E's", state, "!")
|
||||
...
|
||||
|
|
|
@ -234,7 +234,7 @@ Here we take a list of numbers and return a list of three times each number::
|
|||
|
||||
Now we get a little fancier::
|
||||
|
||||
>>> [[x,x**2] for x in vec]
|
||||
>>> [[x, x**2] for x in vec]
|
||||
[[2, 4], [4, 16], [6, 36]]
|
||||
|
||||
Here we apply a method call to each item in a sequence::
|
||||
|
@ -243,7 +243,7 @@ Here we apply a method call to each item in a sequence::
|
|||
>>> [weapon.strip() for weapon in freshfruit]
|
||||
['banana', 'loganberry', 'passion fruit']
|
||||
|
||||
Using the if-clause we can filter the stream::
|
||||
Using the :keyword:`if` clause we can filter the stream::
|
||||
|
||||
>>> [3*x for x in vec if x > 3]
|
||||
[12, 18]
|
||||
|
@ -260,7 +260,7 @@ Tuples can often be created without their parentheses, but not here::
|
|||
>>> [(x, x**2) for x in vec]
|
||||
[(2, 4), (4, 16), (6, 36)]
|
||||
|
||||
Here are some nested for's and other fancy behavior::
|
||||
Here are some nested for loops and other fancy behavior::
|
||||
|
||||
>>> vec1 = [2, 4, 6]
|
||||
>>> vec2 = [4, 3, -9]
|
||||
|
@ -273,7 +273,7 @@ Here are some nested for's and other fancy behavior::
|
|||
|
||||
List comprehensions can be applied to complex expressions and nested functions::
|
||||
|
||||
>>> [str(round(355/113.0, i)) for i in range(1,6)]
|
||||
>>> [str(round(355/113.0, i)) for i in range(1, 6)]
|
||||
['3.1', '3.14', '3.142', '3.1416', '3.14159']
|
||||
|
||||
|
||||
|
@ -469,7 +469,7 @@ with the :func:`zip` function. ::
|
|||
To loop over a sequence in reverse, first specify the sequence in a forward
|
||||
direction and then call the :func:`reversed` function. ::
|
||||
|
||||
>>> for i in reversed(range(1,10,2)):
|
||||
>>> for i in reversed(range(1, 10, 2)):
|
||||
... print(i)
|
||||
...
|
||||
9
|
||||
|
|
|
@ -78,7 +78,7 @@ Some examples::
|
|||
Here are two ways to write a table of squares and cubes::
|
||||
|
||||
>>> for x in range(1, 11):
|
||||
... print(repr(x).rjust(2), repr(x*x).rjust(3),end=' ')
|
||||
... print(repr(x).rjust(2), repr(x*x).rjust(3), end=' ')
|
||||
... # Note use of 'end' on previous line
|
||||
... print(repr(x*x*x).rjust(4))
|
||||
...
|
||||
|
@ -93,7 +93,7 @@ Here are two ways to write a table of squares and cubes::
|
|||
9 81 729
|
||||
10 100 1000
|
||||
|
||||
>>> for x in range(1,11):
|
||||
>>> for x in range(1, 11):
|
||||
... print('%2d %3d %4d' % (x, x*x, x*x*x))
|
||||
...
|
||||
1 1 1
|
||||
|
|
|
@ -111,7 +111,7 @@ of ``j`` or ``J``. Complex numbers with a nonzero real component are written as
|
|||
|
||||
>>> 1j * 1J
|
||||
(-1+0j)
|
||||
>>> 1j * complex(0,1)
|
||||
>>> 1j * complex(0, 1)
|
||||
(-1+0j)
|
||||
>>> 3+1j*3
|
||||
(3+3j)
|
||||
|
@ -271,8 +271,9 @@ with two literals, not with arbitrary string expressions::
|
|||
|
||||
Strings can be subscripted (indexed); like in C, the first character of a string
|
||||
has subscript (index) 0. There is no separate character type; a character is
|
||||
simply a string of size one. As in Icon, substrings can be specified with the
|
||||
*slice notation*: two indices separated by a colon. ::
|
||||
simply a string of size one. As in the Icon programming language, substrings
|
||||
can be specified with the *slice notation*: two indices separated by a colon.
|
||||
::
|
||||
|
||||
>>> word[4]
|
||||
'A'
|
||||
|
@ -523,7 +524,7 @@ example::
|
|||
|
||||
You can add something to the end of the list::
|
||||
|
||||
>>> p[1].append('xtra')
|
||||
>>> p[1].append('xtra')
|
||||
>>> p
|
||||
[1, [2, 3, 'xtra'], 4]
|
||||
>>> q
|
||||
|
|
|
@ -224,9 +224,9 @@ Some tips for experts:
|
|||
files when :option:`-O` is used) for all modules in a directory.
|
||||
|
||||
* If using Python in a parallel processing system with a shared file system,
|
||||
you need to patch python to disable the creation of the compiled files
|
||||
you need to patch Python to disable the creation of the compiled files
|
||||
because otherwise the multiple Python interpreters will encounter race
|
||||
conditions in creating them.
|
||||
conditions in creating them.
|
||||
|
||||
|
||||
.. _tut-standardmodules:
|
||||
|
|
|
@ -184,7 +184,7 @@ tasks in background while the main program continues to run::
|
|||
f = zipfile.ZipFile(self.outfile, 'w', zipfile.ZIP_DEFLATED)
|
||||
f.write(self.infile)
|
||||
f.close()
|
||||
print('Finished background zip of: ', self.infile)
|
||||
print('Finished background zip of:', self.infile)
|
||||
|
||||
background = AsyncZip('mydata.txt', 'myarchive.zip')
|
||||
background.start()
|
||||
|
|
Loading…
Reference in New Issue