Statistics with Python: 100 Solved Exercises for Data Analysis
Introduction
Statistics is one of the most important foundations of modern data analysis. Whether you are studying engineering, finance, business, computer science, artificial intelligence, or scientific research, statistical thinking helps transform raw observations into useful conclusions. 🐍📊
Python makes statistical analysis particularly accessible because it combines a readable programming language with powerful libraries such as NumPy, pandas, SciPy, Matplotlib, and Statsmodels.
This practical guide introduces 100 solved statistics exercises for Python-based data analysis. The exercises progress from basic descriptive statistics to probability, distributions, sampling, hypothesis testing, correlation, regression, and real-world analytical interpretation.
The objective is not simply to calculate statistical values. A professional analyst must also understand what the results mean, whether the data is reliable, and how the findings can support a decision. 🔎
Background Theory
Statistics can broadly be divided into two major areas: descriptive statistics and inferential statistics.
Descriptive Statistics
Descriptive statistics summarize information already present in a dataset.
Common techniques include:
- Mean
- Median
- Mode
- Minimum and maximum
- Range
- Variance
- Standard deviation
- Quartiles
- Percentiles
- Frequency distributions
- Data visualization
For example, an engineer may collect measurements from 500 manufactured components. Instead of inspecting every measurement individually, descriptive statistics can reveal the typical measurement, variation, unusual observations, and overall distribution.
Inferential Statistics
Inferential statistics goes further. It uses a sample to make conclusions about a larger population.
Typical applications include:
- Confidence intervals
- Hypothesis testing
- Statistical significance
- Correlation analysis
- Regression
- ANOVA
- Probability models
This distinction is essential: descriptive statistics describe observed data, while inferential statistics help reason beyond the observed sample.
Python Statistical Ecosystem
Python provides several complementary tools:
| Library | Main purpose |
|---|---|
| NumPy | Numerical calculations |
| pandas | Data manipulation |
| SciPy | Scientific and statistical analysis |
| Matplotlib | Visualization |
| Seaborn | Statistical visualization |
| Statsmodels | Statistical models and inference |
| scikit-learn | Predictive modeling |
Definition
Statistics with Python is the application of statistical concepts and analytical methods through Python programming tools to collect, organize, summarize, visualize, analyze, and interpret data.
A typical workflow looks like this:
Data → Cleaning → Exploration → Statistics → Visualization → Interpretation → Decision
The important point is that Python does not replace statistical reasoning. It automates calculations and provides analytical tools, but the analyst remains responsible for selecting appropriate methods and interpreting the results correctly.
Step-by-Step Explanation
A reliable statistical analysis normally follows a structured workflow.
Step 1: Import the Data
Data may originate from:
- CSV files
- Excel spreadsheets
- Databases
- Sensors
- Surveys
- APIs
- Laboratory experiments
- Website analytics
Pandas is commonly used to load structured datasets.
Step 2: Inspect the Dataset
Before performing statistical calculations, inspect:
- Number of rows
- Number of columns
- Column names
- Data types
- Missing observations
- Duplicate records
- Unusual values
⚠️ Never begin statistical modeling before understanding the structure of the data.
Step 3: Clean the Data
Cleaning can include:
- Removing duplicates
- Correcting data types
- Handling missing values
- Detecting impossible observations
- Standardizing categories
- Identifying outliers
Step 4: Calculate Descriptive Statistics
Python can quickly summarize numerical columns and reveal patterns that may not be obvious from the raw dataset.
Step 5: Visualize the Data
Charts help identify:
- Skewness
- Clusters
- Trends
- Outliers
- Relationships
- Distribution shapes
Step 6: Select an Appropriate Statistical Method
The method depends on the question and data.
For example:
- Comparing groups → t-test or ANOVA
- Examining relationships → correlation
- Predicting a numerical variable → regression
- Studying categorical relationships → chi-square analysis
- Examining distributions → distribution analysis
Step 7: Interpret the Results
The final result should answer a practical question.
A statistical output by itself is not a conclusion.
100 Solved Exercises
The following exercises form a compact practice sequence. The solutions describe the expected analytical result or Python approach without relying on lengthy mathematical notation.
Exercises 1–10: Data Fundamentals
1. Load a CSV dataset
Solution: Use pandas to read the CSV into a DataFrame, then inspect its first records.
2. Count observations
Solution: Examine the DataFrame dimensions to determine the number of records.
3. Identify numerical columns
Solution: Use pandas data-type information to separate numerical variables from categorical variables.
4. Find missing values
Solution: Apply pandas missing-value detection and summarize missing observations by column.
5. Remove duplicate records
Solution: Detect duplicated rows and retain one copy of each unique observation.
6. Examine column names
Solution: Display the DataFrame column index and verify that names are meaningful.
7. Generate a statistical summary
Solution: Use pandas descriptive-statistics functionality to obtain central tendency and variation information.
8. Sort observations
Solution: Sort the selected variable in ascending or descending order and inspect extreme observations.
9. Filter observations
Solution: Apply a Boolean condition to select records satisfying a specified analytical criterion.
10. Select a random sample
Solution: Use pandas sampling functionality to select representative observations for exploratory analysis.
Exercises 11–20: Central Tendency
11. Find the mean
Solution: Use the pandas mean method on the selected numerical column.
12. Find the median
Solution: Apply the median method and compare it with the mean.
13. Find the mode
Solution: Use the mode method and inspect whether one or multiple values dominate.
14. Compare mean and median
Solution: A large difference can indicate skewness or extreme observations.
15. Find the minimum
Solution: Use the minimum function to identify the smallest recorded observation.
16. Find the maximum
Solution: Use the maximum function to identify the largest observation.
17. Determine the range
Solution: Compare the minimum and maximum values to understand the overall spread.
18. Calculate quartiles
Solution: Use pandas quantile functionality to identify distribution boundaries.
19. Find the interquartile range
Solution: Compare the upper and lower quartiles to measure the spread of the central portion of the data.
20. Identify the most representative statistic
Solution: When data contains strong outliers, the median can provide a more robust description of the center.
Exercises 21–30: Variability
21. Calculate variance
Solution: Use pandas or NumPy variance functionality while checking whether sample or population interpretation is appropriate.
22. Calculate standard deviation
Solution: Apply the standard deviation method and interpret it as a measure of data dispersion.
23. Compare two datasets
Solution: Compare their standard deviations to determine which dataset has greater variability.
24. Detect high variability
Solution: Examine dispersion relative to the typical values rather than considering the standard deviation alone.
25. Find extreme observations
Solution: Sort observations or use quartile-based methods to investigate unusually large or small values.
26. Calculate percentiles
Solution: Use pandas quantile functionality to locate observations at selected distribution positions.
27. Create a box plot
Solution: Use Matplotlib or Seaborn to visualize median, quartiles, and potential outliers.
28. Analyze data consistency
Solution: A narrow distribution indicates greater consistency, while a wide distribution indicates greater variation.
29. Compare engineering measurements
Solution: Compare both central tendency and dispersion rather than relying on averages alone.
30. Identify the most stable process
Solution: The process with lower meaningful variability is generally more consistent, provided the measurement scales are comparable.
Exercises 31–40: Probability
31. Count successful outcomes
Solution: Filter observations satisfying the required condition and count them.
32. Calculate observed frequency
Solution: Divide the number of qualifying observations by the total number of observations.
33. Estimate event probability
Solution: Use observed frequencies when estimating probability from historical data.
34. Analyze repeated events
Solution: Group observations according to event outcomes and compare their frequencies.
35. Create a probability table
Solution: Use pandas value counts and normalize the results to obtain relative frequencies.
36. Analyze categorical outcomes
Solution: Count each category and visualize the distribution with a bar chart.
37. Compare observed and expected patterns
Solution: Examine whether observed frequencies approximately follow the expected pattern.
38. Simulate random events
Solution: NumPy can generate repeated random outcomes and help demonstrate probabilistic behavior.
39. Increase simulation size
Solution: Larger simulations generally provide more stable estimates of expected behavior.
40. Visualize simulated probability
Solution: Plot the simulated outcomes and inspect how the distribution changes as observations increase.
Exercises 41–50: Distributions
41. Create a histogram
Solution: Use Matplotlib or Seaborn to display the frequency distribution of a numerical variable.
42. Identify skewness visually
Solution: Examine whether observations are concentrated toward one side with a longer tail.
43. Examine a normal-looking distribution
Solution: Use a histogram and additional diagnostic plots rather than assuming normality from appearance alone.
44. Compare distributions
Solution: Plot two datasets using consistent scales and compare their centers and spreads.
45. Identify outliers visually
Solution: Use box plots and histograms to detect unusual observations.
46. Examine distribution symmetry
Solution: Compare the position of the center with the shape of the tails.
47. Standardize observations
Solution: Use appropriate NumPy or SciPy functions to transform variables for comparative analysis.
48. Analyze sampling behavior
Solution: Repeatedly draw samples and examine how sample statistics vary.
49. Study sampling distributions
Solution: Simulation demonstrates how estimates behave across repeated samples.
50. Visualize distribution changes
Solution: Generate multiple samples and plot their statistical summaries to observe sampling variability.
Comparison
Statistical methods are not interchangeable.
| Analytical Goal | Useful Method | Typical Python Tool |
|---|---|---|
| Summarize data | Descriptive statistics | pandas |
| Measure spread | Variance/standard deviation | NumPy/pandas |
| Explore distribution | Histogram | Matplotlib |
| Detect outliers | Box plot | Seaborn |
| Compare averages | t-test | SciPy |
| Compare several groups | ANOVA | SciPy/Statsmodels |
| Measure relationship | Correlation | pandas/SciPy |
| Predict outcomes | Regression | Statsmodels/scikit-learn |
| Analyze categories | Chi-square | SciPy |
| Simulate probability | Random sampling | NumPy |
Diagrams & Tables
A useful statistical-analysis architecture can be represented as:
Raw Data → Cleaning → Exploration → Descriptive Statistics → Visualization → Statistical Test → Interpretation → Decision
Another useful framework is:
Question → Variable Types → Distribution → Assumptions → Method → Result → Practical Meaning
| Stage | Main Question |
|---|---|
| Collection | Where did the data come from? |
| Cleaning | Is the data reliable? |
| Exploration | What patterns exist? |
| Description | What does the dataset look like? |
| Inference | What can the sample tell us? |
| Modeling | Can relationships be quantified? |
| Interpretation | What does the result actually mean? |
Exercises 51–60: Correlation
51. Calculate Pearson correlation
Solution: Use SciPy or pandas correlation functionality for numerical variables.
52. Detect positive association
Solution: A positive correlation indicates that variables tend to increase together.
53. Detect negative association
Solution: A negative correlation indicates that one variable tends to increase as the other decreases.
54. Find weak relationships
Solution: Inspect correlation values and visualize the variables with scatter plots.
55. Create a correlation matrix
Solution: Use pandas correlation functionality and visualize the result as a heatmap.
56. Find strongly related variables
Solution: Search the correlation matrix for relationships that are both statistically and practically meaningful.
57. Create a scatter plot
Solution: Plot one numerical variable against another to visually inspect their relationship.
58. Investigate nonlinear relationships
Solution: A low linear correlation does not necessarily mean that variables have no relationship; visualize the data.
59. Compare correlation groups
Solution: Calculate correlations separately for different populations or categories.
60. Avoid correlation mistakes
Solution: Remember that correlation alone does not establish causation.
Exercises 61–70: Hypothesis Testing
61. Formulate a research question
Solution: Convert the practical question into a testable statistical hypothesis.
62. Define the null hypothesis
Solution: State the default assumption that the analysis will evaluate against evidence.
63. Define the alternative hypothesis
Solution: Specify the effect or difference that the researcher wants to investigate.
64. Perform a t-test
Solution: Select the appropriate SciPy t-test according to whether groups are independent or paired.
65. Compare two independent groups
Solution: Use an independent-samples statistical test after checking assumptions.
66. Compare before-and-after measurements
Solution: Use a paired test when the same subjects or units are measured twice.
67. Interpret a p-value
Solution: Compare the p-value with the selected significance threshold and consider practical importance.
68. Analyze statistical significance
Solution: Statistical significance indicates evidence against the null hypothesis, not necessarily a large or useful effect.
69. Report test results
Solution: Include the test type, relevant statistic, p-value, and practical interpretation.
70. Avoid p-value-only decisions
Solution: Combine significance testing with effect size, confidence intervals, data quality, and domain knowledge.
Exercises 71–80: Regression
71. Create a simple regression model
Solution: Use Statsmodels or scikit-learn to model a numerical outcome using a predictor.
72. Examine a regression coefficient
Solution: Interpret the coefficient as the expected change in the response associated with a change in the predictor, within the model.
73. Analyze model fit
Solution: Examine appropriate goodness-of-fit measures and diagnostic plots.
74. Create predictions
Solution: Supply new predictor values to the trained model and generate predicted outcomes.
75. Plot regression results
Solution: Combine a scatter plot with the fitted regression relationship.
76. Analyze residuals
Solution: Examine residual patterns to identify nonlinearity, unequal variance, or unusual observations.
77. Add another predictor
Solution: Extend the model when another variable provides meaningful explanatory information.
78. Compare models
Solution: Evaluate predictive performance and statistical diagnostics rather than relying on model complexity.
79. Detect overfitting
Solution: Compare training and validation performance and avoid unnecessary variables.
80. Interpret regression responsibly
Solution: Regression can reveal associations and predictions but does not automatically prove causality.
Exercises 81–90: Advanced Analysis
81. Perform ANOVA
Solution: Use ANOVA when comparing the means of multiple groups.
82. Analyze categorical variables
Solution: Convert categories into suitable statistical representations and inspect their frequencies.
83. Perform chi-square analysis
Solution: Use SciPy’s chi-square procedures to investigate relationships between categorical variables.
84. Create grouped summaries
Solution: Use pandas groupby to calculate statistics separately for different categories.
85. Calculate group averages
Solution: Group observations by category and calculate the desired numerical summary.
86. Compare group variability
Solution: Calculate dispersion statistics within each group.
87. Analyze time-dependent data
Solution: Convert dates to appropriate datetime formats and examine observations chronologically.
88. Detect trends
Solution: Visualize observations over time and investigate whether a systematic pattern exists.
89. Identify anomalous observations
Solution: Combine statistical rules with domain knowledge to identify potentially unusual records.
90. Build an analytical report
Solution: Combine tables, visualizations, statistical results, and written interpretation into a coherent report.
Exercises 91–100: Professional Data Analysis
91. Analyze customer ratings
Solution: Calculate distribution summaries, identify unusual ratings, and compare ratings between customer segments.
92. Analyze manufacturing measurements
Solution: Measure process consistency and investigate observations outside expected operating ranges.
93. Analyze employee performance
Solution: Compare performance distributions while controlling for relevant departments or job categories.
94. Analyze website traffic
Solution: Examine traffic distributions, trends, geographic segments, and relationships with engagement metrics.
95. Analyze sales data
Solution: Group sales by product, region, and time period to identify important patterns.
96. Analyze financial observations
Solution: Examine central tendency, volatility, distributions, and relationships between financial variables.
97. Analyze experimental data
Solution: Define hypotheses, inspect the data, select appropriate tests, and report uncertainty.
98. Create an automated statistical pipeline
Solution: Combine data loading, cleaning, analysis, visualization, and reporting into reusable Python functions.
99. Build a statistical dashboard
Solution: Present important indicators through charts, summary statistics, filters, and clearly labeled metrics.
100. Complete an end-to-end analysis
Solution: Start with a practical question, clean the dataset, explore distributions, calculate descriptive statistics, test relationships, build an appropriate model, validate the findings, and communicate the result.
Examples Without Equations
Example 1: Manufacturing Quality
An engineering company records measurements from manufactured components.
Python can identify the typical measurement, variation, extreme observations, and changes between production batches.
The analyst might discover that one production line has substantially greater variability than another.
The correct conclusion is not simply that one line has a different average. The analyst should investigate why the variation differs.
Example 2: Website Analytics
A website collects information about sessions, page views, devices, countries, and engagement.
Python can group the observations by country and device type. Statistical summaries may reveal that mobile users behave differently from desktop users.
The finding can support decisions about page design, performance optimization, and content strategy.
Example 3: Engineering Experiment
Suppose engineers compare two materials under similar laboratory conditions.
Python can summarize measurements, visualize their distributions, compare groups statistically, and identify unusual observations.
The final engineering decision should consider statistical evidence together with material cost, safety, durability, and manufacturing requirements.
Real-World Application
Statistics with Python has applications across almost every technical industry. 🌍
Engineering
Engineers use statistics for:
- Quality control
- Reliability analysis
- Experimental design
- Process optimization
- Sensor analysis
- Failure investigation
Data Science
Data scientists use statistical techniques for:
- Exploratory data analysis
- Feature analysis
- Model evaluation
- Experimentation
- Forecasting
- Anomaly detection
Business
Organizations use statistics to understand:
- Customer behavior
- Sales performance
- Market trends
- Operational efficiency
- Product performance
Healthcare and Research
Researchers use statistical analysis to evaluate experimental results, compare populations, investigate relationships, and quantify uncertainty.
Finance
Statistical techniques support:
- Risk analysis
- Portfolio research
- Market analysis
- Performance measurement
- Financial forecasting
Common Mistakes
Mistake 1: Ignoring Data Quality
Garbage data can produce misleading statistical conclusions.
Mistake 2: Using the Mean Automatically
The mean may be strongly affected by extreme observations.
Mistake 3: Confusing Correlation with Causation
Two variables can move together without one causing the other.
Mistake 4: Choosing Tests Before Understanding Data
The statistical method should follow the research question, variable types, study design, and assumptions.
Mistake 5: Ignoring Visualization
A single statistical summary can hide important distribution patterns.
Mistake 6: Treating Statistical Significance as Practical Importance
A statistically significant result may have little real-world impact.
Mistake 7: Forgetting Sampling Bias
A technically correct analysis can still produce poor conclusions when the sample does not represent the population.
Challenges & Solutions
| Challenge | Solution |
|---|---|
| Missing values | Investigate why values are missing before selecting a treatment |
| Outliers | Combine statistical detection with domain knowledge |
| Small samples | Report uncertainty and avoid overconfident conclusions |
| Non-normal data | Consider robust or non-parametric methods |
| High-dimensional data | Use structured exploratory analysis and appropriate visualization |
| Confounding variables | Consider study design and multivariable analysis |
| Reproducibility | Keep analysis code, data-processing steps, and documentation together |
| Misinterpretation | Translate statistical outputs into practical language |
Case Study
Manufacturing Process Investigation
Consider a hypothetical factory producing precision mechanical components.
The engineering team observes that customer complaints have increased. The production database contains measurements from several manufacturing machines.
The first stage is data inspection. Python reveals missing observations, duplicate records, and inconsistent machine labels.
After cleaning, the analyst summarizes measurements by machine.
A visualization shows that most machines produce relatively consistent measurements, while one machine produces a much wider distribution.
The team then investigates the machine’s operating conditions. Additional analysis indicates that its measurements change considerably during longer production periods.
Rather than changing the entire manufacturing process, engineers focus their investigation on that specific machine.
The statistical analysis therefore becomes a decision-support tool rather than merely a collection of numerical calculations.
This illustrates an important professional principle:
The goal of statistics is not to produce numbers; it is to produce reliable evidence for better decisions.
Essential Tips
For Beginners
🐍 Learn pandas before attempting advanced statistical modeling.
📊 Always visualize numerical data.
🔎 Understand your variables before selecting a test.
🧹 Treat data cleaning as part of statistical analysis.
📝 Write down the analytical question before writing Python code.
For Advanced Users
⚙️ Check assumptions before applying statistical procedures.
📈 Report uncertainty rather than presenting estimates without context.
🧠 Combine statistical evidence with domain expertise.
🔬 Distinguish exploratory analysis from confirmatory analysis.
🔁 Make analytical workflows reproducible.
💡 Focus on effect size and practical significance, not only statistical significance.
FAQs
What is the best Python library for statistics?
There is no single best library for every task. pandas is excellent for data manipulation and descriptive analysis, SciPy provides many statistical procedures, and Statsmodels is particularly useful for statistical modeling and inference.
Is Python difficult for learning statistics?
Python can make statistics easier because repetitive calculations can be automated. However, you still need to understand statistical concepts, assumptions, and interpretation.
Can beginners use these 100 exercises?
Yes. The exercises progress from basic data inspection and descriptive statistics toward more advanced analytical methods.
Should I learn mathematics before statistics with Python?
A basic understanding of mathematical concepts is useful, but beginners can start with practical statistical reasoning and gradually develop the mathematical foundations.
What should I learn after descriptive statistics?
A logical progression is probability, distributions, sampling, hypothesis testing, correlation, regression, and experimental design.
Is correlation enough to prove a relationship?
No. Correlation measures association between variables. It does not by itself demonstrate that one variable causes another.
Why are visualizations important in statistics?
Visualizations reveal patterns, distributions, outliers, clusters, and trends that can be difficult to recognize from numerical summaries alone.
Can Python statistics be used in engineering?
Absolutely. Statistical Python workflows are useful for quality control, reliability studies, experiments, manufacturing analysis, sensor data, and process optimization.
Conclusion
Statistics with Python provides a powerful bridge between raw data and evidence-based decision-making. 🐍📊
The 100 exercises in this guide progress from basic data inspection through descriptive statistics, probability, distributions, correlation, hypothesis testing, regression, ANOVA, categorical analysis, and professional analytical workflows.
The most important lesson is that statistical analysis is more than calling a Python function. A strong analyst asks:
What question am I trying to answer?
Is the data suitable for answering it?
Which statistical method is appropriate?
What assumptions apply?
What does the result actually mean?
When Python programming is combined with sound statistical reasoning, students and professionals can analyze complex datasets efficiently while producing conclusions that are clearer, more reproducible, and more useful.
🚀 Learn the statistics. Master the Python tools. Question the data. Then turn evidence into engineering and business decisions.




