0

Perhaps it is a very basic question, but it doesn't make any sense to me.

If I do:

In: not(False),(not(False))
Out: (True,True)

So, the basic logical operation stands that True Or True is always True.

But if I do:

not(False)|(not(False))
Out: False

If I use parenthesis for the first condition:

In: (not(False))|(not(False))
Out: True

And if I use Or instead of |:

In: not(False)or(not(False))
Out: True

Is it suppose to behave like this? If so, why?

iury simoes-sousa
  • 1,440
  • 3
  • 20
  • 37

2 Answers2

5

not isn't a function; it's a unary operator with lower precedence than |.

not(False)|(not(False)) 
    == not False | (not False)    drop (...) around literal False
    == not (False | (not False)   add (...) implied by operator precedence
    == not (False | True)         evaluate ``not False``
    == not True                   evaluate ``False | True``
    == False                      evaluate ``not True``

But not does have higher precedence than or, which is the operator you should be using.

not(False)or(not(False)) == not False or (not False)
                         == (not False) or (not False)
                         == True or True
                         == True

The documentation provides a full list of the available operators in order of precedence, from lowest to highest.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Thank you. I know it seems a very basic question, but I think it is important to be here at StackOverflow. It can be the same doubt of other users. – iury simoes-sousa Mar 20 '20 at 15:31
1

Python uses the words and, or, not for its boolean operators rather than the symbolic &&, ||, !.

Sergio Fonseca
  • 326
  • 2
  • 11