FAQ: Rules of Probability - Addition Rule

This community-built FAQ covers the “Addition Rule” exercise from the lesson “Rules of Probability”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Master Statistics with Python

FAQs on the exercise Addition Rule

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi! I find this section confusing. Possibly there is a mistake in the wording of the section? It says “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 :” but then it goes on to find the probability of one OR another of the events occurring, rather than their intersection or Joint Probability. Then, it seems like it might try in the code to talk about intersection but that never gets explained in the text. Can you help me?

This lesson deals with non-exclusive events so you have probabilities for A or B occurring and a non-zero probability of both A and B occurring at the same time.

It might be the terminology tripping you up here (really wish they used the mathematical symbols in addition to the text).

The phrase you mention specifically mentions the union; “the probability of the union of A and B”. This would be P(AB) that you could also call P(A or B); that is the probability of either A, B or both occurring.

This is different to the probability of both A and B occurring together. That would be the intersection you mention P(AB) or P(A and B).

Yes, thank you!!! I see now how I was reading it wrong, and YES math symbols are WAY better! I get it now.

1 Like

Hi, I’m taking this Fundamental Math Course along with my Data Science Path Courses but I already find it confusing since it requires Python programming in the Addition Rule Quiz and I have no coding experience before. Should I start with another beginner coding course first?

Hi,

I am having a hard time with this part:

print(prob_a_or_b(evens, odds, all_possible_rolls))

Where are evens and odd and all possible rolls defined… as a or b? I see it separate but never combined except on this line which is confusing to me.

Thanks,
Sarah

In the Python script there should be a function definition for prob_a_or_b so the a and b are parameters inside this function that let us calculate the union of these two events occurring.

You could pass them as keyword arguments explicitly if you like but it results in the same function call in the end-
prob_a_or_b(a=evens, b=odds, all_possible_outcomes=all_possible_rolls)

1 Like

Greetings everyone, I am doing the fundament math’s for data science course. I am on rules of probability lesson 5 doing the first exercise which is typing the return statement for the total probability which is ":

return prob_a + prob_b - prob_inter

and I copy the solution and I am getting the error :
File “script.py”, line 15
return prob_a + prob_b - prob_inter
^
SyntaxError: ‘return’ outside function

I have not learnt python as yet and I cant figure out why the code is giving me a error seeing as it is the same code in the solution.

Could you link the code you used, without seeing the full function it relies on guesswork, see How do I format code in my posts? for formatted code that keeps indentation (as mentioned below indentation is very important in Python).

A function in Python is a single code block (typically a group of several statements). Python organises code blocks using indentation (generally this is four whitespaces at the start of a line, the cc editor defaults to two, make sure it’s consistent whichever you use). Every statement that is part of the function needs to be indented to the same level.

The following is perfectly valid, each statement that makes up the function is at the same level of indentation-

def func(x):
    y = x + 1
    return y

The following will FAIL because the indentation of the return is wrong (the return is not part of the function code block)-

def func(x):
    y = x + 1
return y  # <-- This will throw a syntax error

Thank you very much I figured out the issue. I should have indented the function, truly sorry I am not used to python code as yet.

1 Like