个人工具

“Python 官方简明教程 3”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
— 3.1.2. 字符串
 
(未显示7个用户的63个中间版本)
第1行: 第1行:
=3. An Informal Introduction to Python¶=
+
=3.Python概述=
 +
'''感谢Lilydjwg和remeber翻译并校对本页,可以到[http://docspy3zh.readthedocs.org/en/latest/tutorial/index.html readthedocs.org]查看更新更专业的翻译版本'''
  
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, #, 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 literal. A hash character within a string literal is just a hash character. Since comments are to clarify code and are not interpreted by Python, they may be omitted when typing in examples.
+
本手册中的部分例子,包括注释都是在交互提示符后输入的。Python中的注释由井号 # (the hash character)开始,直到物理行的末尾。注释可以从行首开始,或者跟在空白或代码之后,但不能位于字符串内。字符串里的井号只是井号。由于注释是用来解释代码的,不会被Python解释,所以在实践操作中您不必输入例子中的注释。
  
Some examples:
+
例如:
  
# this is the first comment
+
# 这是第一行注释
SPAM = 1                # and this is the second comment
+
SPAM = 1                # 这是第二行注释
                        # ... and now a third!
+
                          # ……第三行了!
STRING = "# This is not a comment."
+
STRING = "# 这不是注释。"
  
==3.1. Using Python as a Calculator¶==
+
==3.1. 把Python作为一个计算器==
  
Let’s try some simple Python commands. Start the interpreter and wait for the primary prompt, >>>. (It shouldn’t take long.)
+
让我们尝试一些简单的Python命令。打开解释器等待主提示符 >>> 的出现(不会花很久)。
===3.1.1. 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:
+
===— 3.1.1. 数字===
  
>>> 2+2
+
解释器可以作为一个简单的计算器:您可以在解释器里输入一个表达式,它将输出表达式的值。表达式的语法很直白: +, -, * / 和在许多其它语言(如Pascal或C)里一样;括号可以用来为运算分组。例如:
4
+
>>> # This is a comment
+
... 2+2
+
4
+
>>> 2+2  # and a comment on the same line as code
+
4
+
>>> (50-5*6)/4
+
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 Floating Point Arithmetic: Issues and Limitations for a full discussion of some of the subtleties of floating point numbers and their representations.
+
>>> 2+2
 +
4
 +
>>> # 这是一行注释
 +
... 2+2
 +
4
 +
>>> 2+2  # 这是和代码同在一行的注释
 +
4
 +
>>> (50-5*6)/4
 +
5.0
 +
>>> 8/5 # 整数除法不会丢失分数部分
 +
1.6
  
To do integer division and get an integer result, discarding any fractional result, there is another operator, //:
+
注意:在不同的机器上浮点运算的结果可能会不一样。之后我们会介绍有关控制浮点运算输出结果的内容。关于浮点数及其表示方式的微妙之处的全面细致的讨论,请参考《浮点运算:问题和限制》。
  
>>> # Integer division returns the floor:
+
在整数除法中,如果只想得到整数的结果,丢弃可能的分数部分,可以使用运算符 //
... 7//3
+
2
+
>>> 7//-3
+
-3
+
  
The equal sign ('=') is used to assign a value to a variable. Afterwards, no result is displayed before the next interactive prompt:
+
>>> # 整数除法返回向下取整后的结果:
 +
... 7//3
 +
2
 +
>>> 7//-3
 +
-3
  
>>> width = 20
+
等号(‘=’)用于给变量赋值。赋值之后,除了下一个提示符,解释器不会显示任何结果。
>>> height = 5*9
+
>>> width * height
+
900
+
  
A value can be assigned to several variables simultaneously:
+
>>> width = 20
 +
>>> height = 5*9
 +
>>> width * height
 +
900
  
>>> x = y = z = 0  # Zero x, y and z
+
一个值可以同时赋予多个变量:
>>> x
+
0
+
>>> y
+
0
+
>>> z
+
0
+
  
Variables must be “defined” (assigned a value) before they can be used, or an error will occur:
+
>>> x = y = z = 0  # 清零 x, y 和 z
 +
>>> x
 +
0
 +
>>> y
 +
0
 +
>>> z
 +
0
  
>>> # try to access an undefined variable
+
变量在使用前必须先“定义”(即赋予变量一个值),否则会出现错误:
... n
+
Traceback (most recent call last):
+
  File "<stdin>", line 1, in <module>
+
NameError: name 'n' is not defined
+
  
There is full support for floating point; operators with mixed type operands convert the integer operand to floating point:
+
>>> # 尝试访问一个未定义的变量
 +
... n
 +
Traceback (most recent call last):
 +
  File "<stdin>", line 1, in <module>
 +
NameError: name 'n' is not defined
  
>>> 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.
+
>>> 3 * 3.75 / 1.5
 +
7.5
 +
>>> 7.0 / 2
 +
3.5
  
