个人工具

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

来自Ubuntu中文

跳转至: 导航, 搜索
— 3.1.2. Strings
— 3.1.2. 字符串
 
(未显示7个用户的44个中间版本)
第1行: 第1行:
=— 3. 非正式地介绍Python(译文尚未校对)=
+
=— 3.Python概述=
 +
'''感谢Lilydjwg和remeber翻译并校对本页,可以到[http://docspy3zh.readthedocs.org/en/latest/tutorial/index.html readthedocs.org]查看更新更专业的翻译版本'''
  
在随后的例子中,输入和输出被分别显示在时隐时现的提示符(>>> 和 ...)之后:为了实践这个例子,当提示符出现时,您必须在提示符后用键盘输入每一个字符;每一行不会由一个解释器输出的提示符起头。记住,在例子中独自占一行的提示符表示您必须键入一个空行;这是用于结束一个多行命令。
+
在随后的例子中,输入前有提示符(>>> 和 ...),而输出前没有:要重复例子,需在提示符后输入所有字符;不以提示符开头的行是解释器的输出。记住,在例子中独自占一行的次级提示符表示您必须键入一个空行;这用于结束一个多行命令。
  
本手册中的一些例子,甚至在交互提示符后输入的内容中,包括了注释。Python中的注释由哈希字符 # 起头,一直延续到物理行尾。一个注释可以从行首开始显示,或者跟随在空格或代码之后,但不能位于文本串里面。一个文本串里的哈希字符就只是一个哈希字符。既然注释是用来阐述说明代码的,不会被Python解释执行,所以您可以在实践操作中省略键入例子中的注释。
+
本手册中的部分例子,包括注释都是在交互提示符后输入的。Python中的注释由井号 # (the hash character)开始,直到物理行的末尾。注释可以从行首开始,或者跟在空白或代码之后,但不能位于字符串内。字符串里的井号只是井号。由于注释是用来解释代码的,不会被Python解释,所以在实践操作中您不必输入例子中的注释。
  
 
例如:
 
例如:
第9行: 第10行:
 
  # 这是第一行注释
 
  # 这是第一行注释
 
  SPAM = 1                # 这是第二行注释
 
  SPAM = 1                # 这是第二行注释
                           # ……这是第二行注释!
+
                           # ……第三行了!
  STRING = "# 这不是一行注释。"
+
  STRING = "# 这不是注释。"
  
 
==— 3.1. 把Python作为一个计算器==
 
==— 3.1. 把Python作为一个计算器==
  
让我们尝试一些简单的Python命令。打开解释器等待主提示符>>>的出现(不需要很长时间)。
+
让我们尝试一些简单的Python命令。打开解释器等待主提示符 >>> 的出现(不会花很久)。
  
 
===— 3.1.1. 数字===
 
===— 3.1.1. 数字===
  
解释器可以作为一个简单计算器:您可以在解释器里键入一个表达式,它将返回计算出的数值。表达式的语法很直白:和其它程序语言使用同样的操作符 + - * 和 / (如同Pascal语言或C语言);括号可以用来分组计算。例如:
+
解释器可以作为一个简单的计算器:您可以在解释器里输入一个表达式,它将输出表达式的值。表达式的语法很直白: +, -, * 和 / 和在许多其它语言(如Pascal或C)里一样;括号可以用来为运算分组。例如:
  
 
  >>> 2+2
 
  >>> 2+2
第29行: 第30行:
 
  >>> (50-5*6)/4
 
  >>> (50-5*6)/4
 
  5.0
 
  5.0
  >>> 8/5 # 整数除法时不会丢失分数
+
  >>> 8/5 # 整数除法不会丢失分数部分
 
  1.6
 
  1.6
  
注意:在不同的机器上浮点运算的结果可能会不一样。在稍后的章节中我们会介绍有关控制浮点运算输出的内容。请参考《浮点算法》:关于浮点数及其概念全面细致的研究中出现的问题和限制。
+
注意:在不同的机器上浮点运算的结果可能会不一样。之后我们会介绍有关控制浮点运算输出结果的内容。关于浮点数及其表示方式的微妙之处的全面细致的讨论,请参考《浮点运算:问题和限制》。
  
在整数除法中,如果只想得到整数的结果(商或余数),丢弃其它零碎的结果,可以使用操作符 // :
+
在整数除法中,如果只想得到整数的结果,丢弃可能的分数部分,可以使用运算符 // :
  
  >>> # 整数除法返回值:
+
  >>> # 整数除法返回向下取整后的结果:
 
  ... 7//3
 
  ... 7//3
 
  2
 
  2
