Python for Data Science (2026)
PYTHON FOR DATA
SCIENCE
From Basics to Advanced
The Complete 2026 Industry-Ready Guide
Page 1
Python for Data Science (2026)
1. INTRODUCTION TO PYTHON FOR DATA
SCIENCE
Why Python is Used in Data Science
Python has become the undisputed lingua franca of Data Science and Artificial Intelligence. Its dominance
stems from a perfect storm of features: a gentle learning curve, an incredibly rich ecosystem of specialized
libraries, and its versatility. Unlike R, which is highly specialized for statistics, Python is a general-purpose
language. This means you can build a machine learning model, deploy it behind a REST API, and integrate it
into a web application—all using the same language.
Python Ecosystem Overview
The Python Data Science ecosystem is a stack of libraries built on top of each other:
• Foundation: Python standard library.
• Numerical/Mathematical: NumPy (fast array operations), SciPy (scientific computing).
• Data Manipulation: Pandas (dataframes).
• Visualization: Matplotlib , Seaborn , Plotly .
• Machine Learning: Scikit-learn , XGBoost .
• Deep Learning: TensorFlow , PyTorch .
Setting Up Your Environment
For modern data science, managing environments is critical to avoid dependency conflicts.
• Anaconda/Miniconda: The industry standard for managing Python environments. Conda handles
complex C-library dependencies (crucial for tools like NumPy) better than standard `pip`.
• Jupyter Notebooks: Ideal for exploratory data analysis (EDA). They allow you to run code in blocks (cells)
and see visualizations inline.
• VS Code: The preferred IDE for productionizing data science code. It has excellent support for Jupyter
natively, as well as debugging tools.
Page 2
Python for Data Science (2026)
Writing Clean and Efficient Code
Data science code often starts as a messy notebook. Transitioning to production requires adhering to PEP-8
standards, using meaningful variable names (e.g., customer_data instead of df1 ), and modularizing code
into functions.
2. PYTHON BASICS (FOUNDATION)
Variables, Data Types, and Type Conversion
Python is dynamically typed. The core data types are integers, floats, strings, and booleans.
# Variables and Types
age = 28 # int
salary = 95000.50 # float
name = "Alice" # str
is_analyst = True # bool
# Type Conversion
age_str = str(age)
salary_int = int(salary)
Control Flow
Control flow directs the execution path of your program.
# If-Elif-Else
if salary > 100000:
print("High earner")
elif salary > 50000:
print("Mid-level earner")
else:
print("Entry-level")
# For Loops and While Loops
for i in range(5):
if i == 3:
continue # Skip 3
print(i)
Page 3
Python for Data Science (2026)
Functions
Functions encapsulate logic for reuse. lambda functions are anonymous functions heavily used in data
transformations.
def calculate_discount(price, discount_rate=0.1):
return price * (1 - discount_rate)
# Lambda function (often used with Pandas)
square = lambda x: x ** 2
Data Structures
Python's built-in data structures are the building blocks before we get to Pandas.
• Lists: Ordered, mutable arrays. `[1, 2, 3]`
• Tuples: Ordered, immutable arrays. `(1, 2, 3)` (Faster than lists, used for fixed data).
• Sets: Unordered collections of unique elements. `{1, 2, 3}` (Great for removing duplicates or fast
membership testing).
• Dictionaries: Key-value pairs. `{"name": "Alice", "age": 25}`.
users = ["Alice", "Bob", "Charlie"]
# Slicing: start:stop:step
print(users[0:2]) # ['Alice', 'Bob']
user_info = {"name": "Alice", "role": "Data Scientist"}
print(user_info.get("role", "Not Found"))
File Handling
While Pandas is used for complex CSVs, native file handling is useful for logs and simple text.
import json
# Writing a JSON file
data = {"model": "RandomForest", "accuracy": 0.95}
with open("[Link]", "w") as file:
[Link](data, file)
Page 4
Python for Data Science (2026)
3. INTERMEDIATE PYTHON
Comprehensions
Comprehensions provide a concise way to create lists, dictionaries, or sets. They are often faster than
standard for loops.
# List Comprehension
prices = [10, 20, 30]
taxed_prices = [p * 1.2 for p in prices if p > 15]
# Dictionary Comprehension
names = ["Alice", "Bob"]
name_lengths = {name: len(name) for name in names}
Exception Handling
Robust data pipelines must handle unexpected data gracefully.
try:
result = 10 / 0
except ZeroDivisionError as e:
print(f"Error: {e}")
finally:
print("Execution complete.")
Basic Object-Oriented Programming (OOP)
Understanding OOP is crucial when building custom Scikit-learn transformers or managing complex data
pipelines.
class DataProcessor:
def __init__(self, data):
[Link] = data
def clean(self):
# Implementation here
return [str(item).strip() for item in [Link]]
Page 5
Python for Data Science (2026)
4. NUMPY (DEEP DIVE - CORE LIBRARY)
Basics: Arrays vs. Lists
NumPy (Numerical Python) is the foundation of Pandas and Scikit-learn. The core object is the ndarray .
Unlike Python lists, NumPy arrays are homogenous (contain one data type) and are stored in contiguous
memory blocks, making them orders of magnitude faster.
import numpy as np
# Creating arrays
arr = [Link]([1, 2, 3, 4, 5])
zeros = [Link]((3, 3))
random_matrix = [Link](3, 3)
Vectorization and Broadcasting
Vectorization: Applying an operation to an entire array at once, rather than iterating through it with a
loop. This pushes the loop down to optimized C code.
# Bad: Python Loop
prices = [10, 20, 30]
new_prices = []
for p in prices:
new_prices.append(p * 2)
# Good: NumPy Vectorization
prices_arr = [Link]([10, 20, 30])
new_prices_arr = prices_arr * 2
Broadcasting allows NumPy to perform operations on arrays of different shapes by virtually replicating the
smaller array.
matrix = [Link]([[1, 2, 3], [4, 5, 6]]) # Shape (2, 3)
vector = [Link]([10, 20, 30]) # Shape (3,)
# Broadcasting applies the vector to each row of the matrix
result = matrix + vector
# Result: [[11, 22, 33], [14, 25, 36]]
Page 6
Python for Data Science (2026)
Advanced Operations
NumPy includes robust linear algebra tools, essential for machine learning algorithms.
A = [Link]([[1, 2], [3, 4]])
B = [Link]([[5, 6], [7, 8]])
# Matrix Multiplication (Dot Product)
C = [Link](A, B) # or A @ B
# Inverse of a matrix
A_inv = [Link](A)
5. PANDAS (VERY IMPORTANT - DEEP DIVE)
Series and DataFrames
Pandas introduces two primary structures: Series (1D array with an index) and DataFrame (2D table of
Series). Think of a DataFrame as an in-memory SQL table or Excel sheet.
import pandas as pd
# Creating a DataFrame
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David'],
'Age': [25, [Link], 30, 22],
'Department': ['Sales', 'IT', 'IT', 'HR'],
'Salary': [50000, 70000, 75000, 45000]
}
df = [Link](data)
Data Manipulation & Filtering
Selecting data is primarily done using loc (label-based) and iloc (integer/position-based).
Page 7
Python for Data Science (2026)
# Selecting columns
ages = df['Age']
# Filtering data (Boolean indexing)
it_employees = df[df['Department'] == 'IT']
# Complex filtering
high_earners = df[(df['Salary'] > 60000) & (df['Age'] > 25)]
# loc vs iloc
row_1 = [Link][0] # First row by index position
alice_data = [Link][df['Name'] == 'Alice']
Data Cleaning
Real-world data is messy. Handling missing values is a daily task.
# Check for nulls
print([Link]().sum())
# Drop rows with nulls
df_clean = [Link]()
# Fill nulls with the median age
df['Age'] = df['Age'].fillna(df['Age'].median())
# Data type conversion
df['Salary'] = df['Salary'].astype(float)
Data Transformation & Aggregation
Grouping and aggregating data is similar to SQL's GROUP BY .
# GroupBy
dept_stats = [Link]('Department')['Salary'].agg(['mean', 'count'])
# Pivot Tables
pivot = df.pivot_table(values='Salary', index='Department', aggfunc='mean')
Page 8
Python for Data Science (2026)
Merging and Joining
# Merging DataFrames (like SQL JOIN)
df_bonus = [Link]({'Department': ['IT', 'HR'], 'Bonus': [5000, 2000]})
merged_df = [Link](df, df_bonus, on='Department', how='left')
6. DATA VISUALIZATION IN PYTHON
Visualization is key for Exploratory Data Analysis (EDA) and communicating results.
• Matplotlib: The foundation. Highly customizable but verbose. Good for basic line/scatter plots.
• Seaborn: Built on Matplotlib. Provides beautiful default styles and high-level interfaces for statistical plots
(e.g., heatmaps, violin plots).
• Plotly: Interactive visualizations (hover details, zoom) ideal for dashboards.
import seaborn as sns
import [Link] as plt
# Seaborn Statistical Plot
[Link](data=df, x='Age', y='Salary', hue='Department')
[Link]("Salary vs Age by Department")
[Link]()
7. DATA ANALYSIS WORKFLOW (PRACTICAL
STEP-BY-STEP)
A standard workflow follows these steps:
1. Loading Data: df = pd.read_csv('[Link]')
2. Inspection: [Link]() , [Link]() , [Link]()
3. Cleaning: Handle duplicates ( df.drop_duplicates() ), fix data types, handle missing values.
4. EDA: Plot distributions (histograms) and correlations (heatmaps).
5. Feature Creation (Engineering): Creating new variables from existing ones.
Page 9
Python for Data Science (2026)
# Feature Engineering Example
# Creating a 'Seniority' column based on Age
df['Seniority'] = [Link](df['Age'], bins=[0, 25, 40, 100], labels=['Junior', 'Mid',
'Senior'])
8. PERFORMANCE & OPTIMIZATION
Python is relatively slow, but its data science libraries are fast if used correctly.
The Golden Rule: Never iterate through a Pandas DataFrame using iterrows() or a for loop if you
can avoid it.
• Level 1 (Slowest): for index, row in [Link]():
• Level 2 (Better): [Link](lambda row: func(row), axis=1)
• Level 3 (Fastest): Vectorization. df['new_col'] = df['col1'] + df['col2']
Memory Optimization: By default, Pandas loads integers as int64 . If your data ranges from 0-100,
downcast it to int8 to save massive amounts of RAM.
9. ADDITIONAL ESSENTIAL LIBRARIES
• Scikit-learn: The standard for traditional machine learning (Regression, Classification, Clustering).
Provides clean APIs ( fit , predict ).
• Statsmodels: For rigorous statistical modeling and hypothesis testing (p-values, confidence intervals).
• OpenPyXL: Required for Pandas to read/write modern Excel files ( .xlsx ).
Page 10
Python for Data Science (2026)
10. PROJECTS TO BUILD YOUR PORTFOLIO
Beginner: Dirty Data Cleanup
Goal: Take a raw dataset (e.g., messy sales data from Kaggle). Standardize date formats, fix typos in
categorical columns using string methods ( .[Link]() , .[Link]() ), handle missing values
intelligently, and export a clean CSV.
Intermediate: Exploratory Data Analysis (EDA) Report
Goal: Analyze an e-commerce dataset. Calculate KPIs like Monthly Recurring Revenue (MRR), Customer
Acquisition Cost (CAC), and cohort retention. Use Seaborn to visualize seasonal trends and Pandas to group
data temporally ( [Link]('M') ).
Advanced: End-to-End Pipeline
Goal: Scrape data using BeautifulSoup or an API, store it in an SQLite database, load it into Pandas for
feature engineering, train a Scikit-learn Random Forest model, and output the predictions.
11. BEST PRACTICES
• Code Organization: Do not write a 500-cell Jupyter notebook. Move complex logic into `.py` files and
import them into your notebook.
• Chaining in Pandas: Write functional, readable Pandas code using method chaining.
# Method Chaining
clean_df = (df
.dropna(subset=['Salary'])
.assign(Salary_k=lambda x: x['Salary'] / 1000)
.query('Age > 25')
)
Page 11
Python for Data Science (2026)
12. COMMON MISTAKES
• SettingWithCopyWarning: Modifying a slice of a DataFrame without explicitly using .copy() . Always
use .copy() when you split data.
• Data Leakage: In ML, scaling or imputing missing values on the whole dataset *before* splitting into train/
test sets. Always split first!
• Ignoring Indexes: Pandas relies heavily on the index. Forgetting to reset_index() after grouping or
filtering can cause silent alignment bugs.
13. CAREER PREPARATION
How Much Python is Enough?
You don't need to be a software engineering master to be a Data Scientist. You need to master Pandas,
understand vectorization, know how to write clean functions, and be comfortable with Scikit-learn APIs.
Interview Preparation
Data Science Python interviews usually test:
1. Data Manipulation: Can you do complex `groupby` and `merge` operations in Pandas from memory?
2. Algorithms/Logic: Standard LeetCode easy/medium problems (arrays, string manipulation) to prove basic
programming logic.
3. Applied ML: Given a messy dataset, write the code to clean it and fit a basic model.
Action Step: Build a GitHub portfolio containing 2-3 well-documented Jupyter notebooks that solve real
business problems, not just generic datasets like Iris or Titanic.
Page 12