数字
解释器就像一个简单的计算器一样:你可以在里面输入一个表达式然后它会写出答案。 表达式的语法很直接:运算符 +、-、*、/ 的用法和其他大部分语言一样(比如 Pascal 或者 C 语言);括号 (()) 用来分组。比如:
>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5*6) / 4
5.0
>>> 8 / 5 # division always returns a floating point number
1.6
整数(比如 2、4、20 )的类型是 int,有小数部分的(比如 5.0、1.6 )的类型是 float。 在这个手册的后半部分我们会看到更多的数字类型。
除法运算 (/) 永远返回浮点数类型。如果要做 floor division 得到一个整数结果(忽略小数部分)你可以使用 // 运算符;如果要计算余数,可以使用 %
>>> 17 / 3 # classic division returns a float
5.666666666666667
>>>
>>> 17 // 3 # floor division discards the fractional part
5
>>> 17 % 3 # the % operator returns the remainder of the division
2
>>> 5 * 3 + 2 # result * divisor + remainder
17
在Python中,可以使用 ** 运算符来计算乘方
>>> 5 ** 2 # 5 squared
25
>>> 2 ** 7 # 2 to the power of 7
128
等号 (=) 用于给一个变量赋值。然后在下一个交互提示符之前不会有结果显示出来:
>>> width = 20
>>> height = 5 * 9
>>> width * height
900
字符串
除了数字,Python 也可以操作字符串。字符串有多种形式,可以使用单引号(’……’),双引号("……")都可以获得同样的结果 2。反斜杠 \ 可以用来转义:
>>> 'spam eggs' # single quotes
'spam eggs'
>>> 'doesn\'t' # use \' to escape the single quote...
"doesn't"
>>> "doesn't" # ...or use double quotes instead
"doesn't"
>>> '"Yes," they said.'
'"Yes," they said.'
>>> "\"Yes,\" they said."
'"Yes," they said.'
>>> '"Isn\'t," they said.'
'"Isn\'t," they said.'
在交互式解释器中,输出的字符串外面会加上引号,特殊字符会使用反斜杠来转义。 虽然有时这看起来会与输入不一样(外面所加的引号可能会改变),但两个字符串是相同的。 如果字符串中有单引号而没有双引号,该字符串外将加双引号来表示,否则就加单引号。 print() 函数会生成可读性更强的输出,即略去两边的引号,并且打印出经过转义的特殊字符:
>>> '"Isn\'t," they said.'
'"Isn\'t," they said.'
>>> print('"Isn\'t," they said.')
"Isn't," they said.
>>> s = 'First line.\nSecond line.' # \n means newline
>>> s # without print(), \n is included in the output
'First line.\nSecond line.'
>>> print(s) # with print(), \n produces a new line
First line.
Second line.
如果你不希望前置了 \ 的字符转义成特殊字符,可以使用 原始字符串 方式,在引号前添加 r 即可:
>>> print('C:\some\name') # here \n means newline!
C:\some
ame
>>> print(r'C:\some\name') # note the r before the quote
C:\some\name
字符串字面值可以跨行连续输入。一种方式是用三重引号:"""…""" 或 ‘’’…’’’。字符串中的回车换行会自动包含到字符串中,如果不想包含,在行尾添加一个 \ 即可。如下例:
print("""\
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
""")
字符串可以用 + 进行连接(粘到一起),也可以用 * 进行重复:
>>> # 3 times 'un', followed by 'ium'
>>> 3 * 'un' + 'ium'
'unununium'
只能对两个字面值这样操作,变量或表达式不行:
>>> prefix = 'Py'
>>> prefix 'thon' # can't concatenate a variable and a string literal
File "<stdin>", line 1
prefix 'thon'
^
SyntaxError: invalid syntax
>>> ('un' * 3) 'ium'
File "<stdin>", line 1
('un' * 3) 'ium'
^
SyntaxError: invalid syntax
索引也可以用负数,这种会从右边开始数:
>>> word[-1] # last character
'n'
>>> word[-2] # second-last character
'o'
>>> word[-6]
'P'
注意 -0 和 0 是一样的,所以负数索引从 -1 开始。
除了索引,字符串还支持 切片。索引可以得到单个字符,而 切片 可以获取子字符串:
>>> word[0:2] # characters from position 0 (included) to 2 (excluded)
'Py'
>>> word[2:5] # characters from position 2 (included) to 5 (excluded)
'tho'
切片的索引有默认值;省略开始索引时默认为0,省略结束索引时默认为到字符串的结束:
>>> word[:2] # character from the beginning to position 2 (excluded)
'Py'
>>> word[4:] # characters from position 4 (included) to the end
'on'
>>> word[-2:] # characters from the second-last (included) to the end
'on'
您也可以这么理解切片:将索引视作指向字符 之间 ,第一个字符的左侧标为0,最后一个字符的右侧标为 n ,其中 n 是字符串长度。例如:
+---+---+---+---+---+---+
| P | y | t | h | o | n |
+---+---+---+---+---+---+
0 1 2 3 4 5 6
-6 -5 -4 -3 -2 -1
列表
Python 中可以通过组合一些值得到多种 复合 数据类型。其中最常用的 列表 ,可以通过方括号括起、逗号分隔的一组值得到。一个 列表 可以包含不同类型的元素,但通常使用时各个元素类型相同:
>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]
和字符串(以及各种内置的 sequence 类型)一样,列表也支持索引和切片:
>>> squares[0] # indexing returns the item
1
>>> squares[-1]
25
>>> squares[-3:] # slicing returns a new list
[9, 16, 25]
所有的切片操作都返回一个包含所请求元素的新列表。 这意味着以下切片操作会返回列表的一个 浅拷贝:
>>> squares[:]
[1, 4, 9, 16, 25]
列表同样支持拼接操作:
>>> squares + [36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
与 immutable 的字符串不同, 列表是一个 mutable 类型,就是说,它自己的内容可以改变:
>>> cubes = [1, 8, 27, 65, 125] # something's wrong here
>>> 4 ** 3 # the cube of 4 is 64, not 65!
64
>>> cubes[3] = 64 # replace the wrong value
>>> cubes
[1, 8, 27, 64, 125]