FAQ: Introduction to Data Science - Probability

This community-built FAQ covers the “Probability” exercise from the lesson “Introduction to Data Science”.

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

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

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

Found a bug? Report it!

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!

1 Like

I have zero knowledge of scientific probability so it may be just me, but on a purely common-sense level the result of this script feels completely counter-intuitive:
If there are 365 possible unique birth dates and there are 90 people in the room (i.e. 1/4 of the number of unique birthdates), how can the probability of two of them having the same birthday be “nearly 100%”?

Moreover, why does the script return an error if I enter a number of people higher than 120?

4 Likes

This is a well known problem called the `birthday paradox`.

When there are 23 people in a room, the probability of two having the same birthday is roughly 0.5. That number goes up quickly as more and more people enter the room. With 90 people in the room the chance of no two having the same birthday is very small, about 0.00002. So the probability that at least two people have the same birthday is `1 - 0.00002` which is still very close to 1.

``````>>> 90 * 89 / 2
4005.0
>>> (364/365) ** 4005
1.690905614940199e-05
>>> 1 - (364/365) ** 4005
0.9999830909438506
>>>
``````

I was wondering, in the code for this exercise the “simulate” function comes earlier in the code than the “calculate_probability” and the “find_duplicates” functions even though “simulate” uses these functions in its body. Does python care about the order of the function definitions in its code?

1 Like

See the following:

``````def func_one(m):
return func_two(m) * 2

def func_two(n):
return n * 7

print(func_one(4))
``````

It is fine for `func_one` to include a call to `func_two`, even though `func_two` was not yet defined, so long as `func_two` gets defined before that calling statement actually executes.

Neither of the above functions executes until this statement calls `func_one`:

``````print(func_one(4))
``````

Therefore, by the time the statement in `func_one` that calls `func_two` gets executed, `func_two` has been defined.

By contrast, this raises a `NameError` because the call to `func_two` attempts to execute before `func_two` has been defined:

``````def func_one(m):
return func_two(m) * 2

print(func_one(4))

def func_two(n):
return n * 7
``````
2 Likes

I have the same question.

The reason is in the error message…

OverflowError: int too large to convert to float.

`365 ** 121`

``````10900253526988599119907703164503902697505174148190765436828891855178569939559679242596089803713445592238620332424027497206010873823076867687555682976217182638044634905677337287410710323991339515712966052039592067052495019377970508968863252073606107899050681789037659072567898466132874091272242367267608642578125
``````
1 Like