Z-Test Calculator

Run a one-sample z-test to check whether a sample mean is significantly different from a hypothesized population mean. Enter your values below, and see the formula, a worked example, and ready-to-use Python code underneath.

Enter your values

Please fill in all fields — standard deviation and sample size must be greater than 0.

Z-statistic
P-value
Decision at chosen α

What a z-test tells you

A one-sample z-test checks whether a sample's mean is significantly different from a hypothesized population mean, when the population standard deviation is known. It converts the difference between the sample mean and the hypothesized mean into a z-score, then uses the standard normal distribution to work out how likely that difference (or a more extreme one) would be if the hypothesized mean were actually correct.

A z-test assumes the population standard deviation is known and generally requires a reasonably large sample (commonly n ≥ 30). When the population standard deviation is unknown and estimated from the sample instead, a t-test is the appropriate tool.

Z-test formula

z = ( x̄ − μ₀ ) / ( σ / √n )
x̄ is the sample mean, μ₀ is the hypothesized population mean, σ is the known population standard deviation, and n is the sample size. The p-value is then found from the standard normal distribution, using one or both tails depending on the alternative hypothesis.

Python code

You can compute the z-statistic and p-value manually with math.erf, or with SciPy:

# Method 1 — manual calculation (two-tailed)
import math

x_bar, mu0, sigma, n = 102, 100, 8, 40
z = (x_bar - mu0) / (sigma / math.sqrt(n))

def normal_cdf(z):
    return 0.5 * (1 + math.erf(z / math.sqrt(2)))

p_two_tailed = 2 * (1 - normal_cdf(abs(z)))
print(z, p_two_tailed)  # 1.5811 0.1138

# Method 2 — statsmodels
from statsmodels.stats.weightstats import ztest
# ztest() takes raw sample data rather than summary statistics

Worked example

A factory claims its light bulbs last 100 hours on average, with a known population standard deviation of 8 hours. A sample of 40 bulbs has a mean lifetime of 102 hours. Is there evidence, at α = 0.05, that the true average is greater than 100 hours (a right-tailed test)?

  1. 1
    Compute the standard error.
    σ / √n = 8 / √40 = 1.2649
  2. 2
    Compute the z-statistic.
    (102 − 100) / 1.2649 = 1.5811
  3. 3
    Find the p-value for a right-tailed test.
    p = 1 − Φ(1.5811) = 0.0569
  4. 4
    Compare the p-value to α.
    0.0569 > 0.05, so the result is not statistically significant at this significance level.

We fail to reject the null hypothesis — this sample doesn't provide strong enough evidence, at α = 0.05, that the true average bulb lifetime exceeds 100 hours, even though the sample mean itself was higher.

Frequently asked questions

When should I use a z-test instead of a t-test?

Use a z-test when the population standard deviation is known and the sample size is reasonably large (commonly n ≥ 30). Use a t-test when the population standard deviation is unknown and estimated from the sample, which is the more common situation in practice, especially with smaller samples.

How do I choose between a one-tailed and two-tailed test?

Use a two-tailed test when your hypothesis is simply that the population mean differs from the hypothesized value, in either direction. Use a one-tailed test (left or right) only when your hypothesis specifically predicts the direction of the difference before looking at the data.

What does the p-value from a z-test actually mean?

The p-value is the probability of observing a sample statistic at least as extreme as the one calculated, assuming the null hypothesis is true. A small p-value suggests the observed result would be unusual if the null hypothesis were correct.

What does it mean to "reject the null hypothesis"?

Rejecting the null hypothesis means the p-value fell below your chosen significance level (alpha), so the observed data would be unusually unlikely if the null hypothesis were true. It doesn't prove the alternative hypothesis is correct, only that the evidence is inconsistent with the null hypothesis at that significance level.

What significance level (alpha) should I use?

0.05 is the most common default across many fields, meaning you're willing to accept a 5% chance of rejecting a true null hypothesis. Some fields use stricter thresholds like 0.01, and the right choice depends on the cost of a false positive in your specific context.

Advertisement