Why doesn't the "and" condition work in my Python codes?

Question:

Each separate statement is true, but why is the combination of the two with "and" false?
codes

Asked By: ZHONGLI WANG

||

Answers:

In python and is a logical AND that returns True if both the operands are true whereas ‘&’ is a bitwise operator. I think you are trying to to something like this.

if (other_list[0] == 0 and rand_vec[0] == 1):
    # Do something
Answered By: Pw Wolf

Many thanks to Pw Wolf and all others! Now I understand it.

Original code:

if (other_list[0] == 0 & rand_vec[0] == 1):

    # do something 

Improved code:

if (other_list[0] == 0 and rand_vec[0] == 1):

    # do something 
Answered By: ZHONGLI WANG
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.