第42行: 第43行:
 
  -3
 
  -3
  
等号符(‘=’)用于给变量赋值。赋值之后,在解释器的提示符前不会有任何结果显示。
+
等号(‘=’)用于给变量赋值。赋值之后,除了下一个提示符,解释器不会显示任何结果。
  
 
  >>> width = 20
 
  >>> width = 20
第49行: 第50行:
 
  900
 
  900
  
可以同时给多个变量赋予同一个值:
+
一个值可以同时赋予多个变量:
  
  >>> x = y = z = 0  # Zero x, y and z
+
  >>> x = y = z = 0  # 清零 x, y z
 
  >>> x
 
  >>> x
 
  0
 
  0
第59行: 第60行:
 
  0
 
  0
  
在使用变量之前必须先“定义”变量(即赋予变量一个值),否则会有出错提示:
+
变量在使用前必须先“定义”(即赋予变量一个值),否则会出现错误:
  
  >>> # try to access an undefined variable
+
  >>> # 尝试访问一个未定义的变量
 
  ... n
 
  ... n
 
  Traceback (most recent call last):
 
  Traceback (most recent call last):
第67行: 第68行:
 
  NameError: name 'n' is not defined
 
  NameError: name 'n' is not defined
  
完全支持浮点数;可以进行类型混合的整数转换为浮点数操作运算:
+
浮点数得到完全的支持;不同类型的数混合运算时会将整数转换为浮点数:
  
 
  >>> 3 * 3.75 / 1.5
 
  >>> 3 * 3.75 / 1.5
第74行: 第75行:
 
  3.5
 
  3.5
  
也支持复数;虚数带有 j 或 J 后缀。带有非零实部的复数记为 (实部+虚部j),或者使用 complex(实部,虚部) 这功能来创建复数。
+
复数也支持;虚数带有 j 或 J 后缀。带有非零实部的复数记为 (实部+虚部j),或者使用 complex(实部, 虚部) 函数来创建。
  
 
  >>> 1j * 1J
 
  >>> 1j * 1J
第87行: 第88行:
 
  (1.5+0.5j)
 
  (1.5+0.5j)
  
复数总是由两个浮点数来表示,实部和虚部。如果想要从复数z中提取其中一部分,使用 z.real 和 z.imag。
+
复数总是由两个浮点数来表示,实部和虚部。若要从复数z中提取其中一部分,可使用 z.real 和 z.imag。
  
 
  >>> a=1.5+0.5j
 
  >>> a=1.5+0.5j
第95行: 第96行:
 
  0.5
 
  0.5
  
浮点数和整数之间的转换功能(float(), int())不能用于复数——没有一个正确的方法可以把复数转换为实数。可以使用 abs(z) 获得复数的量值,或者使用 z.real 获得复数的实部:
+
浮点数和整数之间的转换函数(float(), int())不能用于复数——尚没办法将复数转换为实数。但可以使用 abs(z) 获得复数的模(结果是浮点数),或者使用 z.real 获取其实部:
  
 
  >>> a=3.0+4.0j
 
  >>> a=3.0+4.0j
第109行: 第110行:
 
  5.0
 
  5.0
  
在交互模式中,最后被输出的表达式结果被赋予变量 _ 。这能令您在把Python作为一个桌面计算器使用时,在连续计算方面应用得更方便,例如:
+
在交互模式中,最后被输出的表达式结果被赋值给变量 _ 。这能使您在把Python作为一个桌面计算器使用时使后续计算更方便,例如:
  
 
  >>> tax = 12.5 / 100
 
  >>> tax = 12.5 / 100
第120行: 第121行:
 
  113.06
 
  113.06
  
这个 _ 变量应该被用户视为只读变量。不要故意地给它赋值——这样您将会创建一个具有相同名称的独立本地变量,并且掩蔽了这个内置变量特有的魔力。
+
此处, _ 变量应被用户视为只读变量。不要显式地给它赋值——这样您将会创建一个具有相同名称的独立的本地变量,并且屏蔽了这个内置变量的功能。
  
 
===— 3.1.2. 字符串===
 
===— 3.1.2. 字符串===
  
除了数字,Python也能以几种方式来操作字符串。字符串可以被封装在单引号或双引号中:
+
除了数字,Python也能操作字符串。字符串有几种表达方式,可以使用单引号或双引号括起来:
  
 
  >>> 'spam eggs'
 
  >>> 'spam eggs'
第139行: 第140行:
 
  '"Isn\'t," she said.'
 
  '"Isn\'t," she said.'
  
