DeMorgan’s Law

  • less than 1 minute read
  • 1 code sample

Also available in Română

DeMorgan’s law is a simple law that I learned at UPT during one of my hardware classes. While it is useful in hardware it, it is also useful when writing programs.

If you have a condition like not (A and B), you can rewrite it to !A or !B.

Experiment

Try all four possible combinations of A and B in the example. Both expressions should produce the same result for every row of the truth table.

if __name__ == '__main__':    a = True    b = True    if not (a and b):        print("True")    else:        print("False")    if not a or not b:        print("True")    else:        print("False")