4  Python Exercises

5 Python Exercises

This chapter demonstrates Python exercises using Pyodide (Python in the browser). No COI is needed for Pyodide-only pages — it boots on the main thread.

5.1 Exercise 1: Square function

Write a function square(n) that returns the square of a number.

Exercise 1

Write a function square(n) that returns n * n.

def square(n):
    ___

5.2 Exercise 2: Even check

Write a function is_even(n) that returns True if n is even.

Exercise 2

Write a function is_even(n) that returns True if n is even.

def is_even(n):
    ___

5.3 Exercise 3: Clean a messy sales dataset

Clean up a messy sales CSV with pandas. Normalize prices and categories, drop missing values and duplicate rows, and compute total revenue per category.

Exercise 3

Clean up a messy sales dataset with pandas. The dataset is a CSV of 24 sales rows with five columns: order_id, product, category, price, and quantity. It was deliberately dirtied to test your data-wrangling skills: prices appear with a dollar sign, with a space after the dollar sign, as bare numbers, or as the word unknown; categories are inconsistent in case and whitespace and include the abbreviation Elec; some quantities are empty; and a few rows are exact duplicates.

Work through six wrangling steps in order. Step 1, print the given data so you can see what you are working with. Step 2, normalize the price column: strip dollar signs and spaces with str.replace, then convert with pd.to_numeric and errors='coerce' so that anything non-numeric becomes NaN. Step 3, normalize the category column with str.strip and str.lower, then apply an alias map so the abbreviation Elec becomes electronics. Step 4, drop NA rows with dropna, do NOT impute. Step 5, drop exact duplicates with drop_duplicates. Step 6, compute revenue as price times quantity and aggregate it per category with groupby.

Name your final variables clean_df and revenue_by_cat. clean_df must be the cleaned data frame: no missing price or quantity, no duplicate rows, and exactly three normalized categories: books, clothing, and electronics. revenue_by_cat must be a Series with the total revenue per category, computed from the cleaned data only. Print revenue_by_cat at the end so your captured output shows the result.

import pandas as pd
import io

csv = """order_id,product,category,price,quantity
1,Widget,Electronics,$12.50,3
2,Gadget,electronics,$8.00,2
3,Book A,Books,$15.00,1
4,Shirt,Clothing,$20.00,2
1,Widget,Electronics,$12.50,3
5,Gadget,ELECTRONICS ,unknown,2
6,Book B,books,$15.00,
7,Pants,Clothing ,$25.00,1
8,Widget,Elec,$12.50,3
2,Gadget,electronics,$8.00,2
9,Book C,Books,15.00,2
10,Shirt,clothing,$20.00,2
11,Hat,Clothing,$10.00,
12,Gadget,electronics,$8.00,1
13,Book D,books,$18.00,2
14,Widget,Electronics,$ 5.00,4
15,Pants,clothing ,$25.00,1
16,Book E,Books,$15.00,1
17,Gadget,ELECTRONICS,$8.00,2
18,Widget,electronics,$12.50,3
19,Shirt,Clothing,$20.00,2
3,Book A,Books,$15.00,1
20,Book F,books,unknown,1
21,Hat,clothing,$10.00,2"""

sales = pd.read_csv(io.StringIO(csv))

# Step 1: print the given data
print(sales.head())

# Step 2: normalize the price column with pd.to_numeric (strip $ and spaces, coerce bad values to NaN)
___

# Step 3: normalize the category column with str.strip, str.lower, and an Elec -> electronics alias map
___

# Step 4: drop NA rows in price and quantity (do NOT impute)
___

# Step 5: drop exact duplicates
___

# Step 6: compute revenue = price * quantity, then aggregate by category with groupby
___

# Name your final variables clean_df and revenue_by_cat
Hints- `pd.to_numeric(..., errors='coerce')` turns non-numeric prices into `NaN`. - Strip `$` and spaces from the price first: `.str.replace(r'[$ ]', '', regex=True)`. - Normalize categories with `.str.strip().str.lower()`, then map the abbreviation with an alias dict like `{'elec': 'electronics'}`. - `dropna(subset=['price', 'quantity'])` removes rows with missing values --- drop, do not impute. - `drop_duplicates()` removes exact duplicate rows. - After computing `revenue = price * quantity`, aggregate with `clean_df.groupby('category')['revenue'].sum()`.