0

Below are the different scenarios tried using '&' and 'and' conditional operators and its result. (using Python 2.7)

  1. Using '&' operator:

enter image description here

  1. Using 'and' operator:

enter image description here

Wondering why both conditional operators showing different behaviour? Explanation with real scenarios would be helpful.

Thanks in advance.

Sayse
  • 42,633
  • 14
  • 77
  • 146
  • 2
    Please post *text* as text – Sayse Nov 23 '17 at 13:17
  • Please see [Why may I not upload images of code on SO when asking a question?](http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-question) – PM 2Ring Nov 23 '17 at 13:22
  • Also see https://stackoverflow.com/questions/36550588/assigning-string-with-boolean-expression – PM 2Ring Nov 23 '17 at 13:28

4 Answers4

5

& is not a conditional operator. It stands for the bitwise and. Not only it is a different operator but also the operator precedence is different (and is below > while & is above).

So first of all an example:

>>> 1 and 2
2
>>> 1 & 2
0

Now lets analyze your case:

>>> point = 1
>>> score = 2
>>> point == 1 & score > 0

Now the operator precedence kicks in and the last line is equivalent to

>>> point == (1 & score) > 0

Note that == and > have equivalent precedence. So lets evaluate that:

>>> 1 & score
0
>>> point == 0 > 0

The last line is equivalent to (point == 0) > 0 (when operators have equal precedence then you simply go from left to right). Lets evaulate that:

>>> point == 0
False
>>> False > 0
False

All in all

>>> point == 1 & score > 0
False

Can you break down the evaluation for your second statement now?

freakish
  • 54,167
  • 9
  • 132
  • 169
  • Can you please elaborate the answer. if we do like (point == 1 & (score > 0)) => return true. – Prashantkumar K B Nov 23 '17 at 13:30
  • 1
    @PrashantkumarKB I've updated the answer. I'm not sure what you are trying to do but unless you are going to manipulate bits then don't use `&`. – freakish Nov 23 '17 at 13:36
1

The issue is that & is not actually a logical "and", it's a bitwise operator. So it'll compare two numbers by each bit, and produce a number that has a bit set if both of the first two numbers had that bit set. So 1 & 2 will give you 0.

This wouldn't usually be a problem (True is 1, False is 0) - except that the operator precedence for & and and relative to > and = are different.

So 1 == 1 & 2 > 0 (the first case):

  • This is interpreted as 1 == (1 & 2) > 0, or 1 == 0 > 0, and since 1 == 0 is False the whole thing is False.

Whereas 1 == 1 and 2 > 0 (the second case):

  • This is interpreted as (1 == 1) and (2 > 0), and since both of those cases are True, the whole thing is True.
bouteillebleu
  • 2,456
  • 23
  • 32
0

and tests whether both expressions are logically True while & (when used with True/False values) tests if both are True.

sachin dubey
  • 755
  • 9
  • 28
0

& is a bitwise AND operator, whereas and is a logical operator.