Core data structures
- list — ordered, changeable: [1, 2, 3]
- tuple — ordered, unchangeable: ("a", 1)
- dict — key → value lookup: {"name": "Asha", "score": 91}
- set — unique values: {"python", "sql"}
scores = {"Asha": 91, "Ravi": 78, "Meena": 85}
for name, score in scores.items():
print(f"{name}: {score}")
top = max(scores, key=scores.get) # 'Asha'Comprehensions and functions
Comprehensions build collections in one readable line. Functions keep logic reusable and testable.
squares = [n * n for n in range(10)]
passed = {k: v for k, v in scores.items() if v >= 80}
def grade(score: int) -> str:
"""Return a letter grade for a numeric score."""
if score >= 90:
return "A"
if score >= 75:
return "B"
return "C"NumPy essentials
NumPy arrays are fast, fixed-type containers for numerical work. Operations apply to every element at once (vectorisation).
import numpy as np
a = np.array([4, 8, 15, 16, 23, 42])
a.mean(), a.std(), a.max()
a[a > 10] # boolean filtering
(a - a.mean()) / a.std() # standardisepandas: load, inspect, select
import pandas as pd
df = pd.read_csv("sales.csv")
df.head() # first rows
df.info() # columns, types, nulls
df.describe() # summary statistics
df["region"] # one column
df[["region", "revenue"]] # several columns
df[df["revenue"] > 10000] # filter rows
df.loc[df["region"] == "South", "revenue"]pandas: clean and transform
df = df.dropna(subset=["revenue"])
df["order_date"] = pd.to_datetime(df["order_date"])
df["month"] = df["order_date"].dt.to_period("M")
df["region"] = df["region"].str.strip().str.title()
df = df.rename(columns={"qty": "quantity"})pandas: group, aggregate, join
summary = (
df.groupby("region")
.agg(total=("revenue", "sum"), orders=("order_id", "count"))
.sort_values("total", ascending=False)
)
merged = df.merge(customers, on="customer_id", how="left")
pivot = df.pivot_table(index="month", columns="region", values="revenue", aggfunc="sum")Quick plotting
import matplotlib.pyplot as plt
summary["total"].plot(kind="bar", title="Revenue by region")
plt.tight_layout()
plt.show()Habits that make you faster
- Work in Jupyter for exploration, then move stable code into .py files
- Use virtual environments (venv or conda) per project
- Prefer vectorised pandas/NumPy operations over Python loops
- Name variables for what they hold — revenue_by_region, not df2
- Version your work with Git from day one