数值
Python的常用数值类型有整型“int”, 浮点型“float”,此外Python还支持诸如复数等其他数值类型。
常见的“+”, “-”, “* ”, “/”, "%"等操作符,在Python也都被支持。
>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5*6) / 4
5.0
>>> 8 / 5 # division always returns a floating point number
1.6
特别之处为”/“在Python里总是返回浮点数,如果需要返回整数的除法,操作符为”//“
>>> 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
”**“表示乘方
>>> 5 ** 2 # 5 squared
25
>>> 2 ** 7 # 2 to the power of 7
128
不同的数值类型共同计算的时候,int会被转换为float
>>> 3 * 3.75 / 1.5
7.5
>>> 7.0 / 2
3.5
字符串
字符串可以用单引号 (’…’) 定义,也可以用双引号定义("…")
>>> '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," he said.'
'"Yes," he said.'
>>> "\"Yes,\" he said."
'"Yes," he said.'
>>> '"Isn\'t," she said.'
'"Isn\'t," she said.'
\
用于在字符串中指明转义的语意,通过字符串文本中本身有的引号类型使用不同的引号声明字符串可以减少和避免使用转义。
交互模式中,字符串显示时会被引号包围,并且原样显示特殊字符,但是如果引号转义的文本中不包括另一种引号,则该引号转义不会被原样显示,而是显示转义后的结果。 如果字符串中不含有双引号,却包含单引号,则将以双引号显示字符串,否则反之。
“print()”函数提供了更加可读的字符串显示功能,省略了包围字符串的引号,同时正确的打印出转义字符
>>> '"Isn\'t," she said.'
'"Isn\'t," she said.'
>>> print('"Isn\'t," she said.')
"Isn't," she 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
跨行的字符串通过三个引号 ("""…""" 或 ‘’’…’’’)声明,默认情况下,EOL被自动添加到书写的每行的行尾,通过在行的末尾附加“\”可以取消默认动作
>>>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
输出的内容中将不存在头部的空行
“+”可用于连接字符串, “*”用于重复字符串
>>> # 3 times 'un', followed by 'ium'
>>> 3 * 'un' + 'ium''unununium'
互相接近的多个字符串字面量会自动被连接
>>> 'Py' 'thon'
'Python'
字符串支持下标访问,下标和C一样,从0开始
>>> word = 'Python'
>>> word[0] # character in position 0
'P'
>>> word[5] # character in position 5
'n'
负索引的下标也被支持,从字符串的末尾开始计算,起始值为“-1”
>>> word[-1] # last character
'n'
>>> word[-2] # second-last character
'o'
>>> word[-6]
'P'
Python中不存在独立的字符类型,下标访问字符串的结果是一个只有一个字符的字符串。
字符串支持切片,切片返回部分或整个字符串,开始的索引包括在返回的字符串里,结束的索引则不包含
>>> 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'
字符串为不可变类型,切片总是返回一个新的字符串
列表
列表属于复合类型, 可以存放有序,有重复的一系列元素,元素的类型可以完全相同,也可以不同,不过一般一个列表中的元素类型都是相同的
>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]
列表支持下标访问和切片
>>> squares[0] # indexing returns the item
1
>>> squares[-1]
25
>>> squares[-3:] # slicing returns a new list
[9, 16, 25]
列表的切片总是返回一个新的列表,所以下面的切片返回的列表将不是squares
>>> squares[:]
[1, 4, 9, 16, 25]
用”+“实现列表连接
>>> squares + [36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
列表是可变类型,可以通过下标修改列表中的单个元素,或者通过赋值到切片修改一段元素
>>> 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]
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> letters
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> # replace some values
>>> letters[2:5] = ['C', 'D', 'E']
>>> letters
['a', 'b', 'C', 'D', 'E', 'f', 'g']
>>> # now remove them
>>> letters[2:5] = []
>>> letters
['a', 'b', 'f', 'g']
>>> # clear the list by replacing all the elements with an empty list
>>> letters[:] = []
>>> letters
[]
列表支持嵌套,即多维列表
>>> a = ['a', 'b', 'c']
>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0]
['a', 'b', 'c']
>>> x[0][1]
'b'
序列
列表和字符串都是序列类型,所以可以看到他们有许多相同的操作被支持
内建函数’len()'可以被用于求一个序列的长度
>>> letters = ['a', 'b', 'c', 'd']
>>> len(letters)
4
>>> s = 'supercalifragilisticexpialidocious'
>>> len(s)
34
注释
Python用符号“#”表示注释,从“#”开始,到所在行结束,其间的内容会被解释器忽略, “#”可以出现在行首,也可以出现在其他字符之后
# this is the first comment
spam = 1 # and this is the second comment
# ... and now a third!
text = "# This is not a comment because it's inside quotes."
通过三个引号 ("""…""" 或 ‘’’…’’’)声明的内容出现于类,函数实现的起始位置,则为文档字符串,可以看作是多行注释。