Before Lab 1 — Python from Zero
Students are not expected to know Python before using this manual. Only a few ideas are needed:
- A variable stores a value:
x = 5. - Use
+,-,*,/for arithmetic. - Use
**for powers;** 0.5gives a square root. - Use parentheses to control the order of calculation.
- Use
print(...)to display a result. - Use
round(value, digits)only to make displayed answers easier to read. - Graph examples use
matplotlibbecause graphing requires plotting software.
x = 5
y = 3
answer = (x + y) / 2
print("Answer:", answer)
Expected Output
Answer: 4.0
3.1 Basic Concepts of Probability
Case 3.1.1 — Sample Space
When do we use this?
Use a sample space to list all possible outcomes of a probability experiment.
Mathematical Equations
Worked Lecture Example
A coin is tossed and then a die is rolled. There are equally likely outcomes.
Step 1 — Count the outcomes
The coin has 2 outcomes and the die has 6.
2 × 6 = 12
Step 2 — Define the event
Tail AND odd gives T1, T3, T5: three outcomes.
P(Tail and odd) = 3 / 12 = 0.25
Step 3 — Find the complement
Subtract from 1.
P(complement) = 1 - 0.25 = 0.75
Final Result
P(Tail and odd) = 0.25; complement = 0.75.
Simple Python Code
probability = 3 / 12
complement = 1 - probability
print(probability)
print(complement)
Expected Output
0.25
0.75
Case 3.1.2 — Empirical Probability
When do we use this?
Use empirical probability when probabilities are based on observed frequencies.
Mathematical Equation
Worked Lecture Example
Lecture survey counts are serious 123, moderate 115, not a problem 82.
| Response category | Frequency | Relative probability |
|---|---|---|
| Serious | 123 | |
| Moderate | 115 | |
| Not a problem | 82 | |
| Total | 320 |
Step 1 — Find the total
Add all responses.
Total = 123 + 115 + 82 = 320
Step 2 — Find the probability of serious
Divide 123 by 320.
P(serious) = 123 / 320 = 0.384375
Final Result
P(serious) = 0.384375.
Simple Python Code
probability = 123 / 320
print(probability)
Expected Output
0.384375
Case 3.1.3 — Fundamental Counting Principle
When do we use this?
Use the counting principle when a process has several stages with a fixed number of choices at each stage.
Mathematical Equation
Worked Lecture Example
An eight-digit ID allows 10 digits in every position and repetition is allowed.
Step 1 — Count all IDs
Multiply 10 choices eight times.
10⁸ = 100,000,000
Step 2 — Find the probability of one specific ID
Only one outcome matches a specified ID.
P = 1 / 100,000,000 = 0.00000001
Final Result
There are 100,000,000 possible IDs; one specific ID has probability 0.00000001.
Simple Python Code
total = 10 ** 8
probability = 1 / total
print("Total:", total)
print("Probability:", probability)
Expected Output
Total: 100000000
Probability: 1e-08
3.2 Conditional Probability and the Multiplication Rule
Case 3.2.1 — Conditional Probability
When do we use this?
Use conditional probability when the probability of one event is calculated under the condition that another event has already occurred.
Mathematical Equation
Worked Lecture Example
In the lecture table, 72 children are in the gene group and 33 of them have high IQ.
| Group partition | Count | Conditional ratio |
|---|---|---|
| High IQ and gene | 33 | |
| Normal IQ and gene | 39 | |
| Total gene group | 72 |
Step 1 — Identify the conditioned group
The denominator is the 72 children with the gene.
Step 2 — Calculate the conditional probability
33 of those 72 have high IQ.
P(high IQ | gene) = 33 / 72 = 0.4583
Final Result
Conditional probability ≈ 0.4583.
Simple Python Code
probability = 33 / 72
print(round(probability, 4))
Expected Output
0.4583
Case 3.2.2 — Multiplication Rule
When do we use this?
Use the multiplication rule for probabilities of events occurring together or in sequence.
Mathematical Equation
Worked Lecture Example
Find the probability of drawing a king and then a queen without replacement.
Step 1 — First draw
There are 4 kings among 52 cards.
P(K) = 4 / 52
Step 2 — Second draw
After one card is removed, 51 remain and all 4 queens remain.
P(Q | K) = 4 / 51
Step 3 — Multiply
Multiply the two probabilities.
P(K then Q) = (4/52)(4/51) = 0.006033
Final Result
P(king then queen) ≈ 0.006033.
Simple Python Code
probability = (4/52) * (4/51)
print(round(probability, 6))
Expected Output
0.006033
Case 3.2.3 — At Least One Success
When do we use this?
For independent repeated trials, use the complement when 'at least one' is easier to calculate through 'none'.
Mathematical Equation
Worked Lecture Example
Three independent procedures each have success probability 0.85.
Step 1 — Find failure probability
Subtract success probability from 1.
q = 1 - 0.85 = 0.15
Step 2 — Find all-success probability
Multiply 0.85 three times.
P(all success) = 0.85³ = 0.614125
Step 3 — Find no-success probability
Multiply 0.15 three times.
P(none) = 0.15³ = 0.003375
Step 4 — Find at least one
Use the complement.
P(at least one) = 1 - 0.003375 = 0.996625
Final Result
P(at least one success) = 0.996625.
Simple Python Code
p = 0.85
q = 1 - p
all_success = p ** 3
none = q ** 3
at_least_one = 1 - none
print(all_success)
print(none)
print(at_least_one)
Expected Output
0.6141249999999999
0.003375000000000001
0.996625
Formula Reference
| Concept | Formula | Application |
|---|---|---|
| Classical | Equally likely sample space | |
| Complement | Negation of event | |
| Empirical | Observed sample tallies | |
| Counting rule | Multi-stage combinations | |
| Conditional | Conditioned outcome space | |
| Multiplication | Joint / sequential draws | |
| At least one | Repeated independent trials |