如果字符串在输入时被封装在引号内,或者引号和其它特殊字符被反斜杠这个转义符忽略时,解释器将会输出同样的字符串操作结果,返回明确的值。如果字符串包含有单引号不包含有双引号,则字符串应该被双引号封装,否则只用总引号封装即可。这样输入的字符串能让print() 函数产生更容易阅读的输出结果。
+
解释器以输入时相同的方式输出字符串操作的结果,即使用引号括起来,并且使用反斜杠转义引号和其它特殊字符来准确地表示。如果字符串包含有单引号但不含双引号,则字符串会用双引号括起来,否则用单引号括起来。对于这样的输入字符串,print() 函数会产生更易读的输出。
  
字符串文本可以用以下几种方法跨过多行。可以用续行符,即在每行最后一个字符后使用反斜线来说明下一行是这一行逻辑上的延续:
+
跨行的字面字符串可用以下几种方法表示。使用续行符,即在每行最后一个字符后使用反斜线来说明下一行是上一行逻辑上的延续:
  
 
  hello = "This is a rather long string containing\n\
 
  hello = "This is a rather long string containing\n\
第150行: 第151行:
 
  print(hello)
 
  print(hello)
  
注意,要换新的一行输出仍然需要嵌入字符 \n ——如果新的一行直接跟着反斜杠则不会换到新的一行输出。这个例子将会输出如下:
+
注意,其中的换行符仍然要使用 \n 表示——反斜杠后的换行符被丢弃了。以上例子将如下输出:
  
 
  This is a rather long string containing
 
  This is a rather long string containing
第156行: 第157行:
 
     Note that whitespace at the beginning of the line is significant.
 
     Note that whitespace at the beginning of the line is significant.
  
