0

I was working in Python 3, I created one If-else statement with the logical operator "&". the result that got was inverse of what actually should have appeared. As: a=20 b=30

if a==b & a==20:
    print("a is equal to b")
else:
    print ("a is not equal to b")

enter image description here

This condition should have printed out the else condition since the first statement "a==b" is a false statement and the second statement "a==20" is true. Mathematical logic says when a statement in "&" condition is false result would be false. The strange thing happened when I replaced the condition "a==b" with "b==a", the result was correct.

enter image description here

Keyur Potdar
  • 7,158
  • 6
  • 25
  • 40
Rahul Kumar
  • 39
  • 1
  • 11
  • 3
    there's a difference between '&' bitwise and "AND" logical – Vj- Apr 11 '18 at 05:26
  • 1
    use "AND" instead of & – Hari Apr 11 '18 at 05:35
  • 2
    Possible duplicate of [Difference between 'and' (boolean) vs. '&' (bitwise) in python. Why difference in behavior with lists vs numpy arrays?](https://stackoverflow.com/questions/22646463/difference-between-and-boolean-vs-bitwise-in-python-why-difference-i) – Keyur Potdar Apr 11 '18 at 05:36
  • I did not ask the difference between "&" and "and"(Bitwise or Binary operator) but what I wanted to know is that why "&" is giving this result even if its a binary operator. Please, someone, explain to me how come result is changing when I replace values of "a" and "b" – Rahul Kumar Apr 11 '18 at 06:12
  • @RahulKumar, can you [edit] your question to add this (important) information? Looking at the question, it looks like you are using `&` instead of `and`. Change the question accordingly, and I'll retract my close vote. – Keyur Potdar Apr 11 '18 at 08:05

2 Answers2

4

Answer to your first question. Python & operator will be executed first than == operator(due to higher precedence)

Answer to your second question.

if a==b & a==20:

When you executed this expression internally this is what happened.

if a==(b&a)==20:

The expression (b&a) will give you the answer 20. So the expression is like this now.

if a==(20)==20:  # which is nothing but if a==20 and 20==20:

Since a = 20,the expression becomes true and you get the if part executed. But when you interchanged a and be this is what actually happened.

if b==(a&a)==20:

a&a again will give you 20. So the expression becomes

if b==(20)==20:    # if b==20 and 20==20:

Now b is not 20,its 30. So expression becomes False and else part gets executed.

Mufeed
  • 3,018
  • 4
  • 20
  • 29
2

In Python '&' has higher precedence than '==' so We are getting the wrong result Try this:

if (a==b) & (a==20):
    print "a is equal to b"
else:
    print "a is not equal to b"
himabindu
  • 336
  • 3
  • 15
  • But why the result changes when I put "b==a"? – Rahul Kumar Apr 11 '18 at 06:26
  • 1
    if a == b & a==20 then the first acomputation will be b & a(11110 & 10100 = 10100) 20. the if condition cheks a == 20 == 20 so the condition becomes true. if b == a & a==20 then the first action will be a & a( 10100 & 10100 = 10100). the if condition checks b == 20 == 20 so the condition becomes false – himabindu Apr 12 '18 at 03:57