summaryrefslogtreecommitdiffstats
path: root/Doc/tutorial/introduction.rst
diff options
context:
space:
mode:
Diffstat (limited to 'Doc/tutorial/introduction.rst')
-rw-r--r--Doc/tutorial/introduction.rst162
1 files changed, 62 insertions, 100 deletions
diff --git a/Doc/tutorial/introduction.rst b/Doc/tutorial/introduction.rst
index 1871dd1..b6d94ac 100644
--- a/Doc/tutorial/introduction.rst
+++ b/Doc/tutorial/introduction.rst
@@ -1,4 +1,4 @@
-.. _tut-informal:
+.. _tut-informal:
**********************************
An Informal Introduction to Python
@@ -54,11 +54,23 @@ operators ``+``, ``-``, ``*`` and ``/`` work just like in most other languages
>>> 2+2 # and a comment on the same line as code
4
>>> (50-5*6)/4
- 5
+ 5.0
+ >>> 8/5 # Fractions aren't lost when dividing integers
+ 1.6
+
+Note: You might not see exactly the same result; floating point results can
+differ from one machine to another. We will say more later about controlling
+the appearance of floating point output. See also :ref:`tut-fp-issues` for a
+full discussion of some of the subtleties of floating point numbers and their
+representations.
+
+To do integer division and get an integer result,
+discarding any fractional result, there is another operator, ``//``::
+
>>> # Integer division returns the floor:
- ... 7/3
+ ... 7//3
2
- >>> 7/-3
+ >>> 7//-3
-3
The equal sign (``'='``) is used to assign a value to a variable. Afterwards, no
@@ -102,7 +114,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)
@@ -122,9 +134,9 @@ and imaginary part. To extract these parts from a complex number *z*, use
0.5
The conversion functions to floating point and integer (:func:`float`,
-:func:`int` and :func:`long`) don't work for complex numbers --- there is no one
-correct way to convert a complex number to a real number. Use ``abs(z)`` to get
-its magnitude (as a float) or ``z.real`` to get its real part. ::
+:func:`int`) don't work for complex numbers --- there is not one correct way to
+convert a complex number to a real number. Use ``abs(z)`` to get its magnitude
+(as a float) or ``z.real`` to get its real part::
>>> a=3.0+4.0j
>>> float(a)
@@ -181,7 +193,7 @@ The interpreter prints the result of string operations in the same way as they
are typed for input: inside quotes, and with quotes and other funny characters
escaped by backslashes, to show the precise value. The string is enclosed in
double quotes if the string contains a single quote and no double quotes, else
-it's enclosed in single quotes. The :keyword:`print` statement produces a more
+it's enclosed in single quotes. The :func:`print` function produces a more
readable output for such input strings.
String literals can span multiple lines in several ways. Continuation lines can
@@ -193,7 +205,7 @@ next line is a logical continuation of the line::
Note that whitespace at the beginning of the line is\
significant."
- print hello
+ print(hello)
Note that newlines still need to be embedded in the string using ``\n`` -- the
newline following the trailing backslash is discarded. This example would print
@@ -207,13 +219,14 @@ the following:
Or, strings can be surrounded in a pair of matching triple-quotes: ``"""`` or
``'''``. End of lines do not need to be escaped when using triple-quotes, but
-they will be included in the string. ::
+they will be included in the string. So the following uses one escape to
+avoid an unwanted initial blank line. ::
- print """
+ print("""\
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
- """
+ """)
produces the following output:
@@ -230,7 +243,7 @@ in the source, are both included in the string as data. Thus, the example::
hello = r"This is a rather long string containing\n\
several lines of text much as you would do in C."
- print hello
+ print(hello)
would print:
@@ -264,8 +277,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. Like 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'
@@ -288,11 +302,11 @@ position in the string results in an error::
>>> word[0] = 'x'
Traceback (most recent call last):
File "<stdin>", line 1, in ?
- TypeError: object does not support item assignment
+ TypeError: 'str' object does not support item assignment
>>> word[:1] = 'Splat'
Traceback (most recent call last):
File "<stdin>", line 1, in ?
- TypeError: object does not support slice assignment
+ TypeError: 'str' object does not support slice assignment
However, creating a new string with the combined content is easy and efficient::
@@ -377,35 +391,32 @@ The built-in function :func:`len` returns the length of a string::
.. seealso::
:ref:`typesseq`
- Strings, and the Unicode strings described in the next section, are
- examples of *sequence types*, and support the common operations supported
- by such types.
+ Strings are examples of *sequence types*, and support the common
+ operations supported by such types.
:ref:`string-methods`
- Both strings and Unicode strings support a large number of methods for
+ Strings support a large number of methods for
basic transformations and searching.
- :ref:`new-string-formatting`
+ :ref:`string-formatting`
Information about string formatting with :meth:`str.format` is described
here.
- :ref:`string-formatting`
+ :ref:`old-string-formatting`
The old formatting operations invoked when strings and Unicode strings are
the left operand of the ``%`` operator are described in more detail here.
.. _tut-unicodestrings:
-Unicode Strings
----------------
+About Unicode
+-------------
-.. sectionauthor:: Marc-Andre Lemburg <mal@lemburg.com>
+.. sectionauthor:: Marc-André Lemburg <mal@lemburg.com>
-Starting with Python 2.0 a new data type for storing text data is available to
-the programmer: the Unicode object. It can be used to store and manipulate
-Unicode data (see http://www.unicode.org/) and integrates well with the existing
-string objects, providing auto-conversions where necessary.
+Starting with Python 3.0 all strings support Unicode (see
+http://www.unicode.org/).
Unicode has the advantage of providing one ordinal for every character in every
script used in modern and ancient texts. Previously, there were only 256
@@ -415,19 +426,12 @@ confusion especially with respect to internationalization (usually written as
``i18n`` --- ``'i'`` + 18 characters + ``'n'``) of software. Unicode solves
these problems by defining one code page for all scripts.
-Creating Unicode strings in Python is just as simple as creating normal
-strings::
-
- >>> u'Hello World !'
- u'Hello World !'
-
-The small ``'u'`` in front of the quote indicates that a Unicode string is
-supposed to be created. If you want to include special characters in the string,
+If you want to include special characters in a string,
you can do so by using the Python *Unicode-Escape* encoding. The following
example shows how::
- >>> u'Hello\u0020World !'
- u'Hello World !'
+ >>> 'Hello\u0020World !'
+ 'Hello World !'
The escape sequence ``\u0020`` indicates to insert the Unicode character with
the ordinal value 0x0020 (the space character) at the given position.
@@ -438,59 +442,15 @@ Latin-1 encoding that is used in many Western countries, you will find it
convenient that the lower 256 characters of Unicode are the same as the 256
characters of Latin-1.
-For experts, there is also a raw mode just like the one for normal strings. You
-have to prefix the opening quote with 'ur' to have Python use the
-*Raw-Unicode-Escape* encoding. It will only apply the above ``\uXXXX``
-conversion if there is an uneven number of backslashes in front of the small
-'u'. ::
-
- >>> ur'Hello\u0020World !'
- u'Hello World !'
- >>> ur'Hello\\u0020World !'
- u'Hello\\\\u0020World !'
-
-The raw mode is most useful when you have to enter lots of backslashes, as can
-be necessary in regular expressions.
-
Apart from these standard encodings, Python provides a whole set of other ways
of creating Unicode strings on the basis of a known encoding.
-.. index:: builtin: unicode
-
-The built-in function :func:`unicode` provides access to all registered Unicode
-codecs (COders and DECoders). Some of the more well known encodings which these
-codecs can convert are *Latin-1*, *ASCII*, *UTF-8*, and *UTF-16*. The latter two
-are variable-length encodings that store each Unicode character in one or more
-bytes. The default encoding is normally set to ASCII, which passes through
-characters in the range 0 to 127 and rejects any other characters with an error.
-When a Unicode string is printed, written to a file, or converted with
-:func:`str`, conversion takes place using this default encoding. ::
-
- >>> u"abc"
- u'abc'
- >>> str(u"abc")
- 'abc'
- >>> u"äöü"
- u'\xe4\xf6\xfc'
- >>> str(u"äöü")
- Traceback (most recent call last):
- File "<stdin>", line 1, in ?
- UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
-
-To convert a Unicode string into an 8-bit string using a specific encoding,
-Unicode objects provide an :func:`encode` method that takes one argument, the
+To convert a string into a sequence of bytes using a specific encoding,
+string objects provide an :func:`encode` method that takes one argument, the
name of the encoding. Lowercase names for encodings are preferred. ::
- >>> u"äöü".encode('utf-8')
- '\xc3\xa4\xc3\xb6\xc3\xbc'
-
-If you have data in a specific encoding and want to produce a corresponding
-Unicode string from it, you can use the :func:`unicode` function with the
-encoding name as the second argument. ::
-
- >>> unicode('\xc3\xa4\xc3\xb6\xc3\xbc', 'utf-8')
- u'\xe4\xf6\xfc'
-
+ >>> "Äpfel".encode('utf-8')
+ b'\xc3\x84pfel'
.. _tut-lists:
@@ -578,7 +538,10 @@ example::
[2, 3]
>>> p[1][0]
2
- >>> p[1].append('xtra') # See section 5.1
+
+You can add something to the end of the list::
+
+ >>> p[1].append('xtra')
>>> p
[1, [2, 3, 'xtra'], 4]
>>> q
@@ -601,7 +564,7 @@ series as follows::
... # the sum of two elements defines the next
... a, b = 0, 1
>>> while b < 10:
- ... print b
+ ... print(b)
... a, b = b, a+b
...
1
@@ -637,24 +600,23 @@ This example introduces several new features.
guess when you have typed the last line). Note that each line within a basic
block must be indented by the same amount.
-* The :keyword:`print` statement writes the value of the expression(s) it is
+* The :func:`print` function writes the value of the expression(s) it is
given. It differs from just writing the expression you want to write (as we did
- earlier in the calculator examples) in the way it handles multiple expressions
+ earlier in the calculator examples) in the way it handles multiple
+ expressions, floating point quantities,
and strings. Strings are printed without quotes, and a space is inserted
between items, so you can format things nicely, like this::
>>> i = 256*256
- >>> print 'The value of i is', i
+ >>> print('The value of i is', i)
The value of i is 65536
- A trailing comma avoids the newline after the output::
+ The keyword *end* can be used to avoid the newline after the output, or end
+ the output with a different string::
>>> a, b = 0, 1
>>> while b < 1000:
- ... print b,
+ ... print(b, end=',')
... a, b = b, a+b
...
- 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
-
- Note that the interpreter inserts a newline before it prints the next prompt if
- the last line was not completed.
+ 1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,