1.数字部分
运算符 +, -, *, /, %, ()
, 与其他编程语言一致
>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5*6) / 4
5.0
>>> 8 / 5
1.6
需要说的一点, /
运算结果是一个浮点数.
如果需要除法运算结果为整数(舍弃小数部分), 则需要使用 //
运算符(吐槽: 不得不说一下这个符号真的会让我误以为是注释..)
>>> 17 / 3
5.666666666666667
>>>
>>> 17 // 3
5
>>> 17 % 3
2
>>> 5 * 3 + 2
17
使用 **
运算符可以计算幂乘方
>>> 5 ** 2
25
>>> 2 ** 7
128
赋值符号依旧是 =
.
>>> width = 20
>>> height = 5*9
>>> width * height
900
最近的一个运算结果给赋值给一个变量 _
, 这个变量是只读的.
>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06
复数使用后缀 j
或者 J
表示虚数部分(例如,3+5j
)
2.字符串
字符串可以使用双引号 "..."
或者 '...'
来表示
转义字符依旧是反斜杠 \
>>> '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()
函数
>>> '"Isn\'t," she said.'
'"Isn\'t," she said.'
>>> print('"Isn\'t," she said.')
"Isn't," she said.
>>> s = 'First line.\nSecond line.'
>>> s
'First line.\nSecond line.'
>>> print(s)
First line.
Second line.
当打印的字符串中有转义字符 \
而你又需要在打印结果中显示这个字符的时候, 可以在第一个引号之前加一个 r
字符, 表示使用原始字符串
>>> print('C:\some\name')
C:\some
ame
>>> print(r'C:\some\name')
C:\some\name
多行打印使用三引号 """..."""
或者 '''...'''
, 多行换行时, 行尾的换行符会被包含到字符串中, 可以在行尾加上 \
,表示下一行是当前行的后续内容, 不进行换行.
print("""\
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
""")
字符串拼接, 可以使用 +
. 使用 *
表示字符串重复.
>>> 3 * 'un' + 'ium'
'unununium'
还可以使用空格来拼接一个相邻的字符串, 但是这个空格左右不能是字符串变量, 只能是字符串文本.
>>> 'Py' 'thon'
'Python'
切分长字符串.
>>> text = ('Put several strings within parentheses '
'to have them joined together.')
>>> text
'Put several strings within parentheses to have them joined together.'
字符串索引, 类似 C, 可以用数组索引的方式获取字符串中的字符, 索引值为负数时表示从右往左计算. 越界报错
>>> word = 'Python'
>>> word[0]
'P'
>>> word[5]
'n'
>>> word[-1]
'n'
>>> word[-2]
'o'
>>> word[-6]
'P'
使用切片获取一个子字符串, 截取结果包左不包右. 越界将被字符串长度代替.
>>> word[0:2]
'Py'
>>> word[2:5]
'tho'
>>> word[:2] + word[2:]
'Python'
>>> word[:4] + word[4:]
'Python'
>>> word[:2]
'Py'
>>> word[4:]
'on'
>>> word[-2:]
'on'
>>> word[4:42]
'on'
>>> word[42:]
''
不可对索引获得的字符或者子字符串赋值, 赋值时将报错.
>>> word[0] = 'J'
...
TypeError: 'str' object does not support item assignment
>>> word[2:] = 'py'
...
TypeError: 'str' object does not support item assignment
切片时的索引是在两个字符之间
+--+--+--+--+--+--+
| P | y | t | h | o | n |
+--+--+--+--+--+--+
0 1 2 3 4 5 6
-6 -5 -4 -3 -2 -1
3. 列表 list
与字符串一样, 列表同样可以进行索引和切片.
>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]
>>> squares[0]
1
>>> squares[-1]
25
>>> squares[-3:]
[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]
与字符串索引或切片不一样, 列表索引或切片的结果是可以进行赋值操作的.
>>> cubes = [1, 8, 27, 65, 125]
>>> 4 ** 3
64
>>> cubes[3] = 64
>>> cubes
[1, 8, 27, 64, 125]
使用 append()
函数可以在列表末尾添加一个新的元素.
>>> cubes.append(216)
>>> cubes.append(7 ** 3)
>>> cubes
[1, 8, 27, 64, 125, 216, 343]
对切片赋值可以改变列表尺寸, 或清空列表.
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> letters
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> letters[2:5] = ['C', 'D', 'E']
>>> letters
['a', 'b', 'C', 'D', 'E', 'f', 'g']
>>> letters[2:5] = []
>>> letters
['a', 'b', 'f', 'g']
>>> letters[:] = []
>>> letters
[]
用 len()
函数可以获取列表的尺寸.
>>> letters = ['a', 'b', 'c', 'd']
>>> len(letters)
4
嵌套列表 (类似多维数组了)
>>> 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'
4. 菲波那契子序列的程序:
>>> 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,>>>
使用 print()
函数时, 用一个符号替换结尾 end
参数可以取消掉默认的换行输出.
与 C 类似, 任何非零整数都是 true, 0 为 false, 条件也可以是任何序列, 如字符串和列表等等, 当长度为 0 时为 false, 长度不为 0 则为 true.
标准比较操作符 < , > , == , <=, >= 和 !=
。