1、求绝对值
1 >>> a = -10 2 >>> a.__abs__() 3 10 4 >>> abs(10) 5 10
2、加法
1 >>> a = 1 2 >>> a.__add__(1) 3 2 4 >>> a + 1 5 2
3、按位与
1 >>> a = 1 2 >>> a.__and__(0) 3 0 4 >>> a & 0 5 0
4、求bool值
1 >>> a = 1 2 >>> a.__bool__() 3 True 4 >>> a != 0 5 True
5、返回数字的上入整数
1 >>> int.__ceil__(2) 2 2 3 >>> import math 4 >>> math.ceil(2) 5 2
6、地板除并取模
1 >>> a = 10 2 >>> a.__divmod__(3) 3 (3, 1) 4 >>> divmod(10, 3) 5 (3, 1)
7、是否相等
1 >>> a = 1 2 >>> a.__eq__(1) 3 True 4 >>> a == 1 5 True
8、转float类型
1 >>> a = 1 2 >>> a.__float__() 3 1.0 4 >>> float(a) 5 1.0
9、数字的下舍整数
1 >>> int.__floor__(3) 2 3 3 >>> import math 4 >>> math.floor(3) 5 3
10、地板除
1 >>> a = 10 2 >>> a.__floordiv__(3) 3 3 4 >>> a // 3 5 3
11、数据格式化
1 >>> int.__format__(0, '3') 2 ' 0' 3 >>> int.__format__(0, '2') 4 ' 0' 5 >>> int.__format__(0, '1') 6 '0'
12、大于等于
1 >>> a = 1 2 >>> a.__ge__(0) 3 True 4 >>> a >= 0 5 True
13、获取对象的属性
1 >>> a = 1 2 >>> a.__getattribute__('real') 3 1 4 >>> getattr(a, 'real') 5 1
14、__getnewargs__
15、大于
1 >>> a = 1 2 >>> a.__gt__(0) 3 True 4 >>> a > 0 5 True
16、哈希
1 >>> a = 1 2 >>> a.__hash__() 3 1 4 >>> hash(a) 5 1
17、__index__
1 >>> a = 10 2 >>> a.__index__() 3 10 4 >>> a = [1, 2, 3] 5 >>> a[1] 6 2
18、转换为int
1 >>> a = 1.5 2 >>> a.__int__() 3 1 4 >>> int(a) 5 1
19、取反
1 >>> ~10 2 -11 3 >>> (10).__invert__() 4 -11
python默认的是有符号表示数,10的二进制 0000 1010,取反之后 1111 0101
取反之后,符号位从0变成了1,成了一个负数,而已1作为符号位开头,计算机会将其识别为补码
因为负数在机器中是以补码表示、补码转换真值的方式为补码的数值位,
也就是除符号位之外的各位取反后加1。
1111 0101除符号位取反后的值为1000 1010,再加1,便得到1000 1011,转换成十进制,便是-11
20、小于等于
1 >>> a = 1 2 >>> a.__le__(2) 3 True 4 >>> a <= 2 5 True
21、左移
1 >>> a = 1 2 >>> a.__lshift__(1) 3 2 4 >>> a <<1 5 2
22、小于
1 >>> a = 1 2 >>> a.__lt__(2) 3 True 4 >>> a < 2 5 True
23、取余
1 >>> a = 10 2 >>> a.__mod__(3) 3 1 4 >>> a % 3 5 1
24、乘
1 >>> a = 1 2 >>> a.__mul__(2) 3 2 4 >>> a * 2 5 2
25、不等于
1 >>> a = 1 2 >>> a.__ne__(2) 3 True 4 >>> a != 2 5 True
26、相反数
1 >>> a = 1 2 >>> a.__neg__() 3 -1 4 >>> -a 5 -1
27、按位或
1 >>> a = 1 2 >>> a.__or__(2) 3 3 4 >>> a | 2 5 3
28、加+号
1 >>> +10 2 10 3 >>> (10).__pos__() 4 10
29、x的y次方,取余
1 >>> a = 2 2 >>> a.__pow__(2) 3 4 4 >>> a.__pow__(2, 3) 5 1 6 >>> pow(2, 2) 7 4 8 >>> pow(2, 2, 3) 9 1
30、反向加
1 >>> a = 1 2 >>> a.__radd__(1) 3 2 4 >>> 1 + a 5 2
31、反向与
1 >>> a = 1 2 >>> a.__and__(2) 3 0 4 >>> 2 & a 5 0
32、反向地板除取模
>>> a = 3 >>> a.__rdivmod__(10) (3, 1) >>> divmod(10, 3) (3, 1)
33、转字符串
1 >>> a = 1 2 >>> a.__repr__() 3 '1' 4 >>> repr(a) 5 '1'
34、反向地板除
1 >>> a = 3 2 >>> a.__rfloordiv__(10) 3 3 4 >>> 10 // 3 5 3
35、反向左移
1 >>> a = 1 2 >>> a.__rlshift__(3) 3 6 4 >>> 3 << 1 5 6
36、反向取模
1 >>> a = 3 2 >>> a.__rmod__(10) 3 1 4 >>> 10 % 3 5 1
37、反向乘
1 >>> a = 2 2 >>> a.__mul__(3) 3 6 4 >>> 3 * 2 5 6
38、反向或
1 >>> a = 1 2 >>> a.__ror__(2) 3 3 4 >>> 2 | 1 5 3
39、四舍五入值
1 >>> round(10) 2 10 3 >>> (10).__round__() 4 10
40、反向成方取模
1 >>> a = 3 2 >>> a.__rpow__(2) 3 8 4 >>> a.__rpow__(2, 3) 5 2 6 >>> pow(2, 3) 7 8 8 >>> pow(2, 3, 3) 9 2
41、反向右移
1 >>> a = 1 2 >>> a.__rrshift__(4) 3 2 4 >>> 4 >> 1 5 2
42、反向减法
1 >>> a = 1 2 >>> a.__rsub__(3) 3 2 4 >>> 3 - 1 5 2
43、反向真除
1 >>> a = 4 2 >>> a.__rtruediv__(3) 3 0.75 4 >>> 3 / 4 5 0.75
44、反向异或
1 >>> a = 2 2 >>> a.__xor__(3) 3 1 4 >>> 3 ^ 2 5 1
45、占内存大小,bytes
1 >>> a = 2 2 >>> a.__sizeof__() 3 28
46、转字符串
1 >>> a = 2 2 >>> a.__str__() 3 '2' 4 >>> str(2) 5 '2'
47、减法
1 >>> a = 5 2 >>> a.__sub__(2) 3 3 4 >>> 5 - 2 5 3
48、真除
1 >>> a = 5 2 >>> a.__truediv__(2) 3 2.5 4 >>> 5 / 2 5 2.5
49、__trunc__
截取返回自己
50、异或
1 >>> a = 2 2 >>> a.__xor__(3) 3 1 4 >>> 2 ^ 3 5 1
51、需要的bit位数
1 >>> bin(37) 2 '0b100101' 3 >>> (37).bit_length() 4 6
52、conjugate
转成复数,返回自己
53、from_bytes
int.from_bytes(bytes, byteorder, *, signed=False) -> int 默认是无符号类型
1 >>> int.from_bytes(b'xb3x15x00x00', 'little') 2 5555
54、to_bytes
int.to_bytes(length, byteorder, *, signed=False) -> bytes 默认是无符号类型
length:转换的长度
byteorder:'big' or 'little'
signed:符号位
1 >>> (100).to_bytes(2, 'big', signed=False) 2 b'x00d' 3 >>> (100).to_bytes(2, 'big', signed=True) 4 b'x00d' 5 >>> (-100).to_bytes(2, 'big', signed=False) 6 Traceback (most recent call last): 7 File "<console>", line 1, in <module> 8 OverflowError: can't convert negative int to unsigned 9 >>> (-100).to_bytes(2, 'big', signed=True) 10 b'xffx9c'
1 >>> (5555).to_bytes(2, 'big') 2 b'x15xb3' 3 >>> (5555).to_bytes(2, 'little') 4 b'xb3x15'
1 >>> (5555).to_bytes(2, 'big') 2 b'x15xb3' 3 >>> (5555).to_bytes(4, 'big') 4 b'x00x00x15xb3' 5 >>> (5555).to_bytes(4, 'little') 6 b'xb3x15x00x00'
55、分子部分
1 >>> a = 5 2 >>> a.numerator 3 5
56、分母部分
1 >>> a = 5 2 >>> a.denominator 3 1
57、复数的实部
1 >>> a = 5 2 >>> a.real 3 5
58、复数的虚部
1 >>> a = 5 2 >>> a.imag 3 0