Z-Test vs. T-Test: When to Use Each and Why?
Statistical hypothesis testing plays a crucial role in data analysis, and two of the most common tests used are the Z-Test and T-Test…
Z-Test vs. T-Test: When to Use Each and Why?
Statistical hypothesis testing plays a crucial role in data analysis, and two of the most common tests used are the Z-Test and T-Test. While both are used to compare means, choosing the right test depends on certain conditions. Let’s break down their differences, assumptions, and when to use each with simple examples and Python code.

What is a Z-Test?
A Z-Test is a statistical test used to determine whether the means of two datasets are significantly different from each other when:
- The sample size is large (n > 30)
- The population variance (σ²) is known
- The data follows a normal distribution

Types of Z-Tests:
- One-Sample Z-Test: Compares the sample mean to the population mean.
- Two-Sample Z-Test: Compares the means of two independent samples.

Example: Comparing the Average Height of Two Cities
Imagine we have height data for people in two different cities and want to test if their mean height differs significantly.
import numpy as np
import scipy.stats as stats
# Sample data
sample_mean = 172 # Mean height of sampled individuals
population_mean = 170 # Expected mean height
population_std = 5 # Known standard deviation
sample_size = 50
# Perform Z-Test
z_stat = (sample_mean - population_mean) / (population_std / np.sqrt(sample_size))
p_value = 2 * (1 - stats.norm.cdf(abs(z_stat))) # Two-tailed test
print(f"Z-Statistic: {z_stat:.4f}")
print(f"P-Value: {p_value:.4f}")
👉 If p-value < 0.05, we reject the null hypothesis, meaning there is a significant difference in heights.

What is a T-Test?
A T-Test is used to compare means when:
- The sample size is small (n ≤ 30)
- The population variance is unknown
- The data follows a normal distribution
Types of T-Tests:
- One-Sample T-Test: Compares the sample mean to the population mean.
- Two-Sample (Independent) T-Test: Compares the means of two independent samples.
- Paired (Dependent) T-Test: Compares means from the same group at different times.
Example: Checking If a New Teaching Method Improves Student Scores
Let’s test if students taught with a new method scored significantly differently from the expected average.
# Sample student scores
scores = np.array([75, 80, 85, 78, 82, 88, 92])
expected_mean = 80
# Perform One-Sample T-Test
t_stat, p_value = stats.ttest_1samp(scores, expected_mean)
print(f"T-Statistic: {t_stat:.4f}")
print(f"P-Value: {p_value:.4f}")
👉 If p-value < 0.05, we conclude that the teaching method has a significant effect on scores.

Example: Comparing Test Scores of Two Groups
Let’s test if two groups of students, one using a traditional method and another using a new method, performed differently.
group_a = np.array([85, 88, 90, 92, 87, 85, 91])
group_b = np.array([78, 80, 82, 79, 81, 77, 83])
# Perform Two-Sample T-Test
t_stat, p_value = stats.ttest_ind(group_a, group_b)
print(f"T-Statistic: {t_stat:.4f}")
print(f"P-Value: {p_value:.4f}")
👉 If p-value < 0.05, we conclude that the two groups performed significantly differently.

Key Differences Between Z-Test and T-Test

Assumptions for Z-Test and T-Test
Before applying either test, ensure these assumptions are met:
✅ Data is normally distributed.
✅ Observations are independent.
✅ For a two-sample test, the two groups should have equal variance (can be checked using Levene’s Test).
( Levene’s test is a statistical test that determines if the variances of two or more groups are equal. It’s also known as the Levene test for homogeneity of variances.
Why is it used?
- Levene’s test is used because many statistical procedures assume that the variances of the populations are equal.
- It’s often used with independent-samples t-tests and between-subjects ANOVAs. )
Conclusion
- Use a Z-Test when the sample size is large and population variance is known.
- Use a T-Test when the sample size is small or population variance is unknown.
- Both tests help determine statistical significance and guide data-driven decisions.
- Always check for normality and independence before performing these tests.
메타데이터
- post_id
- 76bbc4d2bdda
- slug
- z-test-vs-t-test-when-to-use-each-and-why-76bbc4d2bdda
- url
- https://medium.com/@abhishekshaw020/z-test-vs-t-test-when-to-use-each-and-why-76bbc4d2bdda
- canonical_url
- https://medium.com/@abhishekshaw020/z-test-vs-t-test-when-to-use-each-and-why-76bbc4d2bdda
- author_url
- https://medium.com/@abhishekshaw020
- status
- ok
- fetched_at
- 2026-08-10 13:42:29