0

I use python 2.7.12 on Ubuntu 16.04, I have this in some part of my code:

for i in np.arange(0,max+1):
    ...
    if i != 1 and i != max :
            t_try[i] = (C_[i])/(2.0*D)

but I get different results with this changes:

for i in np.arange(0,max+1):
    ...
    if (i != 1) and (i != max) :
            t_try[i] = (C_[i])/(2.0*D)

or

for i in np.arange(0,max+1):
    ...
    if (i != 1 and i != max) :
            t_try[i] = (C_[i])/(2.0*D)

I failed to see what is the problem.

update: please note I'm not talking about "Boolean operators" and "Bitwise operators" accepted answer is @fernand 's answer, BTW thanks for you valuable time

Eugene Lisitsky
  • 12,113
  • 5
  • 38
  • 59
Mr. Who
  • 157
  • 3
  • 14
  • 1
    Can you explain what you try to achieve and what is the expected output? Do you need a logical and (`and`) or a bitwise `&` (which might be the same in your case since it seems you are doing bitwise on boolean...)? The bitwise `&` has higher priority than `!=` and `and` which means it is applied first - based on http://www.mathcs.emory.edu/~valerie/courses/fall10/155/resources/op_precedence.html – urban Nov 24 '17 at 10:32

2 Answers2

0

This is for the precedence of operators: https://docs.python.org/2/reference/expressions.html or, and are prevalent over ==, =!

fernand0
  • 310
  • 1
  • 10
0

In python, & is not a logical conjunction, but rather a bit-wise AND operator. Try changing & to and in your examples.

Peter Abolins
  • 1,520
  • 1
  • 11
  • 18