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

Prerequisites / Imports

This chapter uses only pandas, seaborn, and matplotlib.

In [1]:
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:

  1. Business question — what decision or problem are we addressing?
  2. Data collection — acquire relevant data (databases, files, APIs, logs).
  3. Data cleaning — fix missing, inconsistent, and erroneous values.
  4. Exploratory data analysis (EDA) — summarize and visualize to find patterns.
  5. Modeling — build predictive or inferential models.
  6. Deployment — put models/insights into production.
  7. 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:

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:

In [1]:
# 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.

In [1]:
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
In [1]:
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
In [1]:
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.

In [1]:
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
In [1]:
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

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.

In [1]:
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
In [1]:
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

  1. List the seven stages of the data science lifecycle and give a one-sentence example of each.
  2. Name three Python libraries and describe what each is used for.
  3. Write commands to create a conda environment named ds with Python 3.13 and install pandas.
  4. Load the seaborn penguins dataset and display its first 8 rows.
  5. Compute the mean body mass of penguins by species.
  6. Make a histogram of penguin flipper lengths.
  7. Explain the difference between conda and pip.
  8. Why is reproducibility important in data science?

Python Data Science: From Foundations to Applications — Chapter 1