Population & Sample
You want to know the average commute time of office workers in Pune. You cannot ask all of them, so you ask a few hundred and use their answers. That is the basic move of inferential statistics: measure a sample, then reason about the population.
In this lesson you will learn exactly what these two words mean, the standard symbols for population and sample values, why every sample gives a slightly different answer, and why bigger samples give steadier answers.
Population and sample
The population is the complete group you want to draw conclusions about. It is defined by your question, not by your data. It can be people (all voters in Lucknow), objects (every biscuit packet a factory filled today) or events (every UPI payment made in a day).
A sample is the part of the population you actually observe. A census is the special case where you measure everyone; a national population census tries to do exactly that, but most surveys and studies work with samples.
Why not always measure everyone?
- Cost and time. Interviewing a few thousand people is possible; interviewing millions is not, for most projects.
- Destructive testing. To test how long a light bulb lasts, or whether a cricket helmet survives an impact, you destroy it. You cannot test every one.
- The population may not exist yet. A medicine trial is about all future patients.
- A good sample is often enough. As you will see, a few hundred well-chosen people can estimate a percentage within a few points.
Parameters, statistics and their symbols
A parameter is a number that describes the population. It is fixed but usually unknown. A statistic is calculated from the sample and is used to estimate the parameter. Statisticians use Greek letters for parameters and Latin letters (often with a hat or bar) for statistics.
| Quantity | Population parameter | Sample statistic |
|---|---|---|
| Size | N | n |
| Mean | μ (mu) | x̄ ("x-bar") |
| Standard deviation | σ (sigma) | s |
| Variance | σ² | s² |
| Proportion | p | p̂ ("p-hat") |
In code you will write these as mu, sigma, x_bar, s and p_hat.
Sampling from an invented population
To see how a sample relates to its population, you need a population whose true values you know. In real life that never happens, so this example invents one: commute times of 40,000 office workers. Commute times are skewed (most are short, a few are very long), so the example uses a gamma distribution, which has that shape.
import numpy as np
rng = np.random.default_rng(2026)
# Invented population: one-way commute time (minutes) of all 40,000 office workers in a city
population = rng.gamma(shape=4, scale=9, size=40_000).round(1)
mu = population.mean()
sigma = population.std() # divide by N: this IS the whole population
print(f"Population: N = {population.size}, mean = {mu:.2f}, SD = {sigma:.2f}")
sample = rng.choice(population, size=100, replace=False)
print(f"Sample: n = {sample.size}, mean = {sample.mean():.2f}, SD = {sample.std(ddof=1):.2f}")Population: N = 40000, mean = 35.99, SD = 17.94 Sample: n = 100, mean = 34.89, SD = 15.71
The sample of 100 estimates μ = 35.99 as x̄ = 34.89, about a minute off. rng.choice(..., replace=False) picks 100 different workers, like a real survey where nobody is asked twice. Notice the two different standard deviations: std() divides by N for the whole population, while std(ddof=1) divides by n - 1 for a sample. The spread lesson explains why.
Sampling variability and sample size
The difference between a statistic and the parameter it estimates is called sampling error. It is not a mistake; it happens because each sample contains different people. What matters is how big it usually is, and that depends mostly on the sample size.
import numpy as np
rng = np.random.default_rng(2026)
population = rng.gamma(shape=4, scale=9, size=40_000).round(1)
print(f"True mean: {population.mean():.1f}")
for n in [10, 100, 1000]:
means = [rng.choice(population, size=n, replace=False).mean() for _ in range(5)]
print(f"n = {n:>4}:", " ".join(f"{m:5.1f}" for m in means))True mean: 36.0 n = 10: 47.1 38.9 34.2 31.1 39.8 n = 100: 33.8 34.7 39.6 35.3 35.5 n = 1000: 35.6 36.7 36.0 35.2 36.4
With 10 workers the five sample means range from 31.1 to 47.1. With 1,000 they all fall between 35.2 and 36.7. Bigger samples give answers that cluster more tightly around the truth. Later you will measure this precisely with the standard error, and you will see that to halve the typical error you need four times as many people.
Estimating a proportion
Many questions are about a share: what fraction of households pay by UPI every day, what fraction of voters support a candidate. The population parameter is a proportion p and the sample statistic is p̂.
import numpy as np
rng = np.random.default_rng(11)
uses_upi = rng.random(25_000) < 0.62 # invented town: True = household pays by UPI daily
p = uses_upi.mean()
print("Parameter p (all 25,000 households):", round(p, 4))
survey = rng.choice(uses_upi, size=400, replace=False)
p_hat = survey.mean()
print("Statistic p-hat (400 households): ", p_hat)
print("Sampling error:", round(p_hat - p, 4))Parameter p (all 25,000 households): 0.6242 Statistic p-hat (400 households): 0.6175 Sampling error: -0.0067
The mean of a True/False array is the share of True values, so .mean() gives a proportion directly. Here 400 households estimated the town's 62.4% as 61.75%, within one percentage point.
A file as a population
You can practise the same idea on students.csv. Pretend the 480 students are the whole population, take random samples of 40 with pandas sample(), and compare.
import pandas as pd
students = pd.read_csv("students.csv", na_values=["AB"]).drop_duplicates()
english = students["english"]
print(f"All {len(english)} students: mean {english.mean():.2f}")
for seed in [1, 2, 3]:
sample = english.sample(n=40, random_state=seed)
print(f"Sample of 40 (random_state={seed}): mean {sample.mean():.2f}")All 480 students: mean 62.11 Sample of 40 (random_state=1): mean 64.60 Sample of 40 (random_state=2): mean 60.17 Sample of 40 (random_state=3): mean 60.33
random_state fixes the seed so the same 40 rows are chosen every time. In real work, the 480 students would themselves be a sample of a much larger population, which is exactly why you never treat any one result as the final truth.
Common mistakes
- Not defining the population. "Indians prefer tea" from a survey of 200 people in one Kolkata office is not about Indians.
- Treating p̂ or x̄ as the true value. They are estimates with sampling error.
- Thinking a bigger population needs a much bigger sample. For a large population, the precision depends mainly on the sample size n, not on N.
- Mixing up the formulas. Use N and divide by N for a whole population; use n and the sample formulas for a sample.
Key points
- The population is everyone or everything your question is about; the sample is the part you observe.
- Parameters (μ, σ, p) describe the population; statistics (x̄, s, p̂) are calculated from the sample.
- Every sample gives a slightly different statistic: this is sampling variability, not an error.
- Larger random samples give statistics that cluster more tightly around the parameter.
- The mean of a True/False array is a proportion.
Exercise
Using the invented commute population from this lesson (seed 2026), take 1,000 random samples of size 25 and 1,000 random samples of size 100. For each size, print the smallest and largest of the 1,000 sample means. Which size gives the narrower range, and why?
Show solution
import numpy as np
rng = np.random.default_rng(2026)
population = rng.gamma(shape=4, scale=9, size=40_000).round(1)
for n in [25, 100]:
means = np.array([rng.choice(population, size=n, replace=False).mean() for _ in range(1000)])
print(f"n = {n}: sample means from {means.min():.1f} to {means.max():.1f}")n = 25: sample means from 25.5 to 49.0 n = 100: sample means from 29.6 to 42.3
Samples of 100 give a much narrower range because each mean averages out more individual differences. The true mean is about 36 in both cases.
Quick Quiz
1. A factory tests 50 phone chargers out of the 20,000 made this week. The 20,000 chargers are the:
The population is the whole group the conclusion is about: all 20,000 chargers.
2. Which symbol stands for a sample mean?
Greek letters such as μ are for population parameters; x̄ is the sample mean.
3. Two random samples of 100 students give mean marks of 61.2 and 63.0. What is the best explanation?
Random samples naturally differ; the population mean stays fixed.
4. What happens to sample means as the sample size grows?
Larger samples average out more individual variation, so their means vary less.
Learn Statistics with a mentor
Free lessons get you started. Our Data Science & Machine Learning training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
