Loop over dictionaries & compute averages
Iterate key-value pairs cleanly with .items() and compute summary statistics.
scores = {"Asha": 82, "Ravi": 74, "Meera": 91}
for name, score in scores.items():
print(f"{name}: {score}")
print("Average:", round(sum(scores.values()) / len(scores), 2))Pandas DataFrame filter & groupby aggregation
Filter rows and aggregate metrics by category using pandas.
import pandas as pd
df = pd.DataFrame({"city": ["Mumbai", "Pune", "Mumbai"], "sales": [120, 90, 150]})
print(df.groupby("city")["sales"].agg(["count", "sum", "mean"]))NumPy array math & percentiles
Compute mean, standard deviation, and 90th percentile on numerical arrays.
import numpy as np
arr = np.array([14, 22, 35, 41, 58, 63])
print("Mean:", np.mean(arr), "Std:", round(np.std(arr), 2), "P90:", np.percentile(arr, 90))INNER JOIN + GROUP BY + ORDER BY
Combine relational tables and rank groups by average score.
SELECT s.city, COUNT(*) AS enrolments, ROUND(AVG(e.score), 1) AS avg_score
FROM students s
JOIN enrolments e ON e.student_id = s.id
GROUP BY s.city
ORDER BY avg_score DESC;
Filter aggregated groups with HAVING
Use WHERE before grouping and HAVING after GROUP BY to filter aggregates.
SELECT course, COUNT(*) AS learners, AVG(score) AS avg_score
FROM enrolments
GROUP BY course
HAVING COUNT(*) >= 2;
Window function RANK() OVER
Rank rows within each group without collapsing rows.
SELECT student_id, course, score,
RANK() OVER (PARTITION BY course ORDER BY score DESC) AS course_rank
FROM enrolments;