Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lab 1: Python On-Ramp with Qahwa House

Open In Colab

Lab 1: Python On-Ramp with Qahwa House

In this lab you learn enough Python and pandas to answer a real business question with data.

The question. Qahwa House, a café chain with four branches, ran a second cup half price promotion on about one day in seven during 2025. Should it run the promotion again, and on which items?

How this lab works

  • Run the cells from top to bottom with Shift + Enter.

  • Guided track: replace every ... with your code, then run the check cell under it. A ✅ means your answer is right.

  • Extended track: after Part C, try the ★ challenges at the end.

  • Submit before the next lab session: File → Download → Download .ipynb, then upload the file to the course page.

You may use an AI assistant for code. If you do, add a short note at the end of the notebook: what you asked, what you kept and what you changed.

Part A: Python warm-up

A1. Numbers and variables

A Spanish latte sells for 18 SAR and costs 5 SAR to make. Store these in two variables, then compute the margin (price minus cost) and the margin percentage (margin divided by price, times 100).

A2. Lists

The Al-Kharj branch sold these numbers of lattes over one week, Sunday to Saturday. Use the built-in functions sum, len and max to find the total, the daily average and the best day’s sales.

A3. Functions

Write a function margin_pct(price, cost) that returns the margin percentage. Functions let you reuse a calculation, and a model is usually a set of functions.

A4. Dictionaries and loops

A dictionary maps keys to values. Here each product maps to its (price, cost) pair. Loop over it and print one line per product with its margin percentage, using your function from A3 and an f-string. The first line should read Arabic Coffee: 75.0%.

Notice the gap between drinks and food. Keep it in mind for Part C.

Part B: pandas basics

pandas is the Python library for tables of data. A table is called a DataFrame; each column is a Series.

B1. Load the data

The cell below downloads the Qahwa House sales for 2025 from the course website. Run it as it is.

B2. First look

Use sales.shape to find the number of rows and columns, sales.head() to see the first rows, and sales.info() to see each column’s type. Each row is one product, in one branch, on one day.

ColumnMeaning
date, weekdaythe day
branch, citywhere
product, categorywhat was sold (Drink or Food)
list_pricenormal price, SAR per unit
promo1 on promotion days, 0 otherwise
price_paidSAR per unit actually paid (25% lower on promotion days)
unit_costSAR per unit to make
unitsunits sold that day

B3. Select and filter

  • sales["units"] selects one column; sales[["branch", "units"]] selects several.

  • sales[sales["city"] == "Jeddah"] keeps the rows that satisfy a condition. Combine conditions with & and put each in parentheses.

Keep only the Iced Latte rows of the University branch, and store them in iced_uni. How many units did that branch sell in the whole year?

B4. New columns

Add three columns, each computed from existing ones for every row at once:

  • revenue = units × price paid

  • cost = units × unit cost

  • profit = revenue − cost

B5. Group and summarise

groupby splits the table into groups, applies a summary to each group and combines the results. For example, sales.groupby("city")["units"].sum() gives the units sold in each city.

Compute total revenue and total profit for each branch in 2025, sorted from the most to the least profitable branch. Store the result in by_branch.

B6. Profit margin by product

For each product, compute total revenue and total profit, then add a column margin_pct = profit / revenue × 100. Sort by margin. Which category earns the thinner margins?

B7. A first chart

Run this cell. It adds up revenue by month and branch, then draws one line per branch. Look for the summer and the Ramadan effects.

Part C: The decision

Each branch decides on the promotion for a whole day, so the right unit of comparison is a branch-day: all sales of one branch on one date.

C1. One row per branch-day

Group by date, branch and promo, and sum revenue and profit. Use as_index=False so the group columns stay as ordinary columns. Store the result in daily. It should have 4 × 365 = 1,460 rows.

C2. Promotion days against normal days

Compute the average daily revenue and profit for normal days (promo == 0) and promotion days (promo == 1). Then compute the percentage change from normal to promotion days.

C3. Drinks and food separately

Repeat the comparison by category: group the row-level sales table by date, branch, promo and category, sum, then average by category and promo. Store the average daily profit table in by_cat with categories as rows and promo as columns.

C4. Your recommendation

Write three sentences for the regional manager, who does not read code:

  1. what the data shows, with one or two numbers;

  2. what you recommend;

  3. one thing the data cannot tell us, which could change your recommendation.

Write your recommendation here (double-click to edit).

★ Extended challenges

E1. Does it depend on the branch? Repeat C2 for each branch separately. Is there a branch where the promotion raises profit?

E2. A drinks-only promotion. Estimate the extra annual profit if every branch ran the promotion on drinks only, on every Thursday. State each assumption you make.

E3. A fair comparison? Promotion days were chosen at random. Check it: compute the share of promotion days for each weekday. Why would the comparison in C2 be misleading if promotions had always been run on Fridays?

AI-use log (if you used an AI assistant)

What you asked, what you kept, what you changed.