Learn

Now, it’s time to apply these concepts to calculate probabilities.

Let’s go back to one of our first examples: event A is rolling an odd number on a six-sided die and event B is rolling a number greater than two. What if we want to find the probability of one or both events occurring? This is the probability of the union of A and B:

P(A or B)P(A \text{ or } B)

We can visualize this calculation as follows:

This gif shows three sequential images of a Venn diagram that outline the formula for P(A or B). In the Venn Diagram, there are two overlapping circles: one that corresponds to event A and one that corresponds to event B. In the first image, the event A circle is shaded blue and P(A) is added to the formula. In the second image, the event B circle is shaded red and the formula now shows P(A) + P(B). In the final image, the overlap of event A and event B is shaded green and the formula now shows P(A) + P(B) - P(A and B).

This animation gives a visual representation of the addition rule formula, which is:

P(A or B)=P(A)+P(B)P(A and B)P(A \text{ or } B) = P(A) + P(B) - P(A \text{ and } B)

We subtract the intersection of events A and B because it is included twice in the addition of P(A) and P(B).

What if the events are mutually exclusive? On a single die roll, if event A is that the roll is less than or equal to 2 and event B is that the roll is greater than or equal to 5, then events A and B cannot both happen.

This gif shows two sequential images of a Venn diagram that outline the formula for P(A or B) for independent events. In the Venn Diagram, there are two non-overlapping circles: one that corresponds to event A and one that corresponds to event B. In the first image, the event A circle is shaded blue and P(A) is added to the formula. In the second image, the event B circle is shaded red and the final formula now shows P(A) + P(B).

For mutually exclusive events, the addition rule formula is:

P(A or B)=P(A)+P(B)P(A \text{ or } B) = P(A) + P(B)

This is because the intersection is empty, so we don’t need to remove any overlap between the two events.

Instructions

1.

In script.py, there is some code written out for you. First, there is a function, prob_a_or_b() which calculates the addition rule. It takes in three arguments:

  • a: an event with possible outcomes represented as a set
  • b: an event with possible outcomes represented as a set
  • all_possible_outcomes: a set that represents all possible outcomes of a sample space

In prob_a_or_b(), the probability of a and b as well as the probabilty of their intersection has been calculated in the following variables:

  • prob_a
  • prob_b
  • prob_inter

Using these variables, write a return statement that returns the probability of events a or b occurring.

2.

In script.py, there are three different random events outlined through sets. The first one is below the following comment:

# rolling a die once and getting an even number or an odd number

Call prob_a_or_b() using the following variables:

  • evens
  • odds
  • all_possible_rolls

Be sure to wrap your function call in a print() statement. Add your line of code below the following comment:

# call function here first

Bonus: Try to calculate the probability using pencil and paper and compare it to the value you get using prob_a_or_b().

3.

The second random scenario is below the following comment:

# rolling a die once and getting an odd number or a number greater than 2

Call prob_a_or_b() using the following variables:

  • odds
  • greater_than_two
  • all_possible_rolls

Be sure to wrap your function call in a print() statement. Add your line of code below the following comment:

# call function here second

Bonus: Try to calculate the probability using pencil and paper and compare it to the value you get using prob_a_or_b().

4.

The final random scenario is below the following comment:

# selecting a diamond card or a face card from a standard deck of cards

Call prob_a_or_b() using the following variables:

  • diamond_cards
  • face_cards
  • all_possible_cards

Be sure to wrap your function call in a print() statement. Add your line of code below the following comment:

# call function here third

Bonus: Try to calculate the probability using pencil and paper and compare it to the value you get using prob_a_or_b().

Take this course for free

By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.
Already have an account?