And, Or, Not
Combining events.
Real questions combine events. What's the chance of drawing a heart, or a king? What's the chance of not getting spam? A few simple rules handle all of them.

- Heart or king?
- Not spam?
- A few simple rules
A deck of cards
We build a deck of fifty two cards, from thirteen ranks and four suits. Then we count. Thirteen hearts, and four kings. But heart or king isn't thirteen plus four, because the king of hearts would be counted twice. Run it. Sixteen cards, so four out of thirteen.
from fractions import Fraction
from itertools import product
ranks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
deck = list(product(ranks, ["hearts", "spades", "clubs", "diamonds"]))
def P(test):
return Fraction(sum(test(c) for c in deck), len(deck))
print("hearts:", P(lambda c: c[1] == "hearts"))
print("kings: ", P(lambda c: c[0] == "K"))
print("either:", P(lambda c: c[1] == "hearts" or c[0] == "K"))hearts: 1/4 kings: 1/13 either: 4/13
The three rules
Not A is one minus the probability of A. A or B is the probability of A, plus B, minus the overlap, so we don't count it twice. And if A and B can't both happen, the overlap is zero, so you just add. We'll meet the rule for and in the next lesson.
| Rule | Formula |
|---|---|
| not A | 1 − P(A) |
| A or B | P(A) + P(B) − P(A and B) |
| can't both happen | P(A) + P(B) |
Summary
Not is one minus, and or is add, then subtract the overlap. Next, the and rule, for independent events.
Key takeaways- P(not A) = 1 − P(A)
- Or: add, minus overlap
- Don't double count