和 python 2.7 上的逻辑运算
and logical operation on python 2.7
我在 Ubuntu 16.04 上使用 python 2.7.12,我的部分代码中有这个:
for i in np.arange(0,max+1):
...
if i != 1 and i != max :
t_try[i] = (C_[i])/(2.0*D)
但是我得到了不同的结果:
for i in np.arange(0,max+1):
...
if (i != 1) and (i != max) :
t_try[i] = (C_[i])/(2.0*D)
或
for i in np.arange(0,max+1):
...
if (i != 1 and i != max) :
t_try[i] = (C_[i])/(2.0*D)
我没看出来是什么问题。
更新: 请注意我不是在谈论 "Boolean operators" 和 "Bitwise operators" 接受的答案是@fernand 的答案,顺便说一句谢谢你的宝贵意见时间
这是为了运算符的优先级:
https://docs.python.org/2/reference/expressions.html
或者,并且普遍超过 ==、=!
在python
中,&
不是逻辑合取,而是位与运算符。尝试将示例中的 &
更改为 and
。
我在 Ubuntu 16.04 上使用 python 2.7.12,我的部分代码中有这个:
for i in np.arange(0,max+1):
...
if i != 1 and i != max :
t_try[i] = (C_[i])/(2.0*D)
但是我得到了不同的结果:
for i in np.arange(0,max+1):
...
if (i != 1) and (i != max) :
t_try[i] = (C_[i])/(2.0*D)
或
for i in np.arange(0,max+1):
...
if (i != 1 and i != max) :
t_try[i] = (C_[i])/(2.0*D)
我没看出来是什么问题。
更新: 请注意我不是在谈论 "Boolean operators" 和 "Bitwise operators" 接受的答案是@fernand 的答案,顺便说一句谢谢你的宝贵意见时间
这是为了运算符的优先级: https://docs.python.org/2/reference/expressions.html 或者,并且普遍超过 ==、=!
在python
中,&
不是逻辑合取,而是位与运算符。尝试将示例中的 &
更改为 and
。