数字
- 除法运算 (/) 返回float类型
>>> 8 / 5
1.6
>>> 8 // 5
1
>>> 7 / 5
1.4
>>> 7 // 5
1
>>> 8 % 5
3
- ** 运算符计算乘方
>>> 5 ** 2
25
>>> 2 ** 7
128
# **优先级高于-, 下面等于-(3**2)
>>> -3**2
-9
- =赋值
>>> width = 20
>>> height = 5 * 9
>>> width * height
900
- 包含float数的混合运算,会把int转换为float
>>> 4 * 3.75 - 1
14.0
- 交互模式下,_保存计算结果
>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2) # 保留两位小数
113.06
字符串
>>> 'spam eggs' # 单引号
'spam eggs'
>>> 'doesn\'t' # 用\转义'
"doesn't"
>>> "doesn't" # 用双引号包裹单引号
"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.'
>>> s
'First line.\nSecond line.'
>>> print(s)
First line.
Second line.
print输出不转义,输出原始字符串
>>> print('C:\some\some')
C:\some\some
>>> print(r'C:\some\name')
C:\some\name
>>>
- 字符串跨行连续输出 “”" “”"或’’’ ‘’’, \是去除自动包含的回车
>>> 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 * 'un' + 'ium'
'unununium'
>>> 'Py' 'thon'
'Python'
>>>
>>> text = ('Put serveral strings within parentheses '
... 'to have them joined together.')
>>> text
'Put serveral strings within parentheses to have them joined together.'
>>>
# 只对字面值有效,变量或表达式无效
>>> prefix = 'Py'
>>> prefix 'thon'
File "<stdin>", line 1
prefix 'thon'
^
SyntaxError: invalid syntax
>>>
>>> ('un' * 3) 'ium'
File "<stdin>", line 1
('un' * 3) 'ium'
^
SyntaxError: invalid syntax
>>>
# +连接变量
>>> prefix + 'thon'
'Python'
>>>
- 用下标访问字符串
>>> word = 'Python'
>>> word[0]
'P'
>>> word[5]
'n'
>>>
# 负数索引,从右边-1开始
>>> word[-1]
'n'
>>> word[-2]
'o'
>>> word[-6]
'P'
>>>
- 切片,左闭右开
>>> word = 'python'
>>> 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:]
''
- python字符串不可修改, immutable
>>> word[0] = ']'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> word[2:] = 'py'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>>
# 新的字符串
>>> 'J' + word[1:]
'Jython'
>>> word[:2] + 'py'
'pypy'
>>>
- len(s)返回字符串长度
>>> s = 'sdfdslfdslfsfeweiwpjdsfldsfldsflsdjflsfw'
>>> len(s)
40
>>>
列表
>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]
>>> squares[0]
1
>>> squares[-1]
25
>>> squares[-3:]
[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]
- 列表可修改,mutable
>>> cubes = [1, 8, 27, 65, 125]
>>> 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)
- 列表嵌套
>>> 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'
编程
# Fibonacci series:
>>> a, b = 0, 1
>>> while a < 10:
... print(a)
... a, b = b, a+b
...
0
1
1
2
3
5
8
>>> print('The value of i is', i)
The value of i is 65536
# end在每个输出后面加,
>>> a , b = 0, 1
>>> while a < 1000:
... print(a, end=',')
... a, b = b, a+b
...
0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,>>>
- ** 优先 - , -32 被解释 -(32)
- 特殊字符比如 \n 在单引号 (’…’) 和双引号 ("…") 里意义一样. 不需在单引号里转义双引号 " (但必须把单引号转义成 ') , 反之亦然