Search code examples
pythonpython-3.xlogic

De Morgan Law in Python 3


According to De Morgan law:

¬(P ˄ Q) ↔ (¬P) ˅ (¬Q)
¬(P ˅ Q) ↔ (¬P) ˄ (¬Q)

In Python 3.5 when I run:

A = True
B = True
x = not(A and B)==(not A) or (not B)
y = not(A or B)==(not A) and (not B)
print('x is :', x, '\ny is :' ,y)

This returns:

x is : True 
y is : False

Question: why y is False?


Solution

  • Try adding some parentheses -- == is higher precedence than or.

    Here is the precedence table