statistics.NormalDist Computes Normal Probabilities
Build a statistics.NormalDist from mean and standard deviation, then read PDF, CDF, and inverse CDF values for a normal distribution.
Code
from statistics import NormalDist
def main() -> None:
distribution = NormalDist(mu=100, sigma=15)
print("mean:", distribution.mean)
print("stdev:", distribution.stdev)
for score in (85, 100, 115):
density = distribution.pdf(score)
cumulative = distribution.cdf(score)
print(score, "pdf:", round(density, 6), "cdf:", round(cumulative, 4))
# inv_cdf maps a probability back to the value at that quantile.
print("90th percentile:", round(distribution.inv_cdf(0.90), 2))
# Adding independent normal distributions combines mean and variance.
second = NormalDist(mu=50, sigma=5)
combined = distribution + second
print("combined mean:", combined.mean)
print("combined stdev:", round(combined.stdev, 2))
if __name__ == "__main__":
main()Output
Captured from a sandboxed run of this snippet in the build pipeline.
mean: 100.0
stdev: 15.0
85 pdf: 0.016131 cdf: 0.1587
100 pdf: 0.026596 cdf: 0.5
115 pdf: 0.016131 cdf: 0.8413
90th percentile: 119.22
combined mean: 150.0
combined stdev: 15.81How it works
statistics.NormalDist Computes Normal Probabilities keeps everything in the function main.
Iteration happens inside for score in (85, 100, 115) — everything outside it runs once. NormalDist are the only symbols borrowed from statistics — the coupling is explicit and narrow.
Points of care
- Dependency check:
statistics— not built-in, must be present in the project.
Putting it to use
The main signature reads def main() -> None
The run emits 3 labeled sections — mean:, stdev:, 90th percentile: — one per step.
Interactive Tools for This Topic
A/B Test Sample Size CalculatorCalculate required per-variant sample size and experiment duration for two-proportion A/B tests from baseline conversion, MDE, alpha, and statistical power.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.Statistics CalculatorCompute count, sum, mean, median, minimum, maximum, range, variance, and standard deviation for a number list.Text StatisticsCount characters, words, lines, sentences, paragraphs, and estimated reading time entirely in the browser with no text sent to a server.
FAQ
- What does statistics.NormalDist Computes Normal Probabilities print when run?
- Execute the whole file; each printed line is prefixed with a label like `mean:`, `stdev:`, `90th percentile:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.