diff options
Diffstat (limited to 'Doc/tutorial')
-rw-r--r-- | Doc/tutorial/classes.rst | 9 | ||||
-rw-r--r-- | Doc/tutorial/floatingpoint.rst | 11 | ||||
-rw-r--r-- | Doc/tutorial/inputoutput.rst | 12 | ||||
-rw-r--r-- | Doc/tutorial/interpreter.rst | 14 | ||||
-rw-r--r-- | Doc/tutorial/introduction.rst | 6 | ||||
-rw-r--r-- | Doc/tutorial/stdlib.rst | 2 |
6 files changed, 30 insertions, 24 deletions
diff --git a/Doc/tutorial/classes.rst b/Doc/tutorial/classes.rst index 82735df..a328ab2 100644 --- a/Doc/tutorial/classes.rst +++ b/Doc/tutorial/classes.rst @@ -717,7 +717,7 @@ object that defines the method :meth:`__next__` which accesses elements in the container one at a time. When there are no more elements, :meth:`__next__` raises a :exc:`StopIteration` exception which tells the :keyword:`for` loop to terminate. You can call the :meth:`__next__` method using the :func:`next` -builtin; this example shows how it all works:: +built-in function; this example shows how it all works:: >>> s = 'abc' >>> it = iter(s) @@ -730,7 +730,6 @@ builtin; this example shows how it all works:: >>> next(it) 'c' >>> next(it) - Traceback (most recent call last): File "<stdin>", line 1, in ? next(it) @@ -742,7 +741,7 @@ returns an object with a :meth:`__next__` method. If the class defines :meth:`__next__`, then :meth:`__iter__` can just return ``self``:: class Reverse: - "Iterator for looping over a sequence backwards" + """Iterator for looping over a sequence backwards.""" def __init__(self, data): self.data = data self.index = len(data) @@ -754,6 +753,8 @@ returns an object with a :meth:`__next__` method. If the class defines self.index = self.index - 1 return self.data[self.index] +:: + >>> rev = Reverse('spam') >>> iter(rev) <__main__.Reverse object at 0x00A1DB50> @@ -782,6 +783,8 @@ easy to create:: for index in range(len(data)-1, -1, -1): yield data[index] +:: + >>> for char in reverse('golf'): ... print(char) ... diff --git a/Doc/tutorial/floatingpoint.rst b/Doc/tutorial/floatingpoint.rst index c06568e..863fb28 100644 --- a/Doc/tutorial/floatingpoint.rst +++ b/Doc/tutorial/floatingpoint.rst @@ -92,18 +92,17 @@ thing in all languages that support your hardware's floating-point arithmetic (although some languages may not *display* the difference by default, or in all output modes). -Python's built-in :func:`str` function produces only 12 significant digits, and -you may wish to use that instead. It's unusual for ``eval(str(x))`` to -reproduce *x*, but the output may be more pleasant to look at:: +For more pleasant output, you may may wish to use string formatting to produce a limited number of significant digits:: - >>> str(math.pi) + >>> format(math.pi, '.12g') # give 12 significant digits '3.14159265359' + >>> format(math.pi, '.2f') # give 2 digits after the point + '3.14' + >>> repr(math.pi) '3.141592653589793' - >>> format(math.pi, '.2f') - '3.14' It's important to realize that this is, in a real sense, an illusion: you're simply rounding the *display* of the true machine value. diff --git a/Doc/tutorial/inputoutput.rst b/Doc/tutorial/inputoutput.rst index abe1ce0..00f5aea 100644 --- a/Doc/tutorial/inputoutput.rst +++ b/Doc/tutorial/inputoutput.rst @@ -40,8 +40,8 @@ which can be read by the interpreter (or will force a :exc:`SyntaxError` if there is not equivalent syntax). For objects which don't have a particular representation for human consumption, :func:`str` will return the same value as :func:`repr`. Many values, such as numbers or structures like lists and -dictionaries, have the same representation using either function. Strings and -floating point numbers, in particular, have two distinct representations. +dictionaries, have the same representation using either function. Strings, in +particular, have two distinct representations. Some examples:: @@ -50,9 +50,7 @@ Some examples:: 'Hello, world.' >>> repr(s) "'Hello, world.'" - >>> str(1.0/7.0) - '0.142857142857' - >>> repr(1.0/7.0) + >>> str(1/7) '0.14285714285714285' >>> x = 10 * 3.25 >>> y = 200 * 200 @@ -162,7 +160,7 @@ Positional and keyword arguments can be arbitrarily combined:: An optional ``':'`` and format specifier can follow the field name. This allows greater control over how the value is formatted. The following example -truncates Pi to three places after the decimal. +rounds Pi to three places after the decimal. >>> import math >>> print('The value of PI is approximately {0:.3f}.'.format(math.pi)) @@ -207,7 +205,7 @@ Old string formatting --------------------- The ``%`` operator can also be used for string formatting. It interprets the -left argument much like a :cfunc:`sprintf`\ -style format string to be applied +left argument much like a :c:func:`sprintf`\ -style format string to be applied to the right argument, and returns the string resulting from this formatting operation. For example:: diff --git a/Doc/tutorial/interpreter.rst b/Doc/tutorial/interpreter.rst index 94d7562..8d743de 100644 --- a/Doc/tutorial/interpreter.rst +++ b/Doc/tutorial/interpreter.rst @@ -10,11 +10,11 @@ Using the Python Interpreter Invoking the Interpreter ======================== -The Python interpreter is usually installed as :file:`/usr/local/bin/python3.1` +The Python interpreter is usually installed as :file:`/usr/local/bin/python3.2` on those machines where it is available; putting :file:`/usr/local/bin` in your Unix shell's search path makes it possible to start it by typing the command :: - python3.1 + python3.2 to the shell. [#]_ Since the choice of the directory where the interpreter lives is an installation option, other places are possible; check with your local @@ -22,11 +22,11 @@ Python guru or system administrator. (E.g., :file:`/usr/local/python` is a popular alternative location.) On Windows machines, the Python installation is usually placed in -:file:`C:\\Python31`, though you can change this when you're running the +:file:`C:\\Python32`, though you can change this when you're running the installer. To add this directory to your path, you can type the following command into the command prompt in a DOS box:: - set path=%path%;C:\python31 + set path=%path%;C:\python32 Typing an end-of-file character (:kbd:`Control-D` on Unix, :kbd:`Control-Z` on Windows) at the primary prompt causes the interpreter to exit with a zero exit @@ -94,8 +94,8 @@ with the *secondary prompt*, by default three dots (``...``). The interpreter prints a welcome message stating its version number and a copyright notice before printing the first prompt:: - $ python3.1 - Python 3.1 (py3k, Sep 12 2007, 12:21:02) + $ python3.2 + Python 3.2 (py3k, Sep 12 2007, 12:21:02) [GCC 3.4.6 20060404 (Red Hat 3.4.6-8)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> @@ -148,7 +148,7 @@ Executable Python Scripts On BSD'ish Unix systems, Python scripts can be made directly executable, like shell scripts, by putting the line :: - #! /usr/bin/env python3.1 + #! /usr/bin/env python3.2 (assuming that the interpreter is on the user's :envvar:`PATH`) at the beginning of the script and giving the file an executable mode. The ``#!`` must be the diff --git a/Doc/tutorial/introduction.rst b/Doc/tutorial/introduction.rst index 3e42cee..44519da 100644 --- a/Doc/tutorial/introduction.rst +++ b/Doc/tutorial/introduction.rst @@ -483,6 +483,12 @@ concatenated and so on:: >>> 3*a[:3] + ['Boo!'] ['spam', 'eggs', 100, 'spam', 'eggs', 100, 'spam', 'eggs', 100, 'Boo!'] +All slice operations return a new list containing the requested elements. This +means that the following slice returns a shallow copy of the list *a*:: + + >>> a[:] + ['spam', 'eggs', 100, 1234] + Unlike strings, which are *immutable*, it is possible to change individual elements of a list:: diff --git a/Doc/tutorial/stdlib.rst b/Doc/tutorial/stdlib.rst index b138f65..9729743 100644 --- a/Doc/tutorial/stdlib.rst +++ b/Doc/tutorial/stdlib.rst @@ -72,7 +72,7 @@ three`` at the command line:: The :mod:`getopt` module processes *sys.argv* using the conventions of the Unix :func:`getopt` function. More powerful and flexible command line processing is -provided by the :mod:`optparse` module. +provided by the :mod:`argparse` module. .. _tut-stderr: |