A t-test checks whether a mean differs from a fixed value, or whether two means differ, when the spread has to be estimated from the sample itself.
Every version has the same shape: t = (observed difference − difference under the null) / its standard error. The one-sample test uses t = (x̄ − μ₀) / (s / √n) with n − 1 degrees of freedom. Because s is itself an estimate, the statistic follows a t distribution, which has heavier tails than the normal: the two-sided 5% cutoff is 2.23 with 10 degrees of freedom, 2.04 with 30 and 1.98 with 100, approaching the normal's 1.96. William Sealy Gosset worked this out at the Guinness brewery, where samples were small, and published it in 1908 under the name "Student".
One-sample example: a machine should fill cups with 250 ml, and 16 cups average 247 ml with a standard deviation of 6 ml. t = (247 − 250) / (6 / √16) = −3 / 1.5 = −2.0 with 15 degrees of freedom, so p ≈ 0.064 (two-sided). The 95% confidence interval, 243.8 to 250.2 ml, still contains 250, so the data doesn't show the machine is off, though a larger sample might.
Two-sample example: two page designs are each shown to 25 users, with mean session lengths of 5.2 and 4.4 minutes and standard deviations of 1.5 and 1.3. Welch's t = 0.8 / √(1.5²/25 + 1.3²/25) = 0.8 / 0.397 ≈ 2.02 with about 47 degrees of freedom, so p ≈ 0.050. The 95% interval for the difference runs from about 0 to 1.6 minutes, which says more than "just significant": the true effect could be anywhere from nothing to large. Welch's version doesn't assume equal variances and loses little when they are equal, so it is the safer default over Student's pooled test. R's t.test uses Welch by default; SciPy's ttest_ind needs equal_var=False.
Paired example: 8 users do a task before and after a redesign, taking 12, 15, 9, 20, 14, 11, 18 and 16 seconds before and 11, 13, 9, 18, 12, 10, 16 and 15 after. The paired t-test works on the 8 differences (mean 1.375, standard deviation 0.74): t = 1.375 / (0.74 / √8) ≈ 5.2 with 7 degrees of freedom, p ≈ 0.001. Treating the two columns as independent groups gives t ≈ 0.81 and p ≈ 0.43, because the large differences between users swamp the small, consistent improvement. Pairing removes that between-user variation, which is why before-and-after data on the same units needs the paired test.
The assumptions: observations are independent, and the sample mean (or the mean difference) is roughly normal. That holds when the data itself is normal or, through the central limit theorem, when the sample is large enough for its skew; a few dozen points often handle moderate skew, but heavy tails and outliers still distort both the mean and the standard deviation. For small, skewed samples, use a rank test such as the Mann-Whitney U test (or Wilcoxon signed-rank for paired data), a log transform, or bootstrapping. Only Student's two-sample test needs equal variances.
t-test vs z-test: the z-test needs a known population standard deviation, which is rare, or a large sample, as in A/B tests on conversion rates. With large samples the two agree: in the design example the normal distribution gives p ≈ 0.044 instead of 0.050, and with hundreds of users per group the gap disappears. For three or more groups, use ANOVA rather than many pairwise t-tests; with two groups, ANOVA's F equals the square of the pooled t statistic. Student's two-sample t-test is also the same as regressing the outcome on a 0/1 group indicator and testing its coefficient.
Interview follow-ups: one-sided vs two-sided tests (choose before seeing the data; when the effect goes the predicted way, the one-sided p is half the two-sided one), effect size (Cohen's d = 0.8 / 1.40 ≈ 0.57 in the design example, a medium effect, since a p-value alone mixes the size of the effect with the size of the sample), and what to do after running t-tests across many metrics or segments: correct for multiple testing, for example with Bonferroni.