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

1 2 3 |
penjee = Penguin() while not ( penjee.isFish(here) and penjee.isRock(right)): penjee.waddle() |

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

1 2 3 |
penjee = Penguin() while not penjee.isFish(here) or not penjee.isRock(right): penjee.waddle() |

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

How about a Truth Table comparing

1 |
not (A and B) |

with

1 |
not A and not B |

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

1 |
not (A and B) |

is equivalent to

1 |
not A or not B |

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

##### Python Code Example 2

1 |
not (A or B) |

is equivalent to

1 |
not A and not B |

##### Example 3

1 |
not A and B > 3 |

is equivalent to

1 |
not (A or B<= 3) |