>>> 1j * 1J
+
复数也支持;虚数带有 j 或 J 后缀。带有非零实部的复数记为 (实部+虚部j),或者使用 complex(实部, 虚部) 函数来创建。
(-1+0j)
+
>>> 1j * complex(0, 1)
+
(-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.
+
>>> 1j * 1J
 +
(-1+0j)
 +
>>> 1j * complex(0, 1)
 +
(-1+0j)
 +
>>> 3+1j*3
 +
(3+3j)
 +
>>> (3+1j)*3
 +
(9+3j)
 +
>>> (1+2j)/(1+1j)
 +
(1.5+0.5j)
  
>>> a=1.5+0.5j
+
复数总是由两个浮点数来表示,实部和虚部。若要从复数z中提取其中一部分,可使用 z.real 和 z.imag。
>>> a.real
+
1.5
+
>>> a.imag
+
0.5
+
  
The conversion functions to floating point and integer (float(), 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=1.5+0.5j
 +
>>> a.real
 +
1.5
 +
>>> a.imag
 +
0.5
  
>>> a=3.0+4.0j
+
浮点数和整数之间的转换函数(float(), int())不能用于复数——尚没办法将复数转换为实数。但可以使用 abs(z) 获得复数的模(结果是浮点数),或者使用 z.real 获取其实部:
>>> 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:
+
>>> 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
  
>>> tax = 12.5 / 100
+
在交互模式中,最后被输出的表达式结果被赋值给变量 _ 。这能使您在把Python作为一个桌面计算器使用时使后续计算更方便,例如:
>>> 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.
+
>>> tax = 12.5 / 100
===3.1.2. Strings¶===
+
>>> price = 100.50
 +
>>> price * tax
 +
12.5625
 +
>>> price + _
 +
113.0625
 +
>>> round(_, 2)
 +
113.06
  
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'
+
===— 3.1.2. 字符串===
'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.'
+
  
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 print() function produces a more readable output for such input strings.
+
除了数字,Python也能操作字符串。字符串有几种表达方式,可以使用单引号或双引号括起来:
  
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:
+
>>> '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.'
  
hello = "This is a rather long string containing\n\
+
解释器以输入时相同的方式输出字符串操作的结果,即使用引号括起来,并且使用反斜杠转义引号和其它特殊字符来准确地表示。如果字符串包含有单引号但不含双引号,则字符串会用双引号括起来,否则用单引号括起来。对于这样的输入字符串,print() 函数会产生更易读的输出。
several lines of text just as you would do in C.\n\
+
    Note that whitespace at the beginning of the line is\
+
significant."
+
  
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 the following:
+
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."
 +
 +
print(hello)
  
This is a rather long string containing
+
注意,其中的换行符仍然要使用 \n 表示——反斜杠后的换行符被丢弃了。以上例子将如下输出:
several lines of text just as you would do in C.
+
    Note that whitespace at the beginning of the line is significant.
+
  
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. So the following uses one escape to avoid an unwanted initial blank line.
+
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.
  
print("""\
+
或者,字符串可以被 """ (三个双引号)或者<nowiki> ''' </nowiki>(三个单引号)括起来。使用三引号时,换行符不需要转义,它们会包含在字符串中。以下的例子使用了一个转义符,避免在最开始产生一个不需要的空行。
Usage: thingy [OPTIONS]
+
    -h                        Display this usage message
+
    -H hostname              Hostname to connect to
+
""")
+
  
produces the following output:
+
print("""\
 +
Usage: thingy [OPTIONS]
 +
      -h                        Display this usage message
 +
      -H hostname              Hostname to connect to
 +
""")
  
Usage: thingy [OPTIONS]
+
其输出如下:
    -h                        Display this usage message
+
    -H hostname              Hostname to connect to
+
  
If we make the string literal a “raw” string, \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:
+
Usage: thingy [OPTIONS]
 +
      -h                        Display this usage message
 +
      -H hostname              Hostname to connect to
  
hello = r"This is a rather long string containing\n\
+
如果我们使用“原始 (raw)”字符串,那么 \n 不会被转换成换行,行末的的反斜杠,以及源码中的换行符,都将作为数据包含在字符串内。例如:
several lines of text much as you would do in C."
+
  
print(hello)
+
hello = r"This is a rather long string containing\n\
 +
several lines of text much as you would do in C."
 +
 +
print(hello)
  
would print:
+
将会输出:
  
This is a rather long string containing\n\
+
This is a rather long string containing\n\
several lines of text much as you would do in C.
+
several lines of text much as you would do in C.
  
Strings can be concatenated (glued together) with the + operator, and repeated with *:
+
字符串可以使用 + 运算符串连接在一起,或者用 * 运算符重复:
  
>>> word = 'Help' + 'A'
+
>>> word = 'Help' + 'A'
>>> word
+
>>> word
'HelpA'
+
'HelpA'
>>> '<' + word*5 + '>'
+
>>> '<' + word*5 + '>'
'<HelpAHelpAHelpAHelpAHelpA>'
+
'<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:
+
两个紧邻的字面字符串将自动被串连;上例的第一行也可以写成 word = 'Help' 'A' ;这样的操作只在两个字面值间有效,不能随意用于字符串表达式中:
  
>>> 'str' 'ing'                  #  <-  This is ok
+
>>> 'str' 'ing'                  #  <-  这样操作正确
'string'
+
'string'
>>> 'str'.strip() + 'ing'  #  <-  This is ok
+
>>> 'str'.strip() + 'ing'  #  <-  这样操作正确
'string'
+
'string'
>>> 'str'.strip() 'ing'    #  <-  This is invalid
+
>>> 'str'.strip() 'ing'    #  <-  这样操作错误
  File "<stdin>", line 1, in ?
+
  File "<stdin>", line 1, in ?
    'str'.strip() 'ing'
+
    'str'.strip() 'ing'
                      ^
+
                      ^
SyntaxError: invalid syntax
+
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 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.
+
字符串可以被索引;就像 C 语言一样,字符串的第一个字符的索引为 0。没有单独的字符类型;一个字符就是长度为一的字符串。就像Icon编程语言一样,子字符串可以使用分切符来指定:用冒号分隔的两个索引。
  
>>> word[4]
+
>>> word[4]
'A'
+
'A'
>>> word[0:2]
+
>>> word[0:2]
'He'
+
'Hl'
>>> word[2:4]
+
>>> word[2:4]
'lp'
+
'ep'
  
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
+
>>> word[:2]    # 前两个字符
'He'
+
'He'
>>> word[2:]    # Everything except the first two characters
+
>>> word[2:]    # 除了前两个字符之外,其后的所有字符
'lpA'
+
'lpA'
  
Unlike a C string, Python strings cannot be changed. Assigning to an indexed position in the string results in an error:
+
不同于C字符串的是,Python字符串不能被改变。向一个索引位置赋值会导致错误:
  
>>> word[0] = 'x'
+
>>> word[0] = 'x'
Traceback (most recent call last):
+
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
+
  File "<stdin>", line 1, in ?
TypeError: 'str' object does not support item assignment
+
TypeError: 'str' object does not support item assignment
>>> word[:1] = 'Splat'
+
>>> word[:1] = 'Splat'
Traceback (most recent call last):
+
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
+
  File "<stdin>", line 1, in ?
TypeError: 'str' 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:
+
然而,用组合内容的方法来创建新的字符串是简单高效的:
  
>>> 'x' + word[1:]
+
>>> 'x' + word[1:]
'xelpA'
+
'xelpA'
>>> 'Splat' + word[4]
+
>>> 'Splat' + word[4]
'SplatA'
+
'SplatA'
  
Here’s a useful invariant of slice operations: s[:i] + s[i:] equals s.
+
在分切操作字符串时,有一个很有用的规律: s[:i] + s[i:] 等于 s.
  
>>> word[:2] + word[2:]
+
>>> word[:2] + word[2:]
'HelpA'
+
'HelpA'
>>> word[:3] + word[3:]
+
>>> word[:3] + word[3:]
'HelpA'
+
'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]
+
>>> word[1:100]
'elpA'
+
'elpA'
>>> word[10:]
+
>>> word[10:]
''
+
''
>>> word[2:1]
+
>>> word[2:1]
''
+
''
  
Indices may be negative numbers, to start counting from the right. For example:
+
在索引中可以使用负数,这将会从右往左计数。例如:
  
>>> word[-1]    # The last character
+
>>> word[-1]    # 最后一个字符
'A'
+
'A'
>>> word[-2]    # The last-but-one character
+
>>> word[-2]    # 倒数第二个字符
'p'
+
'p'
>>> word[-2:]    # The last two characters
+
>>> word[-2:]    # 最后两个字符
'pA'
+
'pA'
>>> word[:-2]    # Everything except the last two characters
+
>>> word[:-2]    # 除了最后两个字符之外,其前面的所有字符
'Hel'
+
'Hel'
  
But note that -0 is really the same as 0, so it does not count from the right!
+
但要注意, -0 0 完全一样,所以 -0 不会从右开始计数!
  
>>> word[-0]    # (since -0 equals 0)
+
>>> word[-0]    # (既然 -0 等于 0)
'H'
+
'H'
  
Out-of-range negative slice indices are truncated, but don’t try this for single-element (non-slice) indices:
+
超出范围的负数索引会被截去多余部分,但不要尝试在一个单元素索引(非分切索引)里使用:
  
>>> word[-100:]
+
>>> word[-100:]
'HelpA'
+
'HelpA'
>>> word[-10]    # error
+
>>> word[-10]    # 错误
Traceback (most recent call last):
+
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
+
  File "<stdin>", line 1, in ?
IndexError: string index out of range
+
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:
+
有一个方法可以让您记住分切索引的工作方式,想像索引是指向字符之间,第一个字符左边的数字是 0。接着,有n个字符的字符串最后一个字符的右边是索引n,例如:
  
+---+---+---+---+---+
+
  +---+---+---+---+---+
| H | e | l | p | A |
+
  | H | e | l | p | A |
+---+---+---+---+---+
+
  +---+---+---+---+---+
0  1  2  3  4  5
+
  0  1  2  3  4  5
-5  -4  -3  -2  -1
+
-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.
+
第一行的数字 0...5 给出了字符串中索引的位置;第二行给出了相应的负数索引。分切部分从 i j 分别由在边缘被标记为 i j 的全部字符组成。
  
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.
+
对于非负数分切部分,如果索引都在有效范围内,分切部分的长度就是索引的差值。例如, word[1:3] 的长度是2。
  
The built-in function len() returns the length of a string:
+
内置的函数 len() 用于返回一个字符串的长度:
  
>>> s = 'supercalifragilisticexpialidocious'
+
>>> s = 'supercalifragilisticexpialidocious'
>>> len(s)
+
>>> len(s)
34
+
34
  
See also
+
请参考
  
Sequence Types — str, bytes, bytearray, list, tuple, range
+
:序列类型——字符串,字节,字节数组,列表,元组,范围
    Strings are examples of sequence types, and support the common operations supported by such types.
+
::字符串是序列类型的一个例子,也支持以下类型的通用操作。
String Methods
+
    Strings support a large number of methods for basic transformations and searching.
+
String Formatting
+
    Information about string formatting with str.format() is described here.
+
Old String Formatting Operations
+
    The old formatting operations invoked when strings and Unicode strings are the left operand of the % operator are described in more detail here.
+
  
===3.1.3. About Unicode¶===
+
:字符串方法
 +
::字符串支持大量的方法,用于基本转换和查寻。
  
Starting with Python 3.0 all strings support Unicode (see http://www.unicode.org/).
+
:字符串格式化
 +
::关于字符串格式化的信息请参考函数 str.format() 的表述。
  
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.
+
:旧的字符串格式化操作
 +
::当字符串和Unicode字符串是%运算符左边的操作数时,请参考旧的格式化操作调用方法。
  
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:
+
===— 3.1.3. 关于Unicode===
 +
<!-- 2011-06-01 由 lilydjwg 校订到此处 -->
  
>>> 'Hello\u0020World !'
+
Python从2.0版开始全面支持 Unicode (请参考 http://www.unicode.org/ )。
'Hello World !'
+
  
The escape sequence \u0020 indicates to insert the Unicode character with the ordinal value 0x0020 (the space character) at the given position.
+
Unicode为从古至今在脚本中用到的所有字符提供了一个次序列表。起初,只有256个可用的脚本字符次序。文本通常绑定到一个被映射到脚本字符次序的代码页中。这样很容易产生混淆,尤其在国际化的软件方面更是如此(通常记作 i18n ——  'i' + 18 个字符 + 'n')。Unicode通过为全部脚本定义一个代码页来解决这些问题。
  
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.
+
如果您在字符串中包含特殊字符,您可以使用 Python Unicode-转义编码。如下例所示:
  
Apart from these standard encodings, Python provides a whole set of other ways of creating Unicode strings on the basis of a known encoding.
+
>>> 'Hello\u0020World !'
 +
'Hello World !'
  
To convert a string into a sequence of bytes using a specific encoding, string objects provide an encode() method that takes one argument, the name of the encoding. Lowercase names for encodings are preferred.
+
转义次序 \u0020 用来标志在指定的位置插入次序值是 0x0020 的Unicode字符(空格符)。
  
>>> "Äpfel".encode('utf-8')
+
其它的字符由它们各自的次序值直接解释,就像Unicode次序一样。如果您用到许多西方国家常用的标准拉丁-1编码文本串,你将会发现,较低次序的256个Unicode字符与Latin-1的256个字符一模一样。
b'\xc3\x84pfel'
+
  
===3.1.4. Lists¶===
+
除了这些标准编码,Python还提供了一整套方法用于在已知编码的基础上创建Unicode字符串。
  
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.
+
通过使用专为字符串对像提供的 encode() 方法,可以将一个字符串转换为使用特定编码的字节序列,以编码的名称作为参数。首选小写的编码名称。
  
>>> a = ['spam', 'eggs', 100, 1234]
+
>>> "Äpfel".encode('utf-8')
>>> a
+
b'\xc3\x84pfel'
['spam', 'eggs', 100, 1234]
+
  
Like string indices, list indices start at 0, and lists can be sliced, concatenated and so on:
+
===— 3.1.4. 列表===
  
>>> a[0]
+
Python囊括了大量的复合数据类型,用于组织其它数值。最有用的是列表,即写在方括号之间、用逗号分隔开的数值列表。列表内的项目不必全是相同的类型。
'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!']
+
  
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]
 +
>>> a
 +
['spam', 'eggs', 100, 1234]
  
>>> a[:]
+
同字符串索引一样,列表索引也是从 0 开始,列表也可以被分切,被串联,等等:
['spam', 'eggs', 100, 1234]
+
  
Unlike strings, which are immutable, it is possible to change individual elements of a list:
+
>>> 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!']
  
>>> a
+
所有的分切操作返回一个包含有所需元素的新列表。如下例中,分切将返回列表 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:
+
>>> a[:]
 +
['spam', 'eggs', 100, 1234]
  
>>> # 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 len() also applies to lists:
+
>>> a
 +
['spam', 'eggs', 100, 1234]
 +
>>> a[2] = a[2] + 23
 +
>>> a
 +
['spam', 'eggs', 123, 1234]
  
>>> a = ['a', 'b', 'c', 'd']
+
通过可以给分切部分赋值,可以改变列表的大小甚至清空列表:
>>> len(a)
+
4
+
  
It is possible to nest lists (create lists containing other lists), for example:
+
>>> # 取代一些项目:
 +
... a[0:2] = [1, 12]
 +
>>> a
 +
[1, 12, 123, 1234]
 +
>>> # 删除一些:
 +
... a[0:2] = []
 +
>>> a
 +
[123, 1234]
 +
>>> # 插入一些:
 +
... a[1:1] = ['bletch', 'xyzzy']
 +
>>> a
 +
[123, 'bletch', 'xyzzy', 1234]
 +
>>> # 在开头插入它本身(一个副本)
 +
>>> a[:0] = a
 +
>>> a
 +
[123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
 +
>>> # 清除列表:把所有项目替换为空列表
 +
>>> a[:] = []
 +
>>> a
 +
[]
  
>>> q = [2, 3]
+
内置函数 len() 也能应用于列表:
>>> p = [1, q, 4]
+
>>> len(p)
+
3
+
>>> p[1]
+
[2, 3]
+
>>> p[1][0]
+
2
+
  
You can add something to the end of the list:
+
>>> a = ['a', 'b', 'c', 'd']
 +
>>> len(a)
 +
4
  
>>> p[1].append('xtra')
+
也可以使用嵌套列表(在列表里创建其它列表),例如:
>>> 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.
+
>>> q = [2, 3]
==3.2. First Steps Towards Programming¶==
+
>>> p = [1, q, 4]
 +
>>> len(p)
 +
3
 +
>>> p[1]
 +
[2, 3]
 +
>>> p[1][0]
 +
2
  
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:
+
>>> p[1].append('xtra')
... # the sum of two elements defines the next
+
>>> p
... a, b = 0, 1
+
[1, [2, 3, 'xtra'], 4]
>>> while b < 10:
+
>>> q
...    print(b)
+
[2, 3, 'xtra']
...    a, b = b, a+b
+
...
+
1
+
1
+
2
+
3
+
5
+
8
+
  
This example introduces several new features.
+
注意,在最后一个例子里,p[1] 和 q 实际上指向同一个对象!稍后我们将回到对象语法中继续讨论。
  
    *
+
==— 3.2. 迈出编程第一步==
  
      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.
+
当然,我们能使用Python完成比 2+2 更复杂的工作。在下例里,我们能写出一个初步的斐波纳契数列如下:
    *
+
  
      The 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).
+
>>> # Fibonacci series: 斐波纳契数列
    *
+
... # 两个元素的总和确定了下一个数
 +
... a, b = 0, 1
 +
>>> while b < 10:
 +
...    print(b)
 +
...    a, b = b, a+b
 +
...
 +
1
 +
1
 +
2
 +
3
 +
5
 +
8
  
      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.
+
这个例子介绍了几个新特征。
    *
+
  
      The 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, 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:
+
*第一行包含了一个复合赋值:变量 a 和 b 同时得到新值 0 和 1。最后一行再次使用了同样的方法,可以看到,右边的表达式会在赋值变动之前执行。右边表达式的执行顺序是从左往右的。
  
      >>> i = 256*256
+
*只要条件(这里是: b < 10 )为真,while循环将一直执行下去。在 Python 里,和C语言中一样,任何非零整数为真;零为假。限定条件可以是一个字符串或是一个列表,事实上可以是任何序列;任何非零长度的值都是真,空序列是假。在例子中用于测试的是一个简单的比较。标准的比较操作写法和C语言一样: < (小于), > (大于), == (等于), <= (小于等于), >= (大于等于)和 != (不等于)。
      >>> print('The value of i is', i)
+
      The value of i is 65536
+
  
      The keyword end can be used to avoid the newline after the output, or end the output with a different string:
+
*书写循环体时需要缩进:缩进是Python组合语句的主要方法。Python(暂)没有提供一个智能输入行的编辑功能,因此您必须在每一个缩进行键入一个制表符或空格符。在实践中您将会乐于使用一个文本编辑器,用以编写更复杂的Python程序;许多文本编辑器有自动缩进功能。当使用解释器交互输入一个复合语句时,语句块必须紧接一个空行表示该语句块结束(因为解释器不会知道您什么时候才会输入最后一行)。注意,同一语句块内的每一行都必须有相同的缩进量。
  
      >>> a, b = 0, 1
+
*print() 函数输出表达式被赋予的值。它在处理复合表达式、浮点数和字符串方面,不同于仅仅输出的您想写出的表达式(就像我们较早前计算器的例子)。字符串的输出没有引号,各项之间插入空格,所以您可以很好地格式化输出,就像这样:
      >>> while b < 1000:
+
      ...    print(b, end=',')
+
      ...    a, b = b, a+b
+
      ...
+
      1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,
+
  
'''<center>————&mdash; [[Python_官方教程|返回《 Python 官方教程 》目录]] &mdash;————</center>'''
+
>>> i = 256*256
 +
>>> print('The value of i is', i)
 +
The value of i is 65536
 +
 
 +
关键字end可以被用于防止输出新的一行,或者在输出的末尾添加不同的字符:
 +
 
 +
>>> a, b = 0, 1
 +
>>> while b < 1000:
 +
...    print(b, end=',')
 +
...    a, b = b, a+b
 +
...
 +
1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,
 +
 
 +
'''<center>————&mdash; [[Python_官方简明教程|返回《 Python 官方简明教程 》目录]] &mdash;————</center>'''
 
'''<center>—&mdash; [[编程语言|返回《 Python 手册 》总目录]] &mdash;—</center>'''
 
'''<center>—&mdash; [[编程语言|返回《 Python 手册 》总目录]] &mdash;—</center>'''

2012年11月20日 (二) 23:29的最新版本

— 3.Python概述

感谢Lilydjwg和remeber翻译并校对本页,可以到readthedocs.org查看更新更专业的翻译版本

在随后的例子中,输入前有提示符(>>> 和 ...),而输出前没有:要重复例子,需在提示符后输入所有字符;不以提示符开头的行是解释器的输出。记住,在例子中独自占一行的次级提示符表示您必须键入一个空行;这用于结束一个多行命令。

本手册中的部分例子,包括注释都是在交互提示符后输入的。Python中的注释由井号 # (the hash character)开始,直到物理行的末尾。注释可以从行首开始,或者跟在空白或代码之后,但不能位于字符串内。字符串里的井号只是井号。由于注释是用来解释代码的,不会被Python解释,所以在实践操作中您不必输入例子中的注释。

例如:

# 这是第一行注释
SPAM = 1                 # 这是第二行注释
                         # ……第三行了!
STRING = "# 这不是注释。"

— 3.1. 把Python作为一个计算器

让我们尝试一些简单的Python命令。打开解释器等待主提示符 >>> 的出现(不会花很久)。

— 3.1.1. 数字

解释器可以作为一个简单的计算器:您可以在解释器里输入一个表达式,它将输出表达式的值。表达式的语法很直白: +, -, * 和 / 和在许多其它语言(如Pascal或C)里一样;括号可以用来为运算分组。例如:

>>> 2+2
4
>>> # 这是一行注释
... 2+2
4
>>> 2+2  # 这是和代码同在一行的注释
4
>>> (50-5*6)/4
5.0
>>> 8/5 # 整数除法不会丢失分数部分
1.6

注意:在不同的机器上浮点运算的结果可能会不一样。之后我们会介绍有关控制浮点运算输出结果的内容。关于浮点数及其表示方式的微妙之处的全面细致的讨论,请参考《浮点运算:问题和限制》。

在整数除法中,如果只想得到整数的结果,丢弃可能的分数部分,可以使用运算符 // :

>>> # 整数除法返回向下取整后的结果:
... 7//3
2
>>> 7//-3
-3

等号(‘=’)用于给变量赋值。赋值之后,除了下一个提示符,解释器不会显示任何结果。

>>> width = 20
>>> height = 5*9
>>> width * height
900

一个值可以同时赋予多个变量:

>>> x = y = z = 0  # 清零 x, y 和 z
>>> x
0
>>> y
0
>>> z
0

变量在使用前必须先“定义”(即赋予变量一个值),否则会出现错误:

>>> # 尝试访问一个未定义的变量
... n
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'n' is not defined

浮点数得到完全的支持;不同类型的数混合运算时会将整数转换为浮点数:

>>> 3 * 3.75 / 1.5
7.5
>>> 7.0 / 2
3.5

复数也支持;虚数带有 j 或 J 后缀。带有非零实部的复数记为 (实部+虚部j),或者使用 complex(实部, 虚部) 函数来创建。

>>> 1j * 1J
(-1+0j)
>>> 1j * complex(0, 1)
(-1+0j)
>>> 3+1j*3
(3+3j)
>>> (3+1j)*3
(9+3j)
>>> (1+2j)/(1+1j)
(1.5+0.5j)

复数总是由两个浮点数来表示,实部和虚部。若要从复数z中提取其中一部分,可使用 z.real 和 z.imag。

>>> a=1.5+0.5j
>>> a.real
1.5
>>> a.imag
0.5

浮点数和整数之间的转换函数(float(), int())不能用于复数——尚没办法将复数转换为实数。但可以使用 abs(z) 获得复数的模(结果是浮点数),或者使用 z.real 获取其实部:

>>> 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

在交互模式中,最后被输出的表达式结果被赋值给变量 _ 。这能使您在把Python作为一个桌面计算器使用时使后续计算更方便,例如:

>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06

此处, _ 变量应被用户视为只读变量。不要显式地给它赋值——这样您将会创建一个具有相同名称的独立的本地变量,并且屏蔽了这个内置变量的功能。

— 3.1.2. 字符串

除了数字,Python也能操作字符串。字符串有几种表达方式,可以使用单引号或双引号括起来:

>>> '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.'

解释器以输入时相同的方式输出字符串操作的结果,即使用引号括起来,并且使用反斜杠转义引号和其它特殊字符来准确地表示。如果字符串包含有单引号但不含双引号,则字符串会用双引号括起来,否则用单引号括起来。对于这样的输入字符串,print() 函数会产生更易读的输出。

跨行的字面字符串可用以下几种方法表示。使用续行符,即在每行最后一个字符后使用反斜线来说明下一行是上一行逻辑上的延续:

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."

print(hello)

注意,其中的换行符仍然要使用 \n 表示——反斜杠后的换行符被丢弃了。以上例子将如下输出:

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.

或者,字符串可以被 """ (三个双引号)或者 ''' (三个单引号)括起来。使用三引号时,换行符不需要转义,它们会包含在字符串中。以下的例子使用了一个转义符,避免在最开始产生一个不需要的空行。

print("""\
Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to
""")

其输出如下:

Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to

如果我们使用“原始 (raw)”字符串,那么 \n 不会被转换成换行,行末的的反斜杠,以及源码中的换行符,都将作为数据包含在字符串内。例如:

hello = r"This is a rather long string containing\n\
several lines of text much as you would do in C."

print(hello)

将会输出:

This is a rather long string containing\n\
several lines of text much as you would do in C.

字符串可以使用 + 运算符串连接在一起,或者用 * 运算符重复:

>>> word = 'Help' + 'A'
>>> word
'HelpA'
>>> '<' + word*5 + '>'
'<HelpAHelpAHelpAHelpAHelpA>'

两个紧邻的字面字符串将自动被串连;上例的第一行也可以写成 word = 'Help' 'A' ;这样的操作只在两个字面值间有效,不能随意用于字符串表达式中:

>>> 'str' 'ing'                   #  <-  这样操作正确
'string'
>>> 'str'.strip() + 'ing'   #  <-  这样操作正确
'string'
>>> 'str'.strip() 'ing'     #  <-  这样操作错误
  File "<stdin>", line 1, in ?
    'str'.strip() 'ing'
                      ^
SyntaxError: invalid syntax

字符串可以被索引;就像 C 语言一样,字符串的第一个字符的索引为 0。没有单独的字符类型;一个字符就是长度为一的字符串。就像Icon编程语言一样,子字符串可以使用分切符来指定:用冒号分隔的两个索引。

>>> word[4]
'A'
>>> word[0:2]
'Hl'
>>> word[2:4]
'ep'

默认的分切索引很有用:默认的第一个索引为零,第二个索引默认为字符串可以被分切的长度。

>>> word[:2]    # 前两个字符
'He'
>>> word[2:]    # 除了前两个字符之外,其后的所有字符
'lpA'

不同于C字符串的是,Python字符串不能被改变。向一个索引位置赋值会导致错误:

>>> word[0] = 'x'
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: 'str' object does not support item assignment
>>> word[:1] = 'Splat'
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: 'str' object does not support slice assignment

然而,用组合内容的方法来创建新的字符串是简单高效的:

>>> 'x' + word[1:]
'xelpA'
>>> 'Splat' + word[4]
'SplatA'

在分切操作字符串时,有一个很有用的规律: s[:i] + s[i:] 等于 s.

>>> word[:2] + word[2:]
'HelpA'
>>> word[:3] + word[3:]
'HelpA'

对于有偏差的分切索引的处理方式也很优雅:一个过大的索引将被字符串的大小取代,上限值小于下限值将返回一个空字符串。

>>> word[1:100]
'elpA'
>>> word[10:]

>>> word[2:1]

在索引中可以使用负数,这将会从右往左计数。例如:

>>> word[-1]     # 最后一个字符
'A'
>>> word[-2]     # 倒数第二个字符
'p'
>>> word[-2:]    # 最后两个字符
'pA'
>>> word[:-2]    # 除了最后两个字符之外,其前面的所有字符
'Hel'

但要注意, -0 和 0 完全一样,所以 -0 不会从右开始计数!

>>> word[-0]     # (既然 -0 等于 0)
'H'

超出范围的负数索引会被截去多余部分,但不要尝试在一个单元素索引(非分切索引)里使用:

>>> word[-100:]
'HelpA'
>>> word[-10]    # 错误
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
IndexError: string index out of range

有一个方法可以让您记住分切索引的工作方式,想像索引是指向字符之间,第一个字符左边的数字是 0。接着,有n个字符的字符串最后一个字符的右边是索引n,例如:

 +---+---+---+---+---+
 | H | e | l | p | A |
 +---+---+---+---+---+
 0   1   2   3   4   5
-5  -4  -3  -2  -1

第一行的数字 0...5 给出了字符串中索引的位置;第二行给出了相应的负数索引。分切部分从 i 到 j 分别由在边缘被标记为 i 和 j 的全部字符组成。

对于非负数分切部分,如果索引都在有效范围内,分切部分的长度就是索引的差值。例如, word[1:3] 的长度是2。

内置的函数 len() 用于返回一个字符串的长度:

>>> s = 'supercalifragilisticexpialidocious'
>>> len(s)
34

请参考

序列类型——字符串,字节,字节数组,列表,元组,范围
字符串是序列类型的一个例子,也支持以下类型的通用操作。
字符串方法
字符串支持大量的方法,用于基本转换和查寻。
字符串格式化
关于字符串格式化的信息请参考函数 str.format() 的表述。
旧的字符串格式化操作
当字符串和Unicode字符串是%运算符左边的操作数时,请参考旧的格式化操作调用方法。

— 3.1.3. 关于Unicode

Python从2.0版开始全面支持 Unicode (请参考 http://www.unicode.org/ )。

Unicode为从古至今在脚本中用到的所有字符提供了一个次序列表。起初,只有256个可用的脚本字符次序。文本通常绑定到一个被映射到脚本字符次序的代码页中。这样很容易产生混淆,尤其在国际化的软件方面更是如此(通常记作 i18n —— 'i' + 18 个字符 + 'n')。Unicode通过为全部脚本定义一个代码页来解决这些问题。

如果您在字符串中包含特殊字符,您可以使用 Python Unicode-转义编码。如下例所示:

>>> 'Hello\u0020World !'
'Hello World !'

转义次序 \u0020 用来标志在指定的位置插入次序值是 0x0020 的Unicode字符(空格符)。

其它的字符由它们各自的次序值直接解释,就像Unicode次序一样。如果您用到许多西方国家常用的标准拉丁-1编码文本串,你将会发现,较低次序的256个Unicode字符与Latin-1的256个字符一模一样。

除了这些标准编码,Python还提供了一整套方法用于在已知编码的基础上创建Unicode字符串。

通过使用专为字符串对像提供的 encode() 方法,可以将一个字符串转换为使用特定编码的字节序列,以编码的名称作为参数。首选小写的编码名称。

>>> "Äpfel".encode('utf-8')
b'\xc3\x84pfel'

— 3.1.4. 列表

Python囊括了大量的复合数据类型,用于组织其它数值。最有用的是列表,即写在方括号之间、用逗号分隔开的数值列表。列表内的项目不必全是相同的类型。

>>> a = ['spam', 'eggs', 100, 1234]
>>> a
['spam', 'eggs', 100, 1234]

同字符串索引一样,列表索引也是从 0 开始,列表也可以被分切,被串联,等等:

>>> 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!']

所有的分切操作返回一个包含有所需元素的新列表。如下例中,分切将返回列表 a 的一个拷贝:

>>> a[:]
['spam', 'eggs', 100, 1234]

与字符串的一成不变有所不同的是,列表可以改变其中的个别元素:

>>> a
['spam', 'eggs', 100, 1234]
>>> a[2] = a[2] + 23
>>> a
['spam', 'eggs', 123, 1234]

通过可以给分切部分赋值,可以改变列表的大小甚至清空列表:

>>> # 取代一些项目:
... a[0:2] = [1, 12]
>>> a
[1, 12, 123, 1234]
>>> # 删除一些:
... a[0:2] = []
>>> a
[123, 1234]
>>> # 插入一些:
... a[1:1] = ['bletch', 'xyzzy']
>>> a
[123, 'bletch', 'xyzzy', 1234]
>>> # 在开头插入它本身(一个副本)
>>> a[:0] = a
>>> a
[123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
>>> # 清除列表:把所有项目替换为空列表
>>> a[:] = []
>>> a
[]

内置函数 len() 也能应用于列表:

>>> a = ['a', 'b', 'c', 'd']
>>> len(a)
4

也可以使用嵌套列表(在列表里创建其它列表),例如:

>>> q = [2, 3]
>>> p = [1, q, 4]
>>> len(p)
3
>>> p[1]
[2, 3]
>>> p[1][0]
2

您可以在列表的末尾添加项目:

>>> p[1].append('xtra')
>>> p
[1, [2, 3, 'xtra'], 4]
>>> q
[2, 3, 'xtra']

注意,在最后一个例子里,p[1] 和 q 实际上指向同一个对象!稍后我们将回到对象语法中继续讨论。

— 3.2. 迈出编程第一步

当然,我们能使用Python完成比 2+2 更复杂的工作。在下例里,我们能写出一个初步的斐波纳契数列如下:

>>> # Fibonacci series: 斐波纳契数列
... # 两个元素的总和确定了下一个数
... a, b = 0, 1
>>> while b < 10:
...     print(b)
...     a, b = b, a+b
...
1
1
2
3
5
8

这个例子介绍了几个新特征。

  • 第一行包含了一个复合赋值:变量 a 和 b 同时得到新值 0 和 1。最后一行再次使用了同样的方法,可以看到,右边的表达式会在赋值变动之前执行。右边表达式的执行顺序是从左往右的。
  • 只要条件(这里是: b < 10 )为真,while循环将一直执行下去。在 Python 里,和C语言中一样,任何非零整数为真;零为假。限定条件可以是一个字符串或是一个列表,事实上可以是任何序列;任何非零长度的值都是真,空序列是假。在例子中用于测试的是一个简单的比较。标准的比较操作写法和C语言一样: < (小于), > (大于), == (等于), <= (小于等于), >= (大于等于)和 != (不等于)。
  • 书写循环体时需要缩进:缩进是Python组合语句的主要方法。Python(暂)没有提供一个智能输入行的编辑功能,因此您必须在每一个缩进行键入一个制表符或空格符。在实践中您将会乐于使用一个文本编辑器,用以编写更复杂的Python程序;许多文本编辑器有自动缩进功能。当使用解释器交互输入一个复合语句时,语句块必须紧接一个空行表示该语句块结束(因为解释器不会知道您什么时候才会输入最后一行)。注意,同一语句块内的每一行都必须有相同的缩进量。
  • print() 函数输出表达式被赋予的值。它在处理复合表达式、浮点数和字符串方面,不同于仅仅输出的您想写出的表达式(就像我们较早前计算器的例子)。字符串的输出没有引号,各项之间插入空格,所以您可以很好地格式化输出,就像这样:
>>> i = 256*256
>>> print('The value of i is', i)
The value of i is 65536

关键字end可以被用于防止输出新的一行,或者在输出的末尾添加不同的字符:

>>> a, b = 0, 1
>>> while b < 1000:
...     print(b, end=',')
...     a, b = b, a+b
...
1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,
————— 返回《 Python 官方简明教程 》目录 —————
—— 返回《 Python 手册 》总目录 ——