Numbers
int
整数类型 如 : (2 , 4 , 20)
float
浮点类型 如 : (5.0 , 1.6)
>>> 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
**
: 计算平方数
>>> 5 ** 2 #5
25
>>> 2 ** 7 #2的7次方
128
=
: 用于赋值给一个变量 .
>>> width = 20
>>> height = 5 * 9
>>> width * height
900
混合运算会将结果返回为浮点型
>>> 4 * 3.75 - 1
14.0
Strings
字符串可以使用单引号('...')和双引号("...")表示
\
: 表示单个引号的('
)转义符 , 或者使用 双引号("...")
>>> '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.'
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
>>>""")
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'
网友评论