Ale*_*lex 2 python operators bitwise-operators boolean-operations
在Python 中使用单词and与&符号的逻辑或性能有什么不同吗?
and是一个布尔运算符.它将两个参数视为布尔值,如果它是假的则返回第一个,否则返回第二个.注意,如果第一个是假的,那么第二个参数甚至根本不计算,这对于避免副作用很重要.
例子:
False and True --> FalseTrue and True --> True1 and 2 --> 2False and None.explode() --> False (没有例外)& 有两种行为.
int,那么它计算两个数字的按位AND,返回一个int.如果一个是int一个bool,那么该bool值被强制转换为int(为0或1)并且适用相同的逻辑.bool,那么将评估两个参数并bool返回a.TypeError引发a(例如float & float,等).例子:
1 & 2 --> 01 & True --> 1 & 1 --> 1True & 2 --> 1 & 2 --> 0True & True --> TrueFalse & None.explode() --> AttributeError: 'NoneType' object has no attribute 'explode'| 归档时间: |
|
| 查看次数: |
245 次 |
| 最近记录: |