|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
>>> # 位运算: << >>> & ^ | ~ 二进制按位运算
>>> # bin()函数将数字转换位二进制,0b开头
>>> print([bin(i) for i in range(10)])
['0b0', '0b1', '0b10', '0b11', '0b100', '0b101', '0b110', '0b111', '0b1000', '0b1001']
>>> # 按位与
>>> print(3 & 4)
0
>>> # 按位或
>>> print(3 | 2)
3
>>> # 按位非,涉及补码
>>> print(~2)
-3
>>> # 按异或
>>> print(3 ^ 2)
1
>>> # 右移,移动位数不能是负数
>>> print(8 >> 2)
2
>>> # 左移,移动位数不能是负数
>>> print(8 << 2)
32
>>> # math 数学函数
>>> import math
>>> print(0.1 + 0.2) == 0.3 + math.ulp(0.3)
0.30000000000000004
False
>>> # __index__(self)用法:让对象作为索引值被访问才会触发
>>> class C:
... def __index__(self):
... print("被拦截了~~")
... return 3
...
>>> c = C()
>>> try:
... print(c[2])
... except TypeError as e:
... print(e)
... # 'C' object is not subscriptable
...
'C' object is not subscriptable
>>> s = 'fishc'
>>> print(s[c])
被拦截了~~
h
>>> print(s[bin(c)])
被拦截了~~
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: string indices must be integers
>>> |
|