numerics:
整形 int,用c语言中的long实现, 取值范围-sys.maxint-1~sys.maxin, 无sys.minint
长整形 long, 带有L/l的integer或超出integer范围的,print时会带后缀L,无精度限制,无限大,因此Python中都是有符号数,没有unsigned类型
浮点型 float,用c中的double实现,sys.float_info, 因此Python中无单双精度区分
复数 complex, z.real, z.imag
Operation |
Result |
Notes |
x + y |
sum of x and y |
|
x - y |
difference of x and y |
|
x * y |
product of x and y |
|
x / y |
quotient of x and y |
(1) |
x // y |
(floored) quotient of x and y |
(4)(5) |
x % y |
remainder of x / y |
(4) |
-x |
x negated |
|
+x |
x unchanged |
|
abs(x) |
absolute value or magnitude of x |
(3) |
int(x) |
x converted to integer |
(2) |
long(x) |
x converted to long integer |
(2) |
float(x) |
x converted to floating point |
(6) |
complex(re,im) |
a complex number with real part re, imaginary part im. im defaults to zero. |
|
c.conjugate() |
conjugate of the complex number c. (Identity on real numbers) |
|
divmod(x, y) |
the pair (x // y, x % y) |
(3)(4) |
pow(x, y) |
x to the power y |
(3)(7) |
x ** y |
x to the power y |
(7) |
不同类型的numerics可以混合运算,遵循规则类似c,也即小范围向大范围转型,int<long<float<complex
整除/ : 结果总是整数,哪怕除数、被除数不是整数,而且结果总是趋向负无穷大,-1/2=-1
0的0次幂:pow(0,0) =1, 0**0=1
NaN: not a number , INF:无穷大,-inf +inf , float(‘nan‘) float(‘+inf‘) float(‘-inf‘)
int(), long() 都是向下转型,对应实数int long float还可以用以下方式取舍:
Operation |
Result |
Notes |
math.trunc(x) |
x truncated to Integral |
|
round(x[, n]) |
x rounded to n digits, rounding ties away from zero. If n is omitted, it defaults to 0. |
四舍五入 |
math.floor(x) |
the greatest integral float <= x |
|
math.ceil(x) |
the least integral float >= x |
|
bool布尔:用于if/while后做条件判断
True:非False即为True
False: None, False, 数字类型0,空容器,包括空字符串‘’, class的__nonzero__() 或__len__返回0或False的实例
bool运算符:or and not, 遵循类似java/c的short-circuit, not比non-Boolean operator优先级低,not a==b 等价于not (a==b)
比较运算符: 也用于所有类型的比较,优先级比Boolean operator高,且支持x<y<z这样的写法,x<y<z 等价x<y and y < z 且前者y仅计算一次,都遵循短路原则;不同类型的对象比较结果都是False,除非是不同类型数字或字符串比较,比如0==0L,
‘abc’==u‘abc‘返回True
Operation |
Meaning |
Notes |
< |
strictly less than |
|
<= |
less than or equal |
|
> |
strictly greater than |
|
>= |
greater than or equal |
|
== |
equal |
|
!= 或 <> |
not equal |
(1) |
is |
object identity |
|
is not |
negated object identity |
|
bitwise operation: 位运算只对整数操作有意义,位运算优先级比数字运算符低,但比比较运算符高; ~与其他的一元运算符优先级(+,-)相同,以下表格中优先级从低到高, 负数移位会抛出ValueError异常
Operation |
Result |
Notes |
x | y |
bitwise or of x and y |
|
x ^ y |
bitwise exclusive or of x andy |
|
x & y |
bitwise and of x and y |
|
x << n |
x shifted left by n bits |
(1)(2) |
x >> n |
x shifted right by n bits |
(1)(3) |
~x |
the bits of x inverted |
|
int.bit_length():获取int bit表示长度
long.bit_length():获取long bit表示长度
字符:长度为1的字符串,也即没有单个字符
字符串: 单引号‘abc‘ 或双引号‘‘abc" 或三个连续单/双引号‘‘‘表示多行字符串,字符串可理解为常量字节数组或字节容器,类似Java中String,也不能通过变量改变指向的字符串, s=‘abc‘; id(s) == id(‘abc‘)。
字符串上常用操作:
长度:容器统一用len(),
子串:容器分片操作符[] ‘abcd‘[1:3]=‘bc‘
分隔:split/rsplit
查找/替换:find/rfind 没找到返回-1; index/rindex没找到抛ValueError, replace
trim: strip/lstrip/rstrip
编/解码:只能对str解码 str(‘汉‘).decode(‘UTF-8‘), 只能对Unicode编码 u(‘汉‘).encode(‘UTF-8‘)
大小写转换: lower/uper
判断:isalnum/isalpha/isdigit/islower/isspace/isupper/startwith/endwith
格式化: %+tuple/dict,类似c语言sprintf,一个参数‘%d‘ % 1 = ‘1‘ ; 两个参数‘%s, %s‘ %(‘a‘,‘b‘) = ‘a,b‘; 指
定占位符%(mapping key)+类型字符,mapping
key需加括号‘%(key1)s, %(key2)d‘ %{‘key1‘:‘a‘, ‘key2‘:1}=‘a,1‘
Python中很容易获取帮助:
help(object):显示帮助信息
dir(object) :显示所有方法
object.__doc__ :显示文档