Implementing Probability Basics in Python
Defining Sample Space and Events
# Small numbers on a die
A = {1, 2, 3}
# Even numbers on a die
B = {2, 4, 6}
die_outcomes = 6
Here we define:
- A={1,2,3} representing "small" outcomes;
- B={2,4,6} representing "even" outcomes.
The total number of die outcomes is 6.
Performing Set Operations
12345678# Small numbers on a die A = {1, 2, 3} # Even numbers on a die B = {2, 4, 6} die_outcomes = 6 print(f'A and B = {A & B}') # {2} print(f'A or B = {A | B}') # {1, 2, 3, 4, 6}
- The intersection Aβ©B={2} β common element.
- The union AβͺB={1,2,3,4,6} β all elements in A or B.
Calculating Probabilities
123456789101112131415161718# Small numbers on a die A = {1, 2, 3} # Even numbers on a die B = {2, 4, 6} die_outcomes = 6 A_and_B = A & B # {2} A_or_B = A | B # {1, 2, 3, 4, 6} P_A = len(A) / die_outcomes P_B = len(B) / die_outcomes P_A_and_B = len(A_and_B) / die_outcomes P_A_or_B = P_A + P_B - P_A_and_B print("P(A) =", P_A) print("P(B) =", P_B) print("P(A β© B) =", P_A_and_B) print("P(A βͺ B) =", P_A_or_B)
We use the formulas:
- P(A)=6ββ£Aβ£ββ=6β3β;
- P(B)=6ββ£Bβ£ββ=6β3β;
- P(Aβ©B)=6ββ£Aβ©Bβ£ββ=6β1β;
- P(AβͺB)=P(A)+P(B)βP(Aβ©B)=6β5β.
Additional Set Details
12345only_A = A - B # {1, 3} only_B = B - A # {4, 6} print(only_A) print(only_B)
- Elements only in A: {1, 3};
- Elements only in B: {4, 6}.
Everything was clear?
Thanks for your feedback!
SectionΒ 5. ChapterΒ 2
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Suggested prompts:
Can you explain the inclusion-exclusion principle in more detail?
How do you visualize these probabilities using a Venn diagram?
What happens if the events A and B are mutually exclusive?
Awesome!
Completion rate improved to 1.96
Implementing Probability Basics in Python
Swipe to show menu
Defining Sample Space and Events
# Small numbers on a die
A = {1, 2, 3}
# Even numbers on a die
B = {2, 4, 6}
die_outcomes = 6
Here we define:
- A={1,2,3} representing "small" outcomes;
- B={2,4,6} representing "even" outcomes.
The total number of die outcomes is 6.
Performing Set Operations
12345678# Small numbers on a die A = {1, 2, 3} # Even numbers on a die B = {2, 4, 6} die_outcomes = 6 print(f'A and B = {A & B}') # {2} print(f'A or B = {A | B}') # {1, 2, 3, 4, 6}
- The intersection Aβ©B={2} β common element.
- The union AβͺB={1,2,3,4,6} β all elements in A or B.
Calculating Probabilities
123456789101112131415161718# Small numbers on a die A = {1, 2, 3} # Even numbers on a die B = {2, 4, 6} die_outcomes = 6 A_and_B = A & B # {2} A_or_B = A | B # {1, 2, 3, 4, 6} P_A = len(A) / die_outcomes P_B = len(B) / die_outcomes P_A_and_B = len(A_and_B) / die_outcomes P_A_or_B = P_A + P_B - P_A_and_B print("P(A) =", P_A) print("P(B) =", P_B) print("P(A β© B) =", P_A_and_B) print("P(A βͺ B) =", P_A_or_B)
We use the formulas:
- P(A)=6ββ£Aβ£ββ=6β3β;
- P(B)=6ββ£Bβ£ββ=6β3β;
- P(Aβ©B)=6ββ£Aβ©Bβ£ββ=6β1β;
- P(AβͺB)=P(A)+P(B)βP(Aβ©B)=6β5β.
Additional Set Details
12345only_A = A - B # {1, 3} only_B = B - A # {4, 6} print(only_A) print(only_B)
- Elements only in A: {1, 3};
- Elements only in B: {4, 6}.
Everything was clear?
Thanks for your feedback!
SectionΒ 5. ChapterΒ 2