Basics
Use variables, f-strings and a loop to summarize a list of scores.
name = "Skillancy"
scores = [72, 85, 90, 64]
average = sum(scores) / len(scores)
print(f"{name} average score: {average:.1f}")
for s in scores:
print(s, "pass" if s >= 70 else "fail")
Open in editorList comprehension and counting
Transform a list with a comprehension and count repeated words with collections.Counter.
from collections import Counter
words = "data analytics needs clean data and clear questions".split()
lengths = [len(w) for w in words]
print(lengths)
print(Counter(words).most_common(2))
Open in editorFunctions
Write a function that removes duplicates while preserving the original order.
def dedupe(items):
seen = set()
result = []
for item in items:
if item not in seen:
seen.add(item)
result.append(item)
return result
print(dedupe([3, 1, 3, 2, 1, 4]))
Open in editorPandas groupby
Build a DataFrame and summarize sales by city with groupby and aggregation.
import pandas as pd
df = pd.DataFrame({
"city": ["Pune", "Delhi", "Pune", "Chennai", "Delhi"],
"sales": [120, 90, 150, 60, 110],
})
print(df.groupby("city")["sales"].agg(["sum", "mean"]))
Open in editorPandas merge
Join two DataFrames with the Pandas equivalent of a SQL LEFT JOIN.
import pandas as pd
orders = pd.DataFrame({"order_id": [1, 2, 3], "customer_id": [10, 11, 10], "amount": [250, 400, 150]})
customers = pd.DataFrame({"customer_id": [10, 11, 12], "name": ["Asha", "Ravi", "Meera"]})
merged = orders.merge(customers, on="customer_id", how="left")
print(merged)
Open in editorMissing values
Find missing values and fill numeric and text columns with appropriate replacements.
import numpy as np
import pandas as pd
df = pd.DataFrame({
"age": [25, np.nan, 31, np.nan, 40],
"city": ["Pune", "Delhi", None, "Delhi", "Pune"],
})
print(df.isna().sum())
df["age"] = df["age"].fillna(df["age"].median())
df["city"] = df["city"].fillna("Unknown")
print(df)
Open in editorNumPy statistics
Use vectorized calculations to find summary statistics, filter values and calculate z-scores.
import numpy as np
data = np.array([12, 15, 11, 18, 20, 14])
print("mean:", data.mean(), "std:", round(data.std(), 2))
print("above mean:", data[data > data.mean()])
z = (data - data.mean()) / data.std()
print(np.round(z, 2))
Open in editorDates and rolling averages
Create a daily time series and smooth a metric with a three-day rolling average.
import pandas as pd
days = pd.date_range("2026-01-01", periods=10, freq="D")
df = pd.DataFrame({
"date": days,
"visits": [120, 135, 128, 150, 160, 155, 170, 165, 180, 175],
})
df["rolling_3d"] = df["visits"].rolling(3).mean().round(1)
print(df)
Open in editor