Chapter 1 — Introduction to Python and the Data Science Workflow
Data science is the practice of turning raw data into insight and action. This chapter introduces the field, the Python ecosystem that powers it, and the end-to-end workflow that every data scientist follows. We close with a tiny, complete analysis so you can see the whole pipeline at a glance.
Learning Objectives
- Define data science and describe its typical lifecycle.
- Identify the major stages: question, collect, clean, explore, model, deploy, communicate.
- Explain why Python is a leading language for data science.
- Survey the core Python data-science libraries and what each does.
- Set up a Python 3.13 / Anaconda environment and use Jupyter notebooks.
- Run a complete, miniature data analysis from question to conclusion.
Prerequisites / Imports
This chapter uses only pandas, seaborn, and matplotlib.
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
1 What Is Data Science?
Data science combines statistics, computer science, and domain expertise to extract knowledge from data. A typical project follows a lifecycle:
- Business question — what decision or problem are we addressing?
- Data collection — acquire relevant data (databases, files, APIs, logs).
- Data cleaning — fix missing, inconsistent, and erroneous values.
- Exploratory data analysis (EDA) — summarize and visualize to find patterns.
- Modeling — build predictive or inferential models.
- Deployment — put models/insights into production.
- Communication — tell the story to stakeholders.
Popular process frameworks include CRISP-DM (Cross-Industry Standard Process for Data Mining) and OSEMN (Obtain, Scrub, Explore, Model, iNterpret).
2 Why Python for Data Science?
Python is readable, general-purpose, and has an enormous ecosystem of libraries:
- NumPy — fast numerical arrays.
- Pandas — tabular data manipulation.
- Matplotlib / Seaborn / Plotly — visualization.
- scikit-learn — classical machine learning.
- TensorFlow / Keras / PyTorch — deep learning.
- statsmodels — statistical modeling.
- NLTK / Transformers — natural language processing.
Together these cover the entire workflow from data ingestion to deployed model.
3 Installing Python and the Anaconda Distribution
The recommended setup is the latest Anaconda distribution with Python 3.13. Anaconda bundles Python, Jupyter, and most data-science libraries. Use conda environments to isolate project dependencies.
Code shown for illustration; not executed in this notebook.
# Create a fresh environment with Python 3.13
conda create -n datascience python=3.13
conda activate datascience
# Install the core stack
conda install numpy pandas matplotlib seaborn scikit-learn scipy statsmodels
conda install jupyterlab
4 Jupyter Notebook and JupyterLab Basics
A notebook is a sequence of cells: markdown cells for narrative and code cells for executable Python. Run a cell with Shift+Enter. Magic commands provide convenience:
%matplotlib inline— show plots in the notebook.%timeit— time a statement.%%time— time a whole cell.
# Timing a small operation with the time module (runs cleanly in-process)
import time
start = time.perf_counter()
total = sum(range(1_000_000))
elapsed = time.perf_counter() - start
print('sum =', total)
print(f'elapsed: {elapsed*1000:.3f} ms')
sum = 499999500000 elapsed: 349.131 ms
5 Your First Data Science Program
Let's load the bundled tips dataset, peek at it, summarize it, and draw one plot. This is the entire pipeline in five lines.
tips = sns.load_dataset('tips')
tips.head()
| total_bill | tip | sex | smoker | day | time | size | |
|---|---|---|---|---|---|---|---|
| 0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
| 1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
| 2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
| 3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
| 4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
tips.describe()
| total_bill | tip | size | |
|---|---|---|---|
| count | 244.000000 | 244.000000 | 244.000000 |
| mean | 19.785943 | 2.998279 | 2.569672 |
| std | 8.902412 | 1.383638 | 0.951100 |
| min | 3.070000 | 1.000000 | 1.000000 |
| 25% | 13.347500 | 2.000000 | 2.000000 |
| 50% | 17.795000 | 2.900000 | 2.000000 |
| 75% | 24.127500 | 3.562500 | 3.000000 |
| max | 50.810000 | 10.000000 | 6.000000 |
plt.figure(figsize=(7,4))
sns.histplot(tips['total_bill'], kde=True)
plt.title('Distribution of Total Bill')
plt.xlabel('Total bill ($)')
plt.show()
6 The Data Science Workflow in Practice
Following OSEMN, the snippet below Obtains bundled data, Scrubs by checking for missing values, Explores with a grouped summary, and prepares for modeling.
print('Shape:', tips.shape)
print('Missing values per column:')
print(tips.isna().sum())
Shape: (244, 7) Missing values per column: total_bill 0 tip 0 sex 0 smoker 0 day 0 time 0 size 0 dtype: int64
tips.groupby('time')['total_bill'].agg(['mean','median','count'])
<cell-expr>:1: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.
| mean | median | count | |
|---|---|---|---|
| time | |||
| Lunch | 17.168676 | 15.965 | 68 |
| Dinner | 20.797159 | 18.390 | 176 |
7 Best Practices
- Reproducibility: pin library versions, set random seeds.
- Version control: keep notebooks and code in git.
- Environments: one conda/venv per project.
- Documentation: write narrative in markdown cells; comment non-obvious code.
Case Study: From Question to Insight
Question: Do customers who smoke tip differently than non-smokers?
We load, summarize by group, visualize, and state a conclusion — the full workflow on one question.
tips = sns.load_dataset('tips')
summary = tips.groupby('smoker')['tip'].agg(['mean','median','std','count'])
summary
<cell-prefix>:2: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.
| mean | median | std | count | |
|---|---|---|---|---|
| smoker | ||||
| Yes | 3.008710 | 3.00 | 1.401468 | 93 |
| No | 2.991854 | 2.74 | 1.377190 | 151 |
plt.figure(figsize=(7,4))
sns.boxplot(data=tips, x='smoker', y='tip')
plt.title('Tip amount by smoker status')
plt.show()
Conclusion: The mean tip is similar between smokers and non-smokers, though the spread differs. A formal hypothesis test (Chapter 14) could confirm whether the difference is statistically significant.
Exercises
- List the seven stages of the data science lifecycle and give a one-sentence example of each.
- Name three Python libraries and describe what each is used for.
- Write commands to create a conda environment named
dswith Python 3.13 and install pandas. - Load the seaborn
penguinsdataset and display its first 8 rows. - Compute the mean body mass of penguins by species.
- Make a histogram of penguin flipper lengths.
- Explain the difference between
condaandpip. - Why is reproducibility important in data science?
Python Data Science: From Foundations to Applications — Chapter 1