[Python] In python 3 logical operator giving me an opposite result

Discussão em 'Python' iniciado por Stack, Junho 14, 2025.

  1. Stack

    Stack Membro Participativo

    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")


    [​IMG]

    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.

    [​IMG]

    Continue reading...

Compartilhe esta Página