What is DeMorgan’s Law in Programming? [ Answered with pics]

Demorgan’s Law is something that any student of programming eventually needs to deal with.

Just tell me the “formula”, ok :

not (A and B ) is equivalent to not A or not B

not (A or B ) is equivalent to not A and not B

Demorgan’s Law via Animation and Code

2 animations of Penjee Python code demonstrating DeMorgan’s law

The goal of the map below is to write a while loop until Penjee is at the Fish.


Version 1 of the Code


Version 2 (logically equivalent code rewritten using DeMorgan’s Law)


Demorgan's Law Demo 1

At the bottom of this page, we will demonstrate how these two snippets of Penjee Python code are logically equivalent, but first, here are some more examples of DeMorgan’s Law:

DeMorgan’s Law Truth Table


Demorgans Law Truth Table

How about a Truth Table comparing




This truth table is filled out, below– Pay close attention to when A and B are different (A is True,  B is False)


Python Code Example 1

is equivalent to


So, when you add the not you also must “flip” the and/or

Python Code Example 2

is equivalent to

Example 3

is equivalent to