或者,字符串可以被三个双引号 """ 或者三个单引号封装。使用三引号时,行的末端不必再添加转义符反斜杠,但这些行仍将被包括在同一个字符串内。因此以下的例子使用了一个义符反斜杠,防止在输出刚开始时产生一个不需要的空行。
+
或者,字符串可以被 """ (三个双引号)或者<nowiki> ''' </nowiki>(三个单引号)括起来。使用三引号时,换行符不需要转义,它们会包含在字符串中。以下的例子使用了一个转义符,避免在最开始产生一个不需要的空行。
  
 
  print("""\
 
  print("""\
第164行: 第165行:
 
  """)
 
  """)
  
随后产生的输出如下:
+
其输出如下:
  
 
  Usage: thingy [OPTIONS]
 
  Usage: thingy [OPTIONS]
第170行: 第171行:
 
       -H hostname              Hostname to connect to
 
       -H hostname              Hostname to connect to
  
如果我们在一个“raw”后使用字符串,那么 \n 不会被转换成新的一行输出,但是行尾末端的的反斜杠,以及新的一行的源代码,都将作为整体数据包括在这个字符串内。例如这样:
+
如果我们使用“原始 (raw)”字符串,那么 \n 不会被转换成换行,行末的的反斜杠,以及源码中的换行符,都将作为数据包含在字符串内。例如:
  
 
  hello = r"This is a rather long string containing\n\
 
  hello = r"This is a rather long string containing\n\
第182行: 第183行:
 
  several lines of text much as you would do in C.
 
  several lines of text much as you would do in C.
  
字符串可以使用 + 操作符串连在一起,或者用 * 操作符重复:
+
字符串可以使用 + 运算符串连接在一起,或者用 * 运算符重复:
  
 
  >>> word = 'Help' + 'A'
 
  >>> word = 'Help' + 'A'
第190行: 第191行:
 
  '<HelpAHelpAHelpAHelpAHelpA>'
 
  '<HelpAHelpAHelpAHelpAHelpA>'
  
两个紧靠在一起的字符串文本将自动串连;上例的第一行也可以写成 word = 'Help' 'A' ;这样的操作只在两个文本中有效,不能随意用于字符串表达式中:
+
两个紧邻的字面字符串将自动被串连;上例的第一行也可以写成 word = 'Help' 'A' ;这样的操作只在两个字面值间有效,不能随意用于字符串表达式中:
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'                  #  <-  这样操作正确
 
  >>> 'str' 'ing'                  #  <-  这样操作正确
第203行: 第203行:
 
  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'
第230行: 第230行:
 
  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:]
第237行: 第237行:
 
  '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:]
第244行: 第244行:
 
  '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]
第253行: 第253行:
 
  ''
 
  ''
  
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,例如:
  
 
   +---+---+---+---+---+
 
   +---+---+---+---+---+
第286行: 第286行:
 
  -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'
第296行: 第296行:
 
  34
 
  34
  
See also
+
请参考
 +
 
 +
:序列类型——字符串,字节,字节数组,列表,元组,范围
 +
::字符串是序列类型的一个例子,也支持以下类型的通用操作。
 +
 
 +
:字符串方法
 +
::字符串支持大量的方法,用于基本转换和查寻。
 +
 
 +
:字符串格式化
 +
::关于字符串格式化的信息请参考函数 str.format() 的表述。
  
Sequence Types — str, bytes, bytearray, list, tuple, range
+
:旧的字符串格式化操作
    Strings are examples of sequence types, and support the common operations supported by such types.
+
::当字符串和Unicode字符串是%运算符左边的操作数时,请参考旧的格式化操作调用方法。
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===
+
===— 3.1.3. 关于Unicode===
 +
<!-- 2011-06-01 由 lilydjwg 校订到此处 -->
  
Starting with Python 3.0 all strings support Unicode (see http://www.unicode.org/).
+
Python从2.0版开始全面支持 Unicode (请参考 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 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为从古至今在脚本中用到的所有字符提供了一个次序列表。起初,只有256个可用的脚本字符次序。文本通常绑定到一个被映射到脚本字符次序的代码页中。这样很容易产生混淆,尤其在国际化的软件方面更是如此(通常记作 i18n ——  'i' + 18 个字符 + 'n')。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:
+
如果您在字符串中包含特殊字符,您可以使用 Python Unicode-转义编码。如下例所示:
  
 
  >>> 'Hello\u0020World !'
 
  >>> 'Hello\u0020World !'
 
  'Hello World !'
 
  'Hello World !'
  
The escape sequence \u0020 indicates to insert the Unicode character with the ordinal value 0x0020 (the space character) at the given position.
+
转义次序 \u0020 用来标志在指定的位置插入次序值是 0x0020 的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.
+
其它的字符由它们各自的次序值直接解释,就像Unicode次序一样。如果您用到许多西方国家常用的标准拉丁-1编码文本串,你将会发现,较低次序的256个Unicode字符与Latin-1的256个字符一模一样。
  
Apart from these standard encodings, Python provides a whole set of other ways of creating Unicode strings on the basis of a known encoding.
+
除了这些标准编码,Python还提供了一整套方法用于在已知编码的基础上创建Unicode字符串。
  
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.
+
通过使用专为字符串对像提供的 encode() 方法,可以将一个字符串转换为使用特定编码的字节序列,以编码的名称作为参数。首选小写的编码名称。
  
 
  >>> "Äpfel".encode('utf-8')
 
  >>> "Äpfel".encode('utf-8')
 
  b'\xc3\x84pfel'
 
  b'\xc3\x84pfel'
  
===— 3.1.4. Lists===
+
===— 3.1.4. 列表===
  
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.
+
Python囊括了大量的复合数据类型,用于组织其它数值。最有用的是列表,即写在方括号之间、用逗号分隔开的数值列表。列表内的项目不必全是相同的类型。
  
 
  >>> a = ['spam', 'eggs', 100, 1234]
 
  >>> a = ['spam', 'eggs', 100, 1234]
第337行: 第341行:
 
  ['spam', 'eggs', 100, 1234]
 
  ['spam', 'eggs', 100, 1234]
  
Like string indices, list indices start at 0, and lists can be sliced, concatenated and so on:
+
同字符串索引一样,列表索引也是从 0 开始,列表也可以被分切,被串联,等等:
  
 
  >>> a[0]
 
  >>> a[0]
第352行: 第356行:
 
  ['spam', 'eggs', 100, 'spam', 'eggs', 100, 'spam', 'eggs', 100, '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 的一个拷贝:
  
 
  >>> a[:]
 
  >>> a[:]
 
  ['spam', 'eggs', 100, 1234]
 
  ['spam', 'eggs', 100, 1234]
  
Unlike strings, which are immutable, it is possible to change individual elements of a list:
+
与字符串的一成不变有所不同的是,列表可以改变其中的个别元素:
  
 
  >>> a
 
  >>> a
第365行: 第369行:
 
  ['spam', 'eggs', 123, 1234]
 
  ['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[0:2] = [1, 12]
 
  >>> a
 
  >>> a
 
  [1, 12, 123, 1234]
 
  [1, 12, 123, 1234]
  >>> # Remove some:
+
  >>> # 删除一些:
 
  ... a[0:2] = []
 
  ... a[0:2] = []
 
  >>> a
 
  >>> a
 
  [123, 1234]
 
  [123, 1234]
  >>> # Insert some:
+
  >>> # 插入一些:
 
  ... a[1:1] = ['bletch', 'xyzzy']
 
  ... a[1:1] = ['bletch', 'xyzzy']
 
  >>> a
 
  >>> a
 
  [123, 'bletch', 'xyzzy', 1234]
 
  [123, 'bletch', 'xyzzy', 1234]
  >>> # Insert (a copy of) itself at the beginning
+
  >>> # 在开头插入它本身(一个副本)
 
  >>> a[:0] = a
 
  >>> a[:0] = a
 
  >>> a
 
  >>> a
 
  [123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
 
  [123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
  >>> # Clear the list: replace all items with an empty list
+
  >>> # 清除列表:把所有项目替换为空列表
 
  >>> a[:] = []
 
  >>> a[:] = []
 
  >>> a
 
  >>> a
 
  []
 
  []
  
The built-in function len() also applies to lists:
+
内置函数 len() 也能应用于列表:
  
 
  >>> a = ['a', 'b', 'c', 'd']
 
  >>> a = ['a', 'b', 'c', 'd']
第394行: 第398行:
 
  4
 
  4
  
It is possible to nest lists (create lists containing other lists), for example:
+
也可以使用嵌套列表(在列表里创建其它列表),例如:
  
 
  >>> q = [2, 3]
 
  >>> q = [2, 3]
第405行: 第409行:
 
  2
 
  2
  
You can add something to the end of the list:
+
您可以在列表的末尾添加项目:
  
 
  >>> p[1].append('xtra')
 
  >>> p[1].append('xtra')
第413行: 第417行:
 
  [2, 3, 'xtra']
 
  [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.
+
注意,在最后一个例子里,p[1] q 实际上指向同一个对象!稍后我们将回到对象语法中继续讨论。
  
==— 3.2. First Steps Towards Programming==
+
==— 3.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:
+
当然,我们能使用Python完成比 2+2 更复杂的工作。在下例里,我们能写出一个初步的斐波纳契数列如下:
  
  >>> # Fibonacci series:
+
  >>> # Fibonacci series: 斐波纳契数列
  ... # the sum of two elements defines the next
+
  ... # 两个元素的总和确定了下一个数
 
  ... a, b = 0, 1
 
  ... a, b = 0, 1
 
  >>> while b < 10:
 
  >>> while b < 10:
第433行: 第437行:
 
  8
 
  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.
+
*第一行包含了一个复合赋值:变量 a b 同时得到新值 0 和 1。最后一行再次使用了同样的方法,可以看到,右边的表达式会在赋值变动之前执行。右边表达式的执行顺序是从左往右的。
  
*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).
+
*只要条件(这里是: b < 10 )为真,while循环将一直执行下去。在 Python 里,和C语言中一样,任何非零整数为真;零为假。限定条件可以是一个字符串或是一个列表,事实上可以是任何序列;任何非零长度的值都是真,空序列是假。在例子中用于测试的是一个简单的比较。标准的比较操作写法和C语言一样: < (小于), > (大于), == (等于), <= (小于等于), >= (大于等于)和 != (不等于)。
  
*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.
+
*书写循环体时需要缩进:缩进是Python组合语句的主要方法。Python(暂)没有提供一个智能输入行的编辑功能,因此您必须在每一个缩进行键入一个制表符或空格符。在实践中您将会乐于使用一个文本编辑器,用以编写更复杂的Python程序;许多文本编辑器有自动缩进功能。当使用解释器交互输入一个复合语句时,语句块必须紧接一个空行表示该语句块结束(因为解释器不会知道您什么时候才会输入最后一行)。注意,同一语句块内的每一行都必须有相同的缩进量。
  
*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:
+
*print() 函数输出表达式被赋予的值。它在处理复合表达式、浮点数和字符串方面,不同于仅仅输出的您想写出的表达式(就像我们较早前计算器的例子)。字符串的输出没有引号,各项之间插入空格,所以您可以很好地格式化输出,就像这样:
  
 
  >>> i = 256*256
 
  >>> i = 256*256
第447行: 第451行:
 
  The value of i is 65536
 
  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:
+
关键字end可以被用于防止输出新的一行,或者在输出的末尾添加不同的字符:
  
 
  >>> a, b = 0, 1
 
  >>> a, b = 0, 1
第456行: 第460行:
 
  1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,
 
  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_官方简明教程|返回《 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 手册 》总目录 ——