2007-08-15 11:28:22 -03:00
|
|
|
.. _tut-informal:
|
|
|
|
|
|
|
|
**********************************
|
|
|
|
An Informal Introduction to Python
|
|
|
|
**********************************
|
|
|
|
|
|
|
|
In the following examples, input and output are distinguished by the presence or
|
|
|
|
absence of prompts (``>>>`` and ``...``): to repeat the example, you must type
|
|
|
|
everything after the prompt, when the prompt appears; lines that do not begin
|
|
|
|
with a prompt are output from the interpreter. Note that a secondary prompt on a
|
|
|
|
line by itself in an example means you must type a blank line; this is used to
|
|
|
|
end a multi-line command.
|
|
|
|
|
|
|
|
Many of the examples in this manual, even those entered at the interactive
|
|
|
|
prompt, include comments. Comments in Python start with the hash character,
|
2008-09-13 14:18:21 -03:00
|
|
|
``#``, and extend to the end of the physical line. A comment may appear at the
|
|
|
|
start of a line or following whitespace or code, but not within a string
|
Merged revisions 59605-59624 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59606 | georg.brandl | 2007-12-29 11:57:00 +0100 (Sat, 29 Dec 2007) | 2 lines
Some cleanup in the docs.
........
r59611 | martin.v.loewis | 2007-12-29 19:49:21 +0100 (Sat, 29 Dec 2007) | 2 lines
Bug #1699: Define _BSD_SOURCE only on OpenBSD.
........
r59612 | raymond.hettinger | 2007-12-29 23:09:34 +0100 (Sat, 29 Dec 2007) | 1 line
Simpler documentation for itertools.tee(). Should be backported.
........
r59613 | raymond.hettinger | 2007-12-29 23:16:24 +0100 (Sat, 29 Dec 2007) | 1 line
Improve docs for itertools.groupby(). The use of xrange(0) to create a unique object is less obvious than object().
........
r59620 | christian.heimes | 2007-12-31 15:47:07 +0100 (Mon, 31 Dec 2007) | 3 lines
Added wininst-9.0.exe executable for VS 2008
Integrated bdist_wininst into PCBuild9 directory
........
r59621 | christian.heimes | 2007-12-31 15:51:18 +0100 (Mon, 31 Dec 2007) | 1 line
Moved PCbuild directory to PC/VS7.1
........
r59622 | christian.heimes | 2007-12-31 15:59:26 +0100 (Mon, 31 Dec 2007) | 1 line
Fix paths for build bot
........
r59623 | christian.heimes | 2007-12-31 16:02:41 +0100 (Mon, 31 Dec 2007) | 1 line
Fix paths for build bot, part 2
........
r59624 | christian.heimes | 2007-12-31 16:18:55 +0100 (Mon, 31 Dec 2007) | 1 line
Renamed PCBuild9 directory to PCBuild
........
2007-12-31 12:14:33 -04:00
|
|
|
literal. A hash character within a string literal is just a hash character.
|
2008-09-13 14:18:21 -03:00
|
|
|
Since comments are to clarify code and are not interpreted by Python, they may
|
|
|
|
be omitted when typing in examples.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
Some examples::
|
|
|
|
|
|
|
|
# this is the first comment
|
|
|
|
SPAM = 1 # and this is the second comment
|
|
|
|
# ... and now a third!
|
|
|
|
STRING = "# This is not a comment."
|
|
|
|
|
|
|
|
|
|
|
|
.. _tut-calculator:
|
|
|
|
|
|
|
|
Using Python as a Calculator
|
|
|
|
============================
|
|
|
|
|
|
|
|
Let's try some simple Python commands. Start the interpreter and wait for the
|
|
|
|
primary prompt, ``>>>``. (It shouldn't take long.)
|
|
|
|
|
|
|
|
|
|
|
|
.. _tut-numbers:
|
|
|
|
|
|
|
|
Numbers
|
|
|
|
-------
|
|
|
|
|
|
|
|
The interpreter acts as a simple calculator: you can type an expression at it
|
|
|
|
and it will write the value. Expression syntax is straightforward: the
|
|
|
|
operators ``+``, ``-``, ``*`` and ``/`` work just like in most other languages
|
|
|
|
(for example, Pascal or C); parentheses can be used for grouping. For example::
|
|
|
|
|
|
|
|
>>> 2+2
|
|
|
|
4
|
|
|
|
>>> # This is a comment
|
|
|
|
... 2+2
|
|
|
|
4
|
|
|
|
>>> 2+2 # and a comment on the same line as code
|
|
|
|
4
|
|
|
|
>>> (50-5*6)/4
|
2007-08-31 00:25:11 -03:00
|
|
|
5.0
|
|
|
|
>>> 8/5 # Fractions aren't lost when dividing integers
|
|
|
|
1.6000000000000001
|
|
|
|
|
|
|
|
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; what we see here is the most
|
|
|
|
informative display but not as easy to read as we would get with::
|
|
|
|
|
|
|
|
>>> print(8/5)
|
|
|
|
1.6
|
|
|
|
|
|
|
|
For clarity in this tutorial we will show the simpler floating point output
|
|
|
|
unless we are specifically discussing output formatting, and explain later
|
|
|
|
why these two ways of displaying floating point data come to be different.
|
|
|
|
See :ref:`tut-fp-issues` for a full discussion.
|
|
|
|
|
|
|
|
To do integer division and get an integer result,
|
|
|
|
discarding any fractional result, there is another operator, ``//``::
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
>>> # Integer division returns the floor:
|
2007-08-31 00:25:11 -03:00
|
|
|
... 7//3
|
2007-08-15 11:28:22 -03:00
|
|
|
2
|
2007-08-31 00:25:11 -03:00
|
|
|
>>> 7//-3
|
2007-08-15 11:28:22 -03:00
|
|
|
-3
|
|
|
|
|
|
|
|
The equal sign (``'='``) is used to assign a value to a variable. Afterwards, no
|
|
|
|
result is displayed before the next interactive prompt::
|
|
|
|
|
|
|
|
>>> width = 20
|
|
|
|
>>> height = 5*9
|
|
|
|
>>> width * height
|
|
|
|
900
|
|
|
|
|
|
|
|
A value can be assigned to several variables simultaneously::
|
|
|
|
|
|
|
|
>>> x = y = z = 0 # Zero x, y and z
|
|
|
|
>>> x
|
|
|
|
0
|
|
|
|
>>> y
|
|
|
|
0
|
|
|
|
>>> z
|
|
|
|
0
|
|
|
|
|
2008-09-13 14:18:21 -03:00
|
|
|
Variables must be "defined" (assigned a value) before they can be used, or an
|
|
|
|
error will occur::
|
|
|
|
|
|
|
|
>>> # try to access an undefined variable
|
|
|
|
... n
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
NameError: name 'n' is not defined
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
There is full support for floating point; operators with mixed type operands
|
|
|
|
convert the integer operand to floating point::
|
|
|
|
|
|
|
|
>>> 3 * 3.75 / 1.5
|
|
|
|
7.5
|
|
|
|
>>> 7.0 / 2
|
|
|
|
3.5
|
|
|
|
|
|
|
|
Complex numbers are also supported; imaginary numbers are written with a suffix
|
|
|
|
of ``j`` or ``J``. Complex numbers with a nonzero real component are written as
|
|
|
|
``(real+imagj)``, or can be created with the ``complex(real, imag)`` function.
|
|
|
|
::
|
|
|
|
|
|
|
|
>>> 1j * 1J
|
|
|
|
(-1+0j)
|
2007-09-03 04:10:24 -03:00
|
|
|
>>> 1j * complex(0, 1)
|
2007-08-15 11:28:22 -03:00
|
|
|
(-1+0j)
|
|
|
|
>>> 3+1j*3
|
|
|
|
(3+3j)
|
|
|
|
>>> (3+1j)*3
|
|
|
|
(9+3j)
|
|
|
|
>>> (1+2j)/(1+1j)
|
|
|
|
(1.5+0.5j)
|
|
|
|
|
|
|
|
Complex numbers are always represented as two floating point numbers, the real
|
|
|
|
and imaginary part. To extract these parts from a complex number *z*, use
|
|
|
|
``z.real`` and ``z.imag``. ::
|
|
|
|
|
|
|
|
>>> a=1.5+0.5j
|
|
|
|
>>> a.real
|
|
|
|
1.5
|
|
|
|
>>> a.imag
|
|
|
|
0.5
|
|
|
|
|
|
|
|
The conversion functions to floating point and integer (:func:`float`,
|
2007-09-28 10:13:35 -03:00
|
|
|
: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::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
>>> a=3.0+4.0j
|
|
|
|
>>> float(a)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in ?
|
|
|
|
TypeError: can't convert complex to float; use abs(z)
|
|
|
|
>>> a.real
|
|
|
|
3.0
|
|
|
|
>>> a.imag
|
|
|
|
4.0
|
|
|
|
>>> abs(a) # sqrt(a.real**2 + a.imag**2)
|
|
|
|
5.0
|
|
|
|
>>>
|
|
|
|
|
|
|
|
In interactive mode, the last printed expression is assigned to the variable
|
|
|
|
``_``. This means that when you are using Python as a desk calculator, it is
|
|
|
|
somewhat easier to continue calculations, for example::
|
|
|
|
|
|
|
|
>>> tax = 12.5 / 100
|
|
|
|
>>> price = 100.50
|
|
|
|
>>> price * tax
|
|
|
|
12.5625
|
|
|
|
>>> price + _
|
|
|
|
113.0625
|
|
|
|
>>> round(_, 2)
|
|
|
|
113.06
|
|
|
|
>>>
|
|
|
|
|
|
|
|
This variable should be treated as read-only by the user. Don't explicitly
|
|
|
|
assign a value to it --- you would create an independent local variable with the
|
|
|
|
same name masking the built-in variable with its magic behavior.
|
|
|
|
|
|
|
|
|
|
|
|
.. _tut-strings:
|
|
|
|
|
|
|
|
Strings
|
|
|
|
-------
|
|
|
|
|
|
|
|
Besides numbers, Python can also manipulate strings, which can be expressed in
|
|
|
|
several ways. They can be enclosed in single quotes or double quotes::
|
|
|
|
|
|
|
|
>>> 'spam eggs'
|
|
|
|
'spam eggs'
|
|
|
|
>>> 'doesn\'t'
|
|
|
|
"doesn't"
|
|
|
|
>>> "doesn't"
|
|
|
|
"doesn't"
|
|
|
|
>>> '"Yes," he said.'
|
|
|
|
'"Yes," he said.'
|
|
|
|
>>> "\"Yes,\" he said."
|
|
|
|
'"Yes," he said.'
|
|
|
|
>>> '"Isn\'t," she said.'
|
|
|
|
'"Isn\'t," she said.'
|
|
|
|
|
2007-08-31 00:25:11 -03:00
|
|
|
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. Once again, the :func:`print` function
|
|
|
|
produces the more readable output.
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
String literals can span multiple lines in several ways. Continuation lines can
|
|
|
|
be used, with a backslash as the last character on the line indicating that the
|
|
|
|
next line is a logical continuation of the line::
|
|
|
|
|
|
|
|
hello = "This is a rather long string containing\n\
|
|
|
|
several lines of text just as you would do in C.\n\
|
|
|
|
Note that whitespace at the beginning of the line is\
|
|
|
|
significant."
|
|
|
|
|
2007-08-31 00:25:11 -03:00
|
|
|
print(hello)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
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
|
|
|
|
the following::
|
|
|
|
|
|
|
|
This is a rather long string containing
|
|
|
|
several lines of text just as you would do in C.
|
|
|
|
Note that whitespace at the beginning of the line is significant.
|
|
|
|
|
|
|
|
If we make the string literal a "raw" string, however, the ``\n`` sequences are
|
|
|
|
not converted to newlines, but the backslash at the end of the line, and the
|
|
|
|
newline character 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."
|
|
|
|
|
2007-08-31 00:25:11 -03:00
|
|
|
print(hello)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
would print::
|
|
|
|
|
|
|
|
This is a rather long string containing\n\
|
|
|
|
several lines of text much as you would do in C.
|
|
|
|
|
|
|
|
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. ::
|
|
|
|
|
2007-08-31 00:25:11 -03:00
|
|
|
print("""
|
2007-08-15 11:28:22 -03:00
|
|
|
Usage: thingy [OPTIONS]
|
|
|
|
-h Display this usage message
|
|
|
|
-H hostname Hostname to connect to
|
2007-08-31 00:25:11 -03:00
|
|
|
""")
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
produces the following output::
|
|
|
|
|
|
|
|
Usage: thingy [OPTIONS]
|
|
|
|
-h Display this usage message
|
|
|
|
-H hostname Hostname to connect to
|
|
|
|
|
|
|
|
|
|
|
|
Strings can be concatenated (glued together) with the ``+`` operator, and
|
|
|
|
repeated with ``*``::
|
|
|
|
|
|
|
|
>>> word = 'Help' + 'A'
|
|
|
|
>>> word
|
|
|
|
'HelpA'
|
|
|
|
>>> '<' + word*5 + '>'
|
|
|
|
'<HelpAHelpAHelpAHelpAHelpA>'
|
|
|
|
|
|
|
|
Two string literals next to each other are automatically concatenated; the first
|
|
|
|
line above could also have been written ``word = 'Help' 'A'``; this only works
|
|
|
|
with two literals, not with arbitrary string expressions::
|
|
|
|
|
|
|
|
>>> 'str' 'ing' # <- This is ok
|
|
|
|
'string'
|
|
|
|
>>> 'str'.strip() + 'ing' # <- This is ok
|
|
|
|
'string'
|
|
|
|
>>> 'str'.strip() 'ing' # <- This is invalid
|
|
|
|
File "<stdin>", line 1, in ?
|
|
|
|
'str'.strip() 'ing'
|
|
|
|
^
|
|
|
|
SyntaxError: invalid syntax
|
|
|
|
|
|
|
|
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
|
2007-09-03 04:10:24 -03:00
|
|
|
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.
|
|
|
|
::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
>>> word[4]
|
|
|
|
'A'
|
|
|
|
>>> word[0:2]
|
|
|
|
'He'
|
|
|
|
>>> word[2:4]
|
|
|
|
'lp'
|
|
|
|
|
|
|
|
Slice indices have useful defaults; an omitted first index defaults to zero, an
|
|
|
|
omitted second index defaults to the size of the string being sliced. ::
|
|
|
|
|
|
|
|
>>> word[:2] # The first two characters
|
|
|
|
'He'
|
|
|
|
>>> word[2:] # Everything except the first two characters
|
|
|
|
'lpA'
|
|
|
|
|
2008-09-13 14:18:21 -03:00
|
|
|
Unlike a C string, Python strings cannot be changed. Assigning to an indexed
|
2007-08-15 11:28:22 -03:00
|
|
|
position in the string results in an error::
|
|
|
|
|
|
|
|
>>> word[0] = 'x'
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in ?
|
2007-08-31 00:25:11 -03:00
|
|
|
TypeError: 'str' object doesn't support item assignment
|
2007-08-15 11:28:22 -03:00
|
|
|
>>> word[:1] = 'Splat'
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in ?
|
2007-08-31 00:25:11 -03:00
|
|
|
TypeError: 'str' object doesn't support slice assignment
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
However, creating a new string with the combined content is easy and efficient::
|
|
|
|
|
|
|
|
>>> 'x' + word[1:]
|
|
|
|
'xelpA'
|
|
|
|
>>> 'Splat' + word[4]
|
|
|
|
'SplatA'
|
|
|
|
|
|
|
|
Here's a useful invariant of slice operations: ``s[:i] + s[i:]`` equals ``s``.
|
|
|
|
::
|
|
|
|
|
|
|
|
>>> word[:2] + word[2:]
|
|
|
|
'HelpA'
|
|
|
|
>>> word[:3] + word[3:]
|
|
|
|
'HelpA'
|
|
|
|
|
|
|
|
Degenerate slice indices are handled gracefully: an index that is too large is
|
|
|
|
replaced by the string size, an upper bound smaller than the lower bound returns
|
|
|
|
an empty string. ::
|
|
|
|
|
|
|
|
>>> word[1:100]
|
|
|
|
'elpA'
|
|
|
|
>>> word[10:]
|
|
|
|
''
|
|
|
|
>>> word[2:1]
|
|
|
|
''
|
|
|
|
|
|
|
|
Indices may be negative numbers, to start counting from the right. For example::
|
|
|
|
|
|
|
|
>>> word[-1] # The last character
|
|
|
|
'A'
|
|
|
|
>>> word[-2] # The last-but-one character
|
|
|
|
'p'
|
|
|
|
>>> word[-2:] # The last two characters
|
|
|
|
'pA'
|
|
|
|
>>> word[:-2] # Everything except the last two characters
|
|
|
|
'Hel'
|
|
|
|
|
|
|
|
But note that -0 is really the same as 0, so it does not count from the right!
|
|
|
|
::
|
|
|
|
|
|
|
|
>>> word[-0] # (since -0 equals 0)
|
|
|
|
'H'
|
|
|
|
|
|
|
|
Out-of-range negative slice indices are truncated, but don't try this for
|
|
|
|
single-element (non-slice) indices::
|
|
|
|
|
|
|
|
>>> word[-100:]
|
|
|
|
'HelpA'
|
|
|
|
>>> word[-10] # error
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in ?
|
|
|
|
IndexError: string index out of range
|
|
|
|
|
|
|
|
One way to remember how slices work is to think of the indices as pointing
|
|
|
|
*between* characters, with the left edge of the first character numbered 0.
|
|
|
|
Then the right edge of the last character of a string of *n* characters has
|
|
|
|
index *n*, for example::
|
|
|
|
|
|
|
|
+---+---+---+---+---+
|
|
|
|
| H | e | l | p | A |
|
|
|
|
+---+---+---+---+---+
|
|
|
|
0 1 2 3 4 5
|
|
|
|
-5 -4 -3 -2 -1
|
|
|
|
|
|
|
|
The first row of numbers gives the position of the indices 0...5 in the string;
|
|
|
|
the second row gives the corresponding negative indices. The slice from *i* to
|
|
|
|
*j* consists of all characters between the edges labeled *i* and *j*,
|
|
|
|
respectively.
|
|
|
|
|
|
|
|
For non-negative indices, the length of a slice is the difference of the
|
|
|
|
indices, if both are within bounds. For example, the length of ``word[1:3]`` is
|
|
|
|
2.
|
|
|
|
|
|
|
|
The built-in function :func:`len` returns the length of a string::
|
|
|
|
|
|
|
|
>>> s = 'supercalifragilisticexpialidocious'
|
|
|
|
>>> len(s)
|
|
|
|
34
|
|
|
|
|
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
:ref:`typesseq`
|
2007-08-31 00:25:11 -03:00
|
|
|
Strings are examples of *sequence types*, and support the common
|
|
|
|
operations supported by such types.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
:ref:`string-methods`
|
2007-08-31 00:25:11 -03:00
|
|
|
Strings support a large number of methods for
|
2007-08-15 11:28:22 -03:00
|
|
|
basic transformations and searching.
|
|
|
|
|
|
|
|
:ref:`string-formatting`
|
2008-05-25 22:03:56 -03:00
|
|
|
Information about string formatting with :meth:`str.format` is described
|
|
|
|
here.
|
|
|
|
|
|
|
|
: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.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. _tut-unicodestrings:
|
|
|
|
|
2007-08-31 00:25:11 -03:00
|
|
|
About Unicode
|
|
|
|
-------------
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. sectionauthor:: Marc-Andre Lemburg <mal@lemburg.com>
|
|
|
|
|
|
|
|
|
2008-09-13 14:18:21 -03:00
|
|
|
Starting with Python 3.0 all strings support Unicode (see
|
|
|
|
http://www.unicode.org/).
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
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
|
|
|
|
possible ordinals for script characters. Texts were typically bound to a code
|
|
|
|
page which mapped the ordinals to script characters. This lead to very much
|
|
|
|
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.
|
|
|
|
|
2007-08-31 00:25:11 -03:00
|
|
|
If you want to include special characters in a string,
|
2007-08-15 11:28:22 -03:00
|
|
|
you can do so by using the Python *Unicode-Escape* encoding. The following
|
|
|
|
example shows how::
|
|
|
|
|
2007-08-31 00:25:11 -03:00
|
|
|
>>> 'Hello\u0020World !'
|
|
|
|
'Hello World !'
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
The escape sequence ``\u0020`` indicates to insert the Unicode character with
|
|
|
|
the ordinal value 0x0020 (the space character) at the given position.
|
|
|
|
|
|
|
|
Other characters are interpreted by using their respective ordinal values
|
|
|
|
directly as Unicode ordinals. If you have literal strings in the standard
|
|
|
|
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.
|
|
|
|
|
|
|
|
Apart from these standard encodings, Python provides a whole set of other ways
|
|
|
|
of creating Unicode strings on the basis of a known encoding.
|
|
|
|
|
2007-08-31 00:25:11 -03:00
|
|
|
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
|
2007-08-15 11:28:22 -03:00
|
|
|
name of the encoding. Lowercase names for encodings are preferred. ::
|
|
|
|
|
2007-08-31 03:46:05 -03:00
|
|
|
>>> "Äpfel".encode('utf-8')
|
|
|
|
b'\xc3\x84pfel'
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. _tut-lists:
|
|
|
|
|
|
|
|
Lists
|
|
|
|
-----
|
|
|
|
|
|
|
|
Python knows a number of *compound* data types, used to group together other
|
|
|
|
values. The most versatile is the *list*, which can be written as a list of
|
|
|
|
comma-separated values (items) between square brackets. List items need not all
|
|
|
|
have the same type. ::
|
|
|
|
|
|
|
|
>>> a = ['spam', 'eggs', 100, 1234]
|
|
|
|
>>> a
|
|
|
|
['spam', 'eggs', 100, 1234]
|
|
|
|
|
|
|
|
Like string indices, list indices start at 0, and lists can be sliced,
|
|
|
|
concatenated and so on::
|
|
|
|
|
|
|
|
>>> a[0]
|
|
|
|
'spam'
|
|
|
|
>>> a[3]
|
|
|
|
1234
|
|
|
|
>>> a[-2]
|
|
|
|
100
|
|
|
|
>>> a[1:-1]
|
|
|
|
['eggs', 100]
|
|
|
|
>>> a[:2] + ['bacon', 2*2]
|
|
|
|
['spam', 'eggs', 'bacon', 4]
|
|
|
|
>>> 3*a[:3] + ['Boo!']
|
|
|
|
['spam', 'eggs', 100, 'spam', 'eggs', 100, 'spam', 'eggs', 100, 'Boo!']
|
|
|
|
|
|
|
|
Unlike strings, which are *immutable*, it is possible to change individual
|
|
|
|
elements of a list::
|
|
|
|
|
|
|
|
>>> a
|
|
|
|
['spam', 'eggs', 100, 1234]
|
|
|
|
>>> a[2] = a[2] + 23
|
|
|
|
>>> a
|
|
|
|
['spam', 'eggs', 123, 1234]
|
|
|
|
|
|
|
|
Assignment to slices is also possible, and this can even change the size of the
|
|
|
|
list or clear it entirely::
|
|
|
|
|
|
|
|
>>> # Replace some items:
|
|
|
|
... a[0:2] = [1, 12]
|
|
|
|
>>> a
|
|
|
|
[1, 12, 123, 1234]
|
|
|
|
>>> # Remove some:
|
|
|
|
... a[0:2] = []
|
|
|
|
>>> a
|
|
|
|
[123, 1234]
|
|
|
|
>>> # Insert some:
|
|
|
|
... a[1:1] = ['bletch', 'xyzzy']
|
|
|
|
>>> a
|
|
|
|
[123, 'bletch', 'xyzzy', 1234]
|
|
|
|
>>> # Insert (a copy of) itself at the beginning
|
|
|
|
>>> a[:0] = a
|
|
|
|
>>> a
|
|
|
|
[123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
|
|
|
|
>>> # Clear the list: replace all items with an empty list
|
|
|
|
>>> a[:] = []
|
|
|
|
>>> a
|
|
|
|
[]
|
|
|
|
|
|
|
|
The built-in function :func:`len` also applies to lists::
|
|
|
|
|
Merged revisions 58886-58929 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r58892 | guido.van.rossum | 2007-11-06 15:32:56 -0800 (Tue, 06 Nov 2007) | 2 lines
Add missing "return NULL" in overflow check in PyObject_Repr().
........
r58893 | raymond.hettinger | 2007-11-06 17:13:09 -0800 (Tue, 06 Nov 2007) | 1 line
Fix marshal's incorrect handling of subclasses of builtin types (backport candidate).
........
r58895 | raymond.hettinger | 2007-11-06 18:26:17 -0800 (Tue, 06 Nov 2007) | 1 line
Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
........
r58896 | raymond.hettinger | 2007-11-06 18:45:46 -0800 (Tue, 06 Nov 2007) | 1 line
Add build option for faster loop execution.
........
r58900 | nick.coghlan | 2007-11-07 03:57:51 -0800 (Wed, 07 Nov 2007) | 1 line
Add missing NEWS entry
........
r58905 | christian.heimes | 2007-11-07 09:50:54 -0800 (Wed, 07 Nov 2007) | 1 line
Backported fix for bug #1392 from py3k branch r58903.
........
r58906 | christian.heimes | 2007-11-07 10:30:22 -0800 (Wed, 07 Nov 2007) | 1 line
Backport of Guido's review of my patch.
........
r58908 | raymond.hettinger | 2007-11-07 18:52:43 -0800 (Wed, 07 Nov 2007) | 1 line
Add set.isdisjoint()
........
r58915 | raymond.hettinger | 2007-11-08 10:47:51 -0800 (Thu, 08 Nov 2007) | 1 line
Reposition the decref (spotted by eagle-eye norwitz).
........
r58920 | georg.brandl | 2007-11-09 04:31:43 -0800 (Fri, 09 Nov 2007) | 2 lines
Fix seealso link to sets docs. Do not merge to Py3k.
........
r58921 | georg.brandl | 2007-11-09 05:08:48 -0800 (Fri, 09 Nov 2007) | 2 lines
Fix misleading example.
........
r58923 | georg.brandl | 2007-11-09 09:33:23 -0800 (Fri, 09 Nov 2007) | 3 lines
Correct a comment about testing methods - nowadays most
tests don't run directly on import.
........
r58924 | martin.v.loewis | 2007-11-09 14:56:30 -0800 (Fri, 09 Nov 2007) | 2 lines
Add Amaury Forgeot d'Arc.
........
r58925 | raymond.hettinger | 2007-11-09 15:14:44 -0800 (Fri, 09 Nov 2007) | 1 line
Optimize common case for dict.fromkeys().
........
r58927 | raymond.hettinger | 2007-11-09 17:54:03 -0800 (Fri, 09 Nov 2007) | 1 line
Use a freelist to speed-up block allocation and deallocation in collections.deque().
........
r58929 | guido.van.rossum | 2007-11-10 14:12:24 -0800 (Sat, 10 Nov 2007) | 3 lines
Issue 1416. Add getter, setter, deleter methods to properties that can be
used as decorators to create fully-populated properties.
........
2007-11-10 19:39:45 -04:00
|
|
|
>>> a = ['a', 'b', 'c', 'd']
|
2007-08-15 11:28:22 -03:00
|
|
|
>>> len(a)
|
Merged revisions 58886-58929 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r58892 | guido.van.rossum | 2007-11-06 15:32:56 -0800 (Tue, 06 Nov 2007) | 2 lines
Add missing "return NULL" in overflow check in PyObject_Repr().
........
r58893 | raymond.hettinger | 2007-11-06 17:13:09 -0800 (Tue, 06 Nov 2007) | 1 line
Fix marshal's incorrect handling of subclasses of builtin types (backport candidate).
........
r58895 | raymond.hettinger | 2007-11-06 18:26:17 -0800 (Tue, 06 Nov 2007) | 1 line
Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
........
r58896 | raymond.hettinger | 2007-11-06 18:45:46 -0800 (Tue, 06 Nov 2007) | 1 line
Add build option for faster loop execution.
........
r58900 | nick.coghlan | 2007-11-07 03:57:51 -0800 (Wed, 07 Nov 2007) | 1 line
Add missing NEWS entry
........
r58905 | christian.heimes | 2007-11-07 09:50:54 -0800 (Wed, 07 Nov 2007) | 1 line
Backported fix for bug #1392 from py3k branch r58903.
........
r58906 | christian.heimes | 2007-11-07 10:30:22 -0800 (Wed, 07 Nov 2007) | 1 line
Backport of Guido's review of my patch.
........
r58908 | raymond.hettinger | 2007-11-07 18:52:43 -0800 (Wed, 07 Nov 2007) | 1 line
Add set.isdisjoint()
........
r58915 | raymond.hettinger | 2007-11-08 10:47:51 -0800 (Thu, 08 Nov 2007) | 1 line
Reposition the decref (spotted by eagle-eye norwitz).
........
r58920 | georg.brandl | 2007-11-09 04:31:43 -0800 (Fri, 09 Nov 2007) | 2 lines
Fix seealso link to sets docs. Do not merge to Py3k.
........
r58921 | georg.brandl | 2007-11-09 05:08:48 -0800 (Fri, 09 Nov 2007) | 2 lines
Fix misleading example.
........
r58923 | georg.brandl | 2007-11-09 09:33:23 -0800 (Fri, 09 Nov 2007) | 3 lines
Correct a comment about testing methods - nowadays most
tests don't run directly on import.
........
r58924 | martin.v.loewis | 2007-11-09 14:56:30 -0800 (Fri, 09 Nov 2007) | 2 lines
Add Amaury Forgeot d'Arc.
........
r58925 | raymond.hettinger | 2007-11-09 15:14:44 -0800 (Fri, 09 Nov 2007) | 1 line
Optimize common case for dict.fromkeys().
........
r58927 | raymond.hettinger | 2007-11-09 17:54:03 -0800 (Fri, 09 Nov 2007) | 1 line
Use a freelist to speed-up block allocation and deallocation in collections.deque().
........
r58929 | guido.van.rossum | 2007-11-10 14:12:24 -0800 (Sat, 10 Nov 2007) | 3 lines
Issue 1416. Add getter, setter, deleter methods to properties that can be
used as decorators to create fully-populated properties.
........
2007-11-10 19:39:45 -04:00
|
|
|
4
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
It is possible to nest lists (create lists containing other lists), for
|
|
|
|
example::
|
|
|
|
|
|
|
|
>>> q = [2, 3]
|
|
|
|
>>> p = [1, q, 4]
|
|
|
|
>>> len(p)
|
|
|
|
3
|
|
|
|
>>> p[1]
|
|
|
|
[2, 3]
|
|
|
|
>>> p[1][0]
|
|
|
|
2
|
2007-08-31 00:25:11 -03:00
|
|
|
|
|
|
|
You can add something to the end of the list::
|
|
|
|
|
2007-09-03 04:10:24 -03:00
|
|
|
>>> p[1].append('xtra')
|
2007-08-15 11:28:22 -03:00
|
|
|
>>> p
|
|
|
|
[1, [2, 3, 'xtra'], 4]
|
|
|
|
>>> q
|
|
|
|
[2, 3, 'xtra']
|
|
|
|
|
|
|
|
Note that in the last example, ``p[1]`` and ``q`` really refer to the same
|
|
|
|
object! We'll come back to *object semantics* later.
|
|
|
|
|
|
|
|
|
|
|
|
.. _tut-firststeps:
|
|
|
|
|
|
|
|
First Steps Towards Programming
|
|
|
|
===============================
|
|
|
|
|
|
|
|
Of course, we can use Python for more complicated tasks than adding two and two
|
|
|
|
together. For instance, we can write an initial sub-sequence of the *Fibonacci*
|
|
|
|
series as follows::
|
|
|
|
|
|
|
|
>>> # Fibonacci series:
|
|
|
|
... # the sum of two elements defines the next
|
|
|
|
... a, b = 0, 1
|
|
|
|
>>> while b < 10:
|
2008-01-07 13:32:13 -04:00
|
|
|
... print(b)
|
|
|
|
... a, b = b, a+b
|
2007-08-15 11:28:22 -03:00
|
|
|
...
|
|
|
|
1
|
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
5
|
|
|
|
8
|
|
|
|
|
|
|
|
This example introduces several new features.
|
|
|
|
|
|
|
|
* The first line contains a *multiple assignment*: the variables ``a`` and ``b``
|
|
|
|
simultaneously get the new values 0 and 1. On the last line this is used again,
|
|
|
|
demonstrating that the expressions on the right-hand side are all evaluated
|
|
|
|
first before any of the assignments take place. The right-hand side expressions
|
|
|
|
are evaluated from the left to the right.
|
|
|
|
|
|
|
|
* The :keyword:`while` loop executes as long as the condition (here: ``b < 10``)
|
|
|
|
remains true. In Python, like in C, any non-zero integer value is true; zero is
|
|
|
|
false. The condition may also be a string or list value, in fact any sequence;
|
|
|
|
anything with a non-zero length is true, empty sequences are false. The test
|
|
|
|
used in the example is a simple comparison. The standard comparison operators
|
|
|
|
are written the same as in C: ``<`` (less than), ``>`` (greater than), ``==``
|
|
|
|
(equal to), ``<=`` (less than or equal to), ``>=`` (greater than or equal to)
|
|
|
|
and ``!=`` (not equal to).
|
|
|
|
|
|
|
|
* The *body* of the loop is *indented*: indentation is Python's way of grouping
|
|
|
|
statements. Python does not (yet!) provide an intelligent input line editing
|
|
|
|
facility, so you have to type a tab or space(s) for each indented line. In
|
|
|
|
practice you will prepare more complicated input for Python with a text editor;
|
|
|
|
most text editors have an auto-indent facility. When a compound statement is
|
|
|
|
entered interactively, it must be followed by a blank line to indicate
|
|
|
|
completion (since the parser cannot guess when you have typed the last line).
|
|
|
|
Note that each line within a basic block must be indented by the same amount.
|
|
|
|
|
2007-08-31 00:25:11 -03:00
|
|
|
* The :func:`print` function writes the value of the expression(s) it is
|
2007-08-15 11:28:22 -03:00
|
|
|
given. It differs from just writing the expression you want to write (as we did
|
2007-08-31 00:25:11 -03:00
|
|
|
earlier in the calculator examples) in the way it handles multiple
|
|
|
|
expressions, floating point quantities,
|
2007-08-15 11:28:22 -03:00
|
|
|
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
|
2007-08-31 00:25:11 -03:00
|
|
|
>>> print('The value of i is', i)
|
2007-08-15 11:28:22 -03:00
|
|
|
The value of i is 65536
|
|
|
|
|
2008-08-05 06:04:16 -03:00
|
|
|
The keyword *end* can be used to avoid the newline after the output, or end
|
|
|
|
the output with a different string::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
>>> a, b = 0, 1
|
|
|
|
>>> while b < 1000:
|
2008-08-05 06:04:16 -03:00
|
|
|
... print(b, end=' ')
|
2007-08-15 11:28:22 -03:00
|
|
|
... a, b = b, a+b
|
|
|
|
...
|
|
|
|
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
|