Basic Probability & Python from Zero
An exact mathematical foundation: sample spaces, empirical distributions, multi-stage counting principles, and conditional theorems implemented with foundational Python 3.
x, y = 5, 3
answer = (x + y) / 2
prob = (4/52) * (4/51)
print(round(prob, 6))Python from Zero: Foundational Rules
x = 5(x + y) / 2x ** 0.5(33 / 72)print(...)round(val, 4)x = 5
y = 3
# 2. Arithmetic with parentheses precedence
answer = (x + y) / 2
# 3. Output result to screen
print("Answer:", answer)
matplotlib for visual distribution curves.Sample Space & Complement Rule
Total Possible Outcomes: Coin toss (2) × Die roll (6) equally likely results
Target Event E (Tail and Odd): Favorable outcomes are {T1, T3, T5} = 3
Complement Event P(Eᶜ): Probability of NOT getting (Tail and Odd)
probability = 3 / 12
# 2. Complement Rule
complement = 1 - probability
print(probability)
print(complement)
0.75
Empirical Probability & Frequency Ratios
Total Survey Responses (n): Sum all observed counts: Serious + Moderate + Not a Problem
Probability of "Serious" Response: Divide observed serious frequency (123) by total (320)
Exact Decimal Probability: Approximates 38.44% probability
| Response Category | Observed (f) | Relative Probability P(E) |
|---|---|---|
| Serious TARGET | 123 | 123 / 320 = 0.384375 |
| Moderate | 115 | 115 / 320 = 0.359375 |
| Not a Problem | 82 | 82 / 320 = 0.256250 |
| Total Frequency (n) | 320 | Σ P(E) = 1.000000 |
probability = 123 / 320
print(probability)
Fundamental Counting Principle
Count All 8-Digit ID Combinations: Each of 8 positions allows 10 digits (0–9) with repetition.
Probability of One Specific ID: Only 1 favorable outcome out of 100,000,000 total IDs.
Scientific & Decimal Format: P(Specific ID) = 0.00000001
10 × 10 × 10 × 10 × 10 × 10 × 10 × 10 = 10⁸total = 10 ** 8
# 2. Probability of one specific ID
probability = 1 / total
print("Total:", total)
print("Probability:", probability)
Probability: 1e-08
Conditional Probability & Subsets
Identify Conditioned Sub-group B: Denominator restricted to children with the gene.
Count High IQ Within Gene Group: 33 of those 72 children have high IQ.
Rounded 4-Digit Decimal Result: Approximates 45.83% probability
| Group Partition | Count | Conditional Ratio |
|---|---|---|
| High IQ & Gene A ∩ B | 33 | 33 / 72 ≈ 0.4583 |
| Normal IQ & Gene | 39 | 39 / 72 ≈ 0.5417 |
| Total Gene Group (B) | 72 | P(B|B) = 1.0000 |
probability = 33 / 72
# Round to 4 decimal places for clean display
print(round(probability, 4))
Multiplication Rule & Sequences
First Draw — King: 4 Kings in standard 52-card deck
Second Draw — Queen (Without Replacement): 4 Queens remain in 51 remaining cards
Multiply Sequential Probabilities: (4/52) × (4/51) = 16 / 2652
probability = (4/52) * (4/51)
# Round to 6 decimal places
print(round(probability, 6))
At Least One Success Rule
Find Failure Probability (q): Success p = 0.85; failure q = 1 - 0.85
All Successes (p³) vs No Success (q³): Independent repeated 3 procedures
Complement Calculation P(≥ 1): 1 - P(none) = 1 - 0.003375
q = 1 - p
all_success = p ** 3
none = q ** 3
at_least_one = 1 - none
print(all_success)
print(none)
print(at_least_one)
0.003375
0.996625
Section 01 Assignment: Practice Problems
Write and submit a Python script solving the following 3 probability tasks. Ensure all formulas are coded from first principles with clear variable names and comments.
Complementary Defect Model (Ch 3.1 & 3.2): A production line has defective rate p = 0.08. For a sample of n = 4 items drawn independently, compute the probability of finding at least one defective item.
Key Permutation Entropy (Ch 3.1.3): A security access key has 7 positions: 2 uppercase letters (A–Z, repeats allowed) followed by 5 digits (0–9, no repetition). Find the total unique keys and the probability of cracking it on the 1st attempt.
Dependent Sequence Draw (Ch 3.2.2): From a standard shuffled 52-card deck, calculate the joint probability of drawing a King, then a Queen, then a Jack sequentially without replacement.
p_defect = 0.08
n_items = 4
# TODO: Calculate P(at least 1 defective)
p_at_least_one = ...
# Task 2: Badge Key Permutations
# TODO: Apply Counting Principle for 2 letters + 5 digits
total_keys = ...
p_crack = ...
# Task 3: Dependent 3-Card Sequence (Without Replacement)
# TODO: Sequential multiplication (King -> Queen -> Jack)
p_sequence = ...
Task 1 - P(At Least 1 Defect): <float>
Task 2 - Total Keys: <int> | P(Crack): <scientific>
Task 3 - P(K then Q then J): <float: 6 